Deploy a Docker Compose project
How to Deploy Docker Compose with Caddy Server, HTTPS, and a Custom Domain on a VPS
Deploying applications using Docker Compose on a VPS, combined with Caddy Server as a reverse proxy, allows you to simplify HTTPS setup and custom domain management. This guide walks you through deploying a Dockerized application using Docker Compose, securing it with HTTPS through Caddy Server, and configuring a custom domain.
Prerequisites
Before you begin, ensure you have:
- A VPS with a Linux-based operating system (e.g., Ubuntu, Debian).
- A registered domain name pointing to your VPS's IP address via DNS (using an A or CNAME record).
- Installed tools on your VPS:
- Docker and Docker Compose.
- Caddy Server installed or run as a Docker container.
- Basic knowledge of Linux terminal commands.
Step 1: Set Up Docker and Docker Compose
- Install Docker
Update your system and install Docker:
sudo apt update && sudo apt upgrade -y
sudo apt install -y docker.io
Enable and start Docker:
sudo systemctl enable docker
sudo systemctl start docker
- Install Docker Compose
Download the latest version of Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Grant executable permissions:
sudo chmod +x /usr/local/bin/docker-compose
Verify the installation:
docker-compose --version
Step 2: Prepare Your Application with Docker Compose
- Create a Project Directory
Create a directory for your Docker Compose project:
mkdir ~/my-docker-app
cd ~/my-docker-app
- Define the Docker Compose File
Create a docker-compose.yml
file in the directory. Here's an example of a simple app stack:
version: '3.8'
services:
app:
image: nginx:latest
container_name: my_app
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
This configuration uses the nginx web server to serve content.
- Add Content
Create a simple HTML file as your application content:
mkdir html
echo "<h1>Welcome to My Dockerized App!</h1>" > html/index.html
- Start Your Docker Compose App
Run the application:
docker-compose up -d
Verify that the app is running:
docker ps
Your application should now be accessible on http://<your-vps-ip>:8080
.
Step 3: Set Up Caddy Server as a Reverse Proxy
Caddy Server will serve as the HTTPS reverse proxy for your application.
Option 1: Running Caddy as a Standalone Service
- Install Caddy
Install Caddy directly on your VPS:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
Verify the installation:
caddy version
- Configure Caddy
Edit the Caddyfile configuration:
sudo nano /etc/caddy/Caddyfile
Add the following content to reverse proxy your application:
my-custom-domain.com {
reverse_proxy localhost:8080
}
Replace my-custom-domain.com
with your domain.
- Enable Caddy and Start It
sudo systemctl enable caddy
sudo systemctl start caddy
Caddy automatically provisions and renews an SSL certificate for your domain.
Option 2: Running Caddy in Docker
If you'd rather use Caddy within Docker, add it to your Docker Compose configuration.
- Update
docker-compose.yml
Extend your configuration to include the Caddy service:
version: '3.8'
services:
app:
image: nginx:latest
container_name: my_app
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
caddy:
image: caddy:latest
container_name: caddy
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
- caddy_config:/config
volumes:
caddy_data:
caddy_config:
- Create the Caddyfile
Place the Caddyfile
in the project directory:
echo "my-custom-domain.com {
reverse_proxy app:80
}" > Caddyfile
Restart Docker Compose
Rebuild and restart the stack:
docker-compose up -d
Step 4: Configure Your Custom Domain
Ensure your domain points to the VPS IP by configuring your DNS settings:
- Log in to your domain registrar's control panel.
- Add or update an A Record for your domain:
- Name:
@
- Value: Your VPS's public IP address.
- Wait for DNS propagation (may take a few minutes to 24 hours).
Step 5: Test Your Setup
-
Visit
https://my-custom-domain.com
in your browser.
You should see the content of theindex.html
file served securely over HTTPS. -
Troubleshooting:
- Logs: Use logs to identify issues:
bash docker logs caddy
- Firewall: Ensure that ports 80 and 443 are open:
bash sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw reload
Step 6: Automate and Maintain
-
Set Up Auto-Renewal
If running Caddy natively, SSL renewal is automatic. For Dockerized Caddy, restart the container periodically (e.g., via a cron job). -
Monitor Your Application
Regularly check your application's performance and logs for potential issues.