dockerasp.net-corenginxdocker-compose

How do I host an ASP.NET Core in a Docker container with Nginx as a reverse proxy?


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?


Solution

  • Here’s my complete setup for hosting an ASP.NET Core application in Docker with Nginx as a reverse proxy, including SSL certificate implementation:

    1. In your project's Dockerfile, ensure the following lines are included to expose the necessary ports:

    dockerfile

    EXPOSE 80  # Expose the web port
    EXPOSE 443 # Expose the SSL port
    # Comment out USER directive if present
    # USER $APP_UID
    
    1. Create a folder anywhere (e.g., reverseproxy).

    2. 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
    
    1. Create an nginx.conf file in the reverseproxy folder:

    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;
            }
        }
    }
    
    1. Build the Docker image:
    docker build -t reverseproxy .
    
    1. Push both your web app and the reverseproxy image to Docker Hub:
    docker push youraccount/yourwebapp
    docker push youraccount/reverseproxy
    
    1. On your Linux host machine, create a folder anywhere with any name and add the following files:

    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"
    
      1. On your Linux host machine, navigate to the folder containing the docker-compose.yml and docker-compose.override.yml files and run:
    docker compose up -d
    
    1. Verify that both your web app and Nginx are running:
    docker compose ps
    

    You should see your web app and Nginx containers up and running.