I'm a bit lost. DigitalOcean requires me to host my Docker container with Nginx as a reverse proxy. all the tutorials I've seen are unclear. I've created a docker-compose.yml file with the following services:
projectweb:
image: ${DOCKER_REGISTRY-}projectweb
build:
context: .
dockerfile: projectweb/Dockerfile
reverseproxy:
image: reverseproxy
container_name: reverseproxy
ports:
- 80:80
- 443:443
restart: always
I've created a reverse proxy container by creating a reverseproxy folder outside my website project folder. I added a Dockerfile with the following content:
FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
Then, I added an nginx.conf file to the folder and built the Docker image using:
docker build -t reverseproxy .
Next, in my website project folder, I ran:
docker-compose up -d
The output showed:
✔ Container projectweb Started 0.6s
✔ Container reverseproxy Started
Everything is working fine so far. In Docker Desktop, under the Containers tab, I can see a 'Compose stack.' How do I push this Compose stack to DigitalOcean's Container Registry or Docker Hub? I've tried:
docker tag projectweb registry.digitalocean.com/mycontainerregistry/projectweb
docker-compose push registry.digitalocean.com/mycontainerregistry/projectweb
But I get a 'no such service' error. A regular docker push works, but docker-compose push does not. Ultimately, I want to deploy my website container to DigitalOcean's App Platform with Nginx installed. How can I achieve this?
Here’s my complete setup for hosting an ASP.NET Core application in Docker with Nginx as a reverse proxy, including SSL certificate implementation:
dockerfile
EXPOSE 80 # Expose the web port
EXPOSE 443 # Expose the SSL port
# Comment out USER directive if present
# USER $APP_UID
Create a folder anywhere (e.g., reverseproxy).
Inside the reverseproxy folder, create a Dockerfile:
dockerfile
FROM nginx:alpine # or FROM nginx:latest
COPY nginx.conf /etc/nginx/nginx.conf
COPY certificate.crt /etc/ssl/certs/certificate.crt
COPY certificate.key /etc/ssl/private/certificate.key
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
upstream yourdomain {
server yourwebapp:80; # Replace with your Docker container name
}
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://yourdomain;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/ssl/certs/certificate.crt;
ssl_certificate_key /etc/ssl/private/certificate.key;
location / {
proxy_pass http://yourdomain;
proxy_redirect off;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "keep-alive";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
docker build -t reverseproxy .
docker push youraccount/yourwebapp
docker push youraccount/reverseproxy
docker-compose.yml:
yaml
services:
yourwebapp:
image: youraccount/yourwebapp
build:
context: .
dockerfile: yourwebapp/Dockerfile
reverseproxy:
image: youraccount/reverseproxy
container_name: reverseproxy
ports:
- "80:80"
- "443:443"
restart: always
docker-compose.override.yml:
yaml
services:
yourwebapp:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_HTTP_PORTS=80
ports:
- "80:80"
docker compose up -d
docker compose ps
You should see your web app and Nginx containers up and running.