In this guide, I'll walk through the process of setting up a Caddy reverse proxy using Docker and Docker Compose. Caddy is a powerful web server that comes with built-in support for automatic HTTPS, making it an excellent choice for hosting reverse proxies. Docker and Docker Compose provide a consistent environment for containerizing and deploying applications.
Prerequisites
Before we begin, you'll need to have Docker and Docker Compose installed on your system. You can download and install Docker from the official website: Docker. Docker Compose is included with Docker Desktop for Windows and macOS, but you may need to install it separately on Linux. You can find installation instructions for Docker Compose here: Docker Compose
Step 1: Create a Dockerfile
First, we need to create a Dockerfile that will define the environment for our Caddy reverse proxy. Create a new file named Dockerfile
in a directory of your choice and add the following content:
FROM caddy:latest
COPY Caddyfile /etc/caddy/Caddyfile
This Dockerfile uses the official Caddy Docker image as its base and copies a Caddyfile
into the container. The Caddyfile
is a configuration file that defines how Caddy should handle incoming requests.
Step 2: Create a Caddyfile
Next, we need to create a Caddyfile
that specifies the reverse proxy configuration. Create a new file named Caddyfile
in the same directory as the Dockerfile
and add the following content:
example.com {
reverse_proxy /app1/* localhost:8080
reverse_proxy /app2/* localhost:8081
tls {
dns cloudflare
}
}
In this example, we're configuring Caddy to forward requests for /app1/*
to localhost:8080
and requests for /app2/*
to localhost:8081
. We're also enabling automatic HTTPS using Let's Encrypt with the Cloudflare DNS provider. You'll need to replace example.com
with your own domain name and configure additional reverse proxy rules as needed.
Step 3: Create a docker-compose.yml File
Next, we'll create a docker-compose.yml
file that will define the services for our Caddy reverse proxy and any backend servers. Create a new file named docker-compose.yml
in the same directory as the Dockerfile
and add the following content:
version: '3'
services:
caddy:
build: .
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
networks:
- web
depends_on:
- app1
- app2
app1:
image: nginx:latest
networks:
- web
app2:
image: nginx:latest
networks:
- web
networks:
web:
driver: bridge
In this docker-compose.yml
file, we define three services: caddy
, app1
, and app2
. The caddy
service uses the Dockerfile
we created earlier and exposes ports 80
and 443
. It also mounts the Caddyfile
into the container. The app1
and app2
services use the official nginx Docker image as their base and are included here as placeholders for your backend servers. You can replace these with your own backend servers as needed.
Step 4: Build and Run the Docker Containers
Now that we have our Dockerfile
, Caddyfile
, and docker-compose.yml
file, we can build and run the Docker containers. Open a terminal or command prompt and navigate to the directory where the Dockerfile
and docker-compose.yml
file are located. Run the following command to build and run the Docker containers:
docker-compose up -d
This command will build the Docker image using the Dockerfile
, create the Docker containers based on the services defined in the docker-compose.yml
file, and start the containers in detached mode.
Step 5: Verify the Setup
Finally, we can verify that the Caddy reverse proxy is working correctly. Open a web browser and navigate to https://example.com/app1/
(replace example.com
with your own domain name). You should see the content served by the backend server running on localhost:8080
. Similarly, navigating to https://example.com/app2/
should display the content served by the backend server running on localhost:8081
.
Conclusion
In this guide, we've covered the steps required to host a Caddy reverse proxy using Docker and Docker Compose. By following these steps, you can easily set up a reverse proxy to forward requests to multiple backend servers based on various criteria. This makes Caddy an excellent choice for hosting reverse proxies in Docker containers.