Skip to content

Instantly share code, notes, and snippets.

@Cdaprod
Created February 10, 2024 21:24
Show Gist options
  • Select an option

  • Save Cdaprod/2ab19e7c6420d2aba45b848ec4e8e64c to your computer and use it in GitHub Desktop.

Select an option

Save Cdaprod/2ab19e7c6420d2aba45b848ec4e8e64c to your computer and use it in GitHub Desktop.
Creating a Docker container that sets up an ad-hoc network on an Ubuntu base image and hosts a web server involves several steps, focusing purely on the networking aspect without incorporating the broader features of ALOA. This guide will provide a streamlined approach to achieve your goal using Bash and networking tools within a Docker environm…

Step 1: Define the Dockerfile

You'll start by creating a Dockerfile that uses an Ubuntu base image. This image will include the necessary tools for setting up an ad-hoc network and hosting a simple web server, such as iw, iproute2, and a lightweight web server like lighttpd.

FROM ubuntu:latest

# Update packages and install necessary tools
RUN apt-get update && apt-get install -y \
    iw \
    iproute2 \
    lighttpd

# Copy the script that sets up the ad-hoc network and starts the web server
COPY setup-network.sh /usr/local/bin/setup-network.sh
COPY start-webserver.sh /usr/local/bin/start-webserver.sh

# Make the scripts executable
RUN chmod +x /usr/local/bin/setup-network.sh
RUN chmod +x /usr/local/bin/start-webserver.sh

# Expose the web server port
EXPOSE 80

# Start the setup script
CMD ["/usr/local/bin/setup-network.sh"]

Step 2: Create the Network Setup Script

The setup-network.sh script configures the ad-hoc network using iw and ip commands. This script also calls start-webserver.sh to start the web server after the network is set up.

Here's an example script to set up an ad-hoc network. Note that actual interface names (wlan0, etc.) and settings (SSID, channel) may vary based on your environment and needs.

#!/bin/bash

# Set up ad-hoc network
iw wlan0 set type ibss
ip link set wlan0 up
iw wlan0 ibss join YOUR_SSID 2412

# Assign an IP address
ip addr add 192.168.1.1/24 dev wlan0

# Start the web server
/usr/local/bin/start-webserver.sh

Step 3: Create the Web Server Start Script

The start-webserver.sh script starts the web server. This example uses lighttpd, but you can substitute it with any web server of your choice.

#!/bin/bash

# Start lighttpd
lighttpd -D -f /etc/lighttpd/lighttpd.conf

Step 4: Building and Running the Docker Container

  1. Build the Docker Image:

    docker build -t adhoc-webserver .
  2. Run the Docker Container:

    • Running the container might require network privileges to configure the network interface and broadcast the ad-hoc network.
    • You may need to run the container in privileged mode and with network host mode to allow it to manage network interfaces on the host machine.
    docker run --privileged --net=host adhoc-webserver

Security Considerations

  • Running containers in privileged mode and with --net=host can expose your host to security risks. Ensure you understand the implications and secure your environment accordingly.
  • The ad-hoc network is configured without encryption in this basic setup. Consider implementing security measures appropriate for your use case.

This guide outlines the foundational steps to create a Docker container that sets up an ad-hoc network and hosts a web server on Ubuntu. Adjust the scripts and configurations according to your specific requirements and environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment