Docker Community Forums

Share and learn in the Docker community.

Add second container to nginx-proxy

I’m running jwilder/nginx-proxy:alpine for my nextcloud instance. My docker ps -a looks like this:

287338265e0d nextcloud:latest “/entrypoint.sh apac…” 5 months ago Up 19 hours 80/tcp nextcloud-app
20c4f073ba80 jrcs/letsencrypt-nginx-proxy-companion “/bin/bash /app/entr…” 5 months ago Up 19 hours nextcloud-letsencrypt
1a42309f874c jwilder/nginx-proxy:alpine “/app/docker-entrypo…” 5 months ago Up 19 hours 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nextcloud-proxy
65fcd75eedaf mariadb “docker-entrypoint.s…” 5 months ago Up 19 hours 3306/tcp nextcloud-mariadb

Now i want to create a new docker container which needs the port 443 for https connection and also a certificate from my nextcloud-letsencrypt. What is the best way to realize this?

Here is the Dockerfile which will be used to create the reverse proxy image. It will use the nginx.conf after copying it to the proxy container:

FROM nginx:alpine

COPY nginx.conf /etc/nginx/nginx.conf

We’re using nginx 1.19.3. To check the version, we can add the following to the Dockerfile because the alpine docker image doesn’t have bash installed by default:

RUN apk update && apk add bash

Then, check its version:

docker build -t nginx-alpine . docker run -t -i nginx-alpine /bin/bash
bash-4.4# nginx -v
nginx version: nginx/1.19.3

Once it’s done, we may want to remove the line we’ve just added since it will increase the size of the image.

Let’s build reverse proxy image:

$ docker build -t reverseproxy .
Sending build context to Docker daemon 64.51kB
Step 1/3 : FROM nginx:alpine
—> 4efb29ff172a
Step 2/3 : COPY nginx.conf /etc/nginx/nginx.conf
—> Using cache
—> 845ad907e486
Step 3/3 : RUN apk update && apk add bash
—> Using cache
—> 788046b5f293
Successfully built 788046b5f293
Successfully tagged reverseproxy:latest

We can check the create image:

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
reverseproxy latest 788046b5f293 3 minutes ago 25.1MB

Note that we copied the following nginx.conf to our reverse proxy container:

nginx.conf:

worker_processes 1;

events { worker_connections 1024; }

http {

sendfile on;

upstream docker-nginx {
    server nginx:80;
}

upstream docker-apache {
    server apache:80;
}

server {
    listen 8080;

    location / {
        proxy_pass         http://docker-nginx;
        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 8081;

    location / {
        proxy_pass         http://docker-apache;
        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;
    }
}

}

We set worker_processes explicitly to 1 which is the default value. It is common practice to run 1 worker process per core. For more about it, check Thread Pools in NGINX Boost Performance 9x!.

1 Like