PinPiguin
PinPiguin

Reputation: 477

Is it possible to share ports between NGINX and Docker Container?

I have Ubuntu 18:04/NGINX VPS where I have a bunch of Laravel project blocks, all use ssl (certbot).

I wanted to deploy Nextcloud via Docker Compose on the same VPS:

version: "3"
services:


proxy:
    image: jwilder/nginx-proxy:alpine
    labels:
    # labels needed by lets encrypt to identify container to generate certs in
    - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true"
    container_name: nextcloud-proxy
    networks:
    - nextcloud_network
    ports:
    - 80:80
    - 443:443
    volumes:
    - ./proxy/conf.d:/etc/nginx/conf.d:rw
    - ./proxy/vhost.d:/etc/nginx/vhost.d:rw
    - ./proxy/html:/usr/share/nginx/html:rw
    - ./proxy/certs:/etc/nginx/certs:ro
    - /etc/localtime:/etc/localtime:ro
    - /var/run/docker.sock:/tmp/docker.sock:ro
    restart: unless-stopped


letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion:v1.12.1
    container_name: nextcloud-letsencrypt
    depends_on:
        - proxy
    networks:
        - nextcloud_network
    volumes:
        - ./proxy/certs:/etc/nginx/certs:rw
        - ./proxy/vhost.d:/etc/nginx/vhost.d:rw
        - ./proxy/html:/usr/share/nginx/html:rw
        - /etc/localtime:/etc/localtime:ro
        - /var/run/docker.sock:/var/run/docker.sock:ro
    restart: unless-stopped


db:
    image: mariadb:10.5.1
    container_name: nextcloud-mariadb
    networks:
        - nextcloud_network
    volumes:
        - ./db:/var/lib/mysql
        - ./dbdumps:/var/dbdumps
        - /etc/localtime:/etc/localtime:ro
    environment:
        - MYSQL_ROOT_PASSWORD=...   # set me
        - MYSQL_PASSWORD=...        # set me
        - MYSQL_DATABASE=...        # set me
        - MYSQL_USER=...            # set me
    restart: unless-stopped


redis:
    container_name: nextcloud-redis
    image: redis:5.0.8
    restart: unless-stopped
    networks:
        - nextcloud_network
    volumes:
    - ./redis/data:/data
    command: ["redis-server", "--appendonly yes"]               


app:
    image: nextcloud:18.0.2
    container_name: nextcloud-app
    networks:
        - nextcloud_network
    depends_on:
        - letsencrypt
        - proxy
        - redis
        - db
    volumes:
        - ./nextcloud:/var/www/html
        - ./app/config:/var/www/html/config
        - ./app/custom_apps:/var/www/html/custom_apps
        - ./app/data:/var/www/html/data
        - ./app/themes:/var/www/html/themes
        - /etc/localtime:/etc/localtime:ro
    environment:
        - VIRTUAL_HOST=YOURDOMAINHERE           # set me
        - LETSENCRYPT_HOST=YOURDOMAINHERE       # set me
        - [email protected]     # set me
    restart: unless-stopped        


networks:


nextcloud_network:
    driver: bridge

When I run this I get:

ERROR: for 3f210d699b80_nextcloud-proxy  Cannot start service proxy: driver failed programming 
external connectivity on endpoint nextcloud-proxy 
(2d76e425c94abb95da70a7d903bf8830d4e9192a512e17db1b39f76da85c7b97): Error starting userland proxy: 
listen tcp 0.0.0.0:443: bind: address already in use

ERROR: for proxy  Cannot start service proxy: driver failed programming external connectivity on 
endpoint nextcloud-proxy (2d76e425c94abb95da70a7d903bf8830d4e9192a512e17db1b39f76da85c7b97): Error 
starting userland proxy: listen tcp 0.0.0.0:443: bind: address already in use
ERROR: Encountered errors while bringing up the project.

Because this port is already in use.

If I stop NGINX on VPS and run docker-compose up -d again, everything is ok and Nextcloud service is accessible via URL.

I tried to change outside ports to

- 8080:80
- 4444:443

And rebuild it. Then I don't see the above error but everything is messed up - the url point to wrong domain...

Is it possible to tweak the proxy container settings somehow to resolve this?

Upvotes: 0

Views: 504

Answers (1)

sktan
sktan

Reputation: 1259

2 services are unable to listen to the same port as you have found. Your laravel applications are already listening on ports 80/443, so when start your nextcloud containers, it won't be able to bind to those ports.

You'll have to have your jwilder/nginx-proxy:alpine act as a proxy to both the nextcloud container and the laravel servers. This can be done via your nginx configurations and mount it to your container (which you seem to be using the ./proxy/ directory):

https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

Although, if your VPS is able to have 2 IP addresses, then you are able to bind the laravel applications to one interface and your nextcloud proxy to the other which will also solve your problem. The first method is better practice as would allow you to scale your server better without having to add another IP address per-application.

https://docs.docker.com/config/containers/container-networking/

Upvotes: 1

Related Questions