Running mutliple Nginx servers inside Docker containers

  docker, docker-compose, nginx

I’m trying to run Nginx servers inside Docker containers that are on the same host.

My nginx configuration is the following

events{}
http {
    include /etc/nginx/mime.types;
    server {
        listen 4200;
        server_name 0.0.0.0;
        listen [::]:4200;
        client_max_body_size 1024M;
        root /usr/share/nginx/html;
        index index.html;
        location / {
             proxy_set_header 'Access-Control-Allow-Origin' '*';
             add_header Allow 'GET, POST, PUT, PATCH, DELETE' always;
             add_header 'Access-Control-Allow-Origin' '*' always;
             add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, PATCH, DELETE' always;
             add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
             add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
             add_header 'Access-Control-Allow-Credentials' 'true' always;
             try_files $uri $uri/ /index.html ;
        }
  location /api/ {

             proxy_pass http://xxxxxxxx/;


  }
    location /secondapi/ {

             proxy_pass http://yyyyyyyyy/;

  }
 
    }



   server {
    # port to listen on. Can also be set to an IP:PORT
    listen 443 ssl http2;

    access_log  /var/log/nginx/host.access.log;
    error_log /var/log/nginx/host.error.log ;


        include /etc/nginx/letsencrypt/options-ssl-nginx.conf;
        ssl_dhparam /etc/nginx/letsencrypt/ssl-dhparams.pem;

        ssl_certificate /etc/nginx/letsencrypt/archive/xxxxxx.com/fullchain1.pem;
        ssl_certificate_key /etc/nginx/letsencrypt/archive/xxxxxx.com/privkey1.pem;

        location /  {
        proxy_pass http://127.0.0.1:4200/ ;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
          }

}

server {
    if ($host = xxxxxxxxxxxx ) {
        return 301 https://$host$request_uri;
    } 


    listen      8989;
    server_name  xxxxxxxxxxx;
    return 404;     

}

}

This is my Dockerfile

FROM base AS builder

LABEL stage=builder

WORKDIR /app

ARG data_path

COPY ${data_path} /app/src/app/

RUN npm install && npm run build -- --prod --aot=true --build-optimizer=true

EXPOSE 4200 443

FROM nginx:alpine

COPY ./nginx.conf  /etc/nginx

WORKDIR /etc/nginx

COPY ./letsencrypt letsencrypt/

RUN service nginx restart &

COPY --from=builder /app/dist/* /usr/share/nginx/html/

And my docker-compose file

version: '3'
services:
  app:
    image: aaaaaaaaaa
    labels:
    - com.centurylinklabs.watchtower.enable=true
    ports:
    - 10720:4200
    - 443:443

So it’s working fine for the first container. If I try to run another one, with a different domain name in the Nginx configuration, it returns the following error:

ERROR: for aa7620ba-76af-11eb-bc19-0a804617efdb_app_1  Cannot start service app: driver failed programming external connectivity on endpoint aa7620ba-76af-11eb-bc19-0a804617efdb_app_1 (a4b7ae0af88df6b83a8d2f68b65b0eb56ed8fadb8030df616887104773343a8d): Bind for 0.0.0.0:443 failed: port is already allocated

ERROR: for app  Cannot start service app: driver failed programming external connectivity on endpoint aa7620ba-76af-11eb-bc19-0a804617efdb_app_1 (a4b7ae0af88df6b83a8d2f68b65b0eb56ed8fadb8030df616887104773343a8d): Bind for 0.0.0.0:443 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.

Is there a way to overcome this? Besides running Nginx on the host.

Source: Docker Questions

LEAVE A COMMENT