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"]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.shThe 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-
Build the Docker Image:
docker build -t adhoc-webserver . -
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
- Running containers in privileged mode and with
--net=hostcan 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.