How to Install Caddy with Docker

Caddy is the best and simplest web server like that of Nginx and Apache. I’ve tried both of the latter and to say it’s the simplest is no joke. I just add my domain to its Caddyfile and where it’ll reverse proxy onto and tada, it’s done. It even takes care of SSL certificate automatically so you don’t have to run the certbot yourself!

Wonderful, yes, but don’t forget to remove the existing SSL certificate with sudo certbot delete (or manually add the path with Caddyfile’s TLS). Otherwise, you’ll be spamming ACME for certification and you’ll end up having to wait a day to get it again. I did this twice. The first time I didn’t know why my site was still on HTTP and the second time, because Caddy couldn’t access Apache’s SSL certificate.

Tested on
Expand this and run it in a terminal for those who are lazy and just want to get it done in one command.

This command will put the files where the terminal is run. It’ll not make a caddy folder and put them inside of there.

One-line Caddy Docker install command
curl -L s.revonzev.com/install-caddy-docker-curl | bash
Bash

Then open hello-world.localhost in your browser.

A guide to installing Docker in Linux Mint is here.

Anyway, here’s my docker-compose for Caddy:

docker-compose.yml
services:
  caddy:
    image: caddy:latest
    container_name: caddy
    restart: unless-stopped
    security_opt:
      - label:disable
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - ./container_config:/etc/caddy
      - ./site:/srv
      - ./caddy_data:/data
      - ./caddy_config:/config
      - ./caddy_log:/var/log/caddy
      - /etc/localtime:/etc/localtime:ro
    networks:
      - reverseproxy-nw


networks:
  reverseproxy-nw:
    external: true
YAML

Don’t forget to add Caddy’s Caddyfile. Create a new folder named container_config and make a new file named Caddyfile inside of it.

Caddyfile
http://hello-world.localhost {
  respond "Hello World!"
}
Go

You can change the http://hello-world.localhost to your own domain. Caddy will not turn HTTP into HTTPS for sites with http://... e.g. http://example.com. Caddy will, however, automatically turn a site like example.com (without the http://) into HTTPS.

One last step to do is to add the reverseproxy-nw Docker network by running this in the terminal:

Bash
docker network create "reverseproxy-nw"
Bash

This is done so Caddy can access other docker containers that are connected to reverseproxy-nw Docker network.

An example of connecting to a container with it.
Go
http://s.localhost {
  reverse_proxy chhoto-url:4567
}
Go

chhoto-url is the Docker container’s name, while 4567 is its port

Now run this command to start the docker container:

Bash
docker compose up -d
Bash

Go to hello-world.localhost in your browser to see if it is working or not. Mine certainly did.

The tutorial I find most helpful in installing caddy was s.revonzev.com/gurucomputing-caddy-tutorial


Posted

in

by


Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments