Serving Static Files with Docker and Darkhttpd
Read it in: CatalàCastellano
In this blog post, we'll explore how to use Docker and the lightweight HTTP server, Darkhttpd, to serve static files. This setup is particularly useful when you need a simple web server for sharing files or hosting a static website. We'll also discuss how to use a reverse proxy like Traefik to route external traffic to the Darkhttpd service.
Docker Compose Configuration
Below is the docker-compose.yml file that defines the Darkhttpd service:
version: '3.3'
services:
darkhttpd:
image: p3terx/darkhttpd
container_name: darkhttpd
restart: unless-stopped
volumes:
- './site:/www:ro'
entrypoint: ["/darkhttpd","/www"]
networks:
your_network:
ipv4_address: your_ipv4_address
networks:
your_network:
external:
name: your_network_name
Here's a brief overview of the configuration:
-
The image field specifies the Docker image to use for the service.
-
The container_name field sets the name of the container.
-
The restart field configures the restart policy for the container.
-
The volumes field defines the volume mounts for the service.
-
The entrypoint field overrides the default entrypoint of the image.
-
The networks field specifies the networks that the service is connected to.
Setting Up the Service
-
Create a directory named site in the same directory as the docker-compose.yml file. Place the static files you want to serve in this directory.
-
Replace your_network, your_ipv4_address, and your_network_name in the docker-compose.yml file with the appropriate values for your setup.
-
Run the following command to start the Darkhttpd service:
docker-compose up -d
- Access the static files by navigating to the IP address specified in the docker-compose.yml file.
Using a Reverse Proxy
To route external traffic to the Darkhttpd service, you can use a reverse proxy like Traefik. Configure the reverse proxy to forward requests to the IP address specified in the docker-compose.yml file.
Conclusion
Using Docker and Darkhttpd to serve static files is a simple and efficient solution for sharing files or hosting a static website. By adding a reverse proxy, you can easily route external traffic to the Darkhttpd service. This setup is ideal for scenarios where you need a lightweight web server without the overhead of a full-fledged web server like Apache or Nginx.