Serve Static Website With Caddy Using Docker
Serving static websites from Caddy webserver running inside Docker containers
The default caddy config serves files from /usr/share/caddy we can create a docker image by using caddy base image then copying our static file over.
Let’s say our static files are under /public folder. Then we can write our Dockerfile similar to this.
FROM caddy:latest
COPY /public /usr/share/caddy
EXPOSE 80
From the Dockerfile we can build our new image.
docker build -t static-website .
After building our new image we can create a container using static-website image.
Note: caddy requires to have persistent data at /data mentioned in docker hub page , so we are adding a named volume caddy_data to the container.
docker run -d -p 80:80 -v caddy_data:/data static-website
Now the website should be accessible from http://localhost
Using Caddyfile
As mentioned earlier the caddy uses a default configuration to run the container. To use your custom configuration for the caddy.
Write your configuration in Caddyfile. In the file I’ve set the root of our static files to be /srv
:80
root * /srv
file_server
Then we can then update the Dockerfile according to our configuration
FROM caddy:latest
# Replacing default Caddyfile from ours
COPY Caddyfile /etc/caddy/Caddyfile
# In our configuration we are serving static files from /srv
# so copy static file to /srv
COPY /public /srv
EXPOSE 80
After these changes build the image and create a container from the image to serve your static website.