Install Caddy Inside Docker Container in Linux

Install Caddy Inside Docker Container in Linux

Caddy is a cross-platform web server that serves web content to clients. Caddy is an open-source project that written in Go programming language and available under the Apache License 2.0.

This tutorial explains how to install Caddy inside a Docker container in the Linux. Commands have been tested on Ubuntu.

Prepare environment

Make sure you have installed Docker in your system. If you are using Ubuntu, installation instructions can be found in the post.

Install Caddy

  • Host network

Run the following command to create a container for Caddy that uses host network:

docker run -d --name=caddy --restart=always --network=host \
    -v /var/www:/usr/share/caddy \
    -v /opt/caddy/data:/data \
    -v /opt/caddy/config:/config \
    caddy
  • User-defined bridge network

User-defined bridge network can be used for listening on different port. By default, Caddy service is listening on port 80. It can be changed with -p option.

docker network create app-net
docker run -d --name=caddy --restart=always --network=app-net \
    -p 8080:80 \
    -v /var/www:/usr/share/caddy \
    -v /opt/caddy/data:/data \
    -v /opt/caddy/config:/config \
    caddy

Testing Caddy

Create simple index.html for testing:

echo '<!DOCTYPE html><html><head><title>Title</title></head><body>Hello world</body></html>' | sudo tee /var/www/index.html

Open a web browser and go to http://<IP_ADDRESS>, where <IP_ADDRESS> is the IP address of the system. You will see a recently created page.

Caddy Inside Docker Container in Linux

Uninstall Caddy

To completely remove the Caddy, remove its container:

docker rm --force caddy

Remove Caddy image:

docker rmi caddy

You can also remove Caddy web data and related directories:

sudo rm -rf /var/www
sudo rm -rf /opt/caddy

If a user-defined bridge network was created, you can delete it as follows:

docker network rm app-net

Leave a Comment

Cancel reply

Your email address will not be published.