0

I'm using jwilder/nginx-proxy with separate docker-compose.yaml. It looks like this:

proxy:
   image: jwilder/nginx-proxy
   restart: always
   volumes:
     - /var/run/docker.sock:/tmp/docker.sock:ro
     - ./nginx/conf.d/proxy.conf:/etc/nginx/conf.d/proxy.conf:ro
     - /Users/marcin/Docker/local_share/certificates:/etc/nginx/certs:ro      
   ports:
     - "80:80"
     - "443:443"
   container_name: proxy

I'm using it for quite a long time and it's working fine when my project docker-compose.yaml looks like this:

web:
  build: /Users/marcin/Docker/definitions/php-nginx/php-7.1-ubuntu
  volumes:
    - /Users/marcin/Docker/projects/test.local/html/:/usr/share/nginx/html/
    - /Users/marcin/Docker/projects/test.local/nginx/conf.d/:/etc/nginx/conf.d/
    - /Users/marcin/Docker/projects/test.local/nginx/log/:/var/log/nginx/
    - /Users/marcin/Docker/projects/test.local/supervisor/conf.d/:/etc/supervisor/conf.d/
    - /Users/marcin/Docker/projects/test.local/supervisor/log/:/var/log/supervisor/
    - /Users/marcin/Docker/projects/test.local/cron/:/root/.cron/
    - /Users/marcin/Docker/local_share/:/root/.local_share/
    - /Users/marcin/Docker/local_share/certificates/:/usr/share/nginx/certificates/  
  working_dir: /usr/share/nginx/html/
  links:
    - db
  container_name: test.php
  hostname: test.local
  ports:
    - "336:22"
    - "8081:80"
    - "18080:443"    
  environment:
    - VIRTUAL_HOST=test.local   
    - CERT_NAME=default
    - HTTPS_METHOD=noredirect
db:
  build: /Users/marcin/Docker/definitions/mysql/5.7
  environment:
     - MYSQL_ROOT_PASSWORD=pass
     - MYSQL_DATABASE=
     - MYSQL_USER=
     - MYSQL_PASSWORD=
  expose:
     - 3306
  volumes:
    - /Users/marcin/Docker/projects/test.local/mysql/data/:/var/lib/mysql/
    - /Users/marcin/Docker/projects/test.local/mysql/conf.d/:/etc/mysql/conf.d/source
    - /Users/marcin/Docker/projects/test.local/mysql/log/:/var/log/mysql/
  ports:
    - "33060:3306"
  container_name: test.db
  hostname: test.local

I can access site without any problem using http://test.local or https://test.local what is expected.

However I had to update my file structure to newer version:

version: "3.2"
services:
  web:
    build: /Users/marcin/Docker/definitions/php-nginx/php-7.1-ubuntu
    volumes:
      - /Users/marcin/Docker/projects/test.local/html/:/usr/share/nginx/html/
      - /Users/marcin/Docker/projects/test.local/nginx/conf.d/:/etc/nginx/conf.d/
      - /Users/marcin/Docker/projects/test.local/nginx/log/:/var/log/nginx/
      - /Users/marcin/Docker/projects/test.local/supervisor/conf.d/:/etc/supervisor/conf.d/
      - /Users/marcin/Docker/projects/test.local/supervisor/log/:/var/log/supervisor/
      - /Users/marcin/Docker/projects/test.local/cron/:/root/.cron/
      - /Users/marcin/Docker/local_share/:/root/.local_share/
      - /Users/marcin/Docker/local_share/certificates/:/usr/share/nginx/certificates/  
    working_dir: /usr/share/nginx/html/
    links:
      - db
    container_name: test.php
    hostname: test.local
    ports:
      - "336:22"
      - "8081:80"
      - "18080:443"    
    environment:
      - VIRTUAL_HOST=test.local   
      - CERT_NAME=default
      - HTTPS_METHOD=noredirect
  db:
    build: /Users/marcin/Docker/definitions/mysql/5.7
    environment:
       - MYSQL_ROOT_PASSWORD=pass
       - MYSQL_DATABASE=
       - MYSQL_USER=
       - MYSQL_PASSWORD=
    expose:
       - 3306
    volumes:
      - /Users/marcin/Docker/projects/test.local/mysql/data/:/var/lib/mysql/
      - /Users/marcin/Docker/projects/test.local/mysql/conf.d/:/etc/mysql/conf.d/source
      - /Users/marcin/Docker/projects/test.local/mysql/log/:/var/log/mysql/
    ports:
      - "33060:3306"
    container_name: test.db
    hostname: test.local

and after that it seems not to work. I can access site using ip and port without a problem, but I cannot longer use domain to access it. When I try I'm getting:

503 Service Temporarily Unavailable

nginx/1.13.8

And this is for sure from jwilder nginx (and not the nginx in project).

So the question is - where should I put environment variables to make it work? It seems that when they are placed as they are at the moment they are not read by proxy.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291

1 Answers1

4

The 503 indicates that the nginx-proxy container can see your container running in docker and it has the configuration needed for nginx to route traffic to it, but it is unable to connect to that container over the docker network. For container-to-container networking to work, you need to have a common docker network defined. You should first run the following to create a network:

docker network create proxy

Then update your nginx-proxy compose file to use the network (this should also be upgraded to at least a v2 syntax, I've gone with 3.2 to match your other file):

version: "3.2"

networks:
  proxy:
    external: true

services:
  proxy:
    image: jwilder/nginx-proxy
    restart: always
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx/conf.d/proxy.conf:/etc/nginx/conf.d/proxy.conf:ro
      - /Users/marcin/Docker/local_share/certificates:/etc/nginx/certs:ro      
    ports:
      - "80:80"
      - "443:443"
    container_name: proxy
    networks:
      - proxy

And then do something similar for your application:

version: "3.2"
networks:
  proxy:
    external: true
services:
  web:
    build: /Users/marcin/Docker/definitions/php-nginx/php-7.1-ubuntu
    volumes:
      - /Users/marcin/Docker/projects/test.local/html/:/usr/share/nginx/html/
      - /Users/marcin/Docker/projects/test.local/nginx/conf.d/:/etc/nginx/conf.d/
      - /Users/marcin/Docker/projects/test.local/nginx/log/:/var/log/nginx/
      - /Users/marcin/Docker/projects/test.local/supervisor/conf.d/:/etc/supervisor/conf.d/
      - /Users/marcin/Docker/projects/test.local/supervisor/log/:/var/log/supervisor/
      - /Users/marcin/Docker/projects/test.local/cron/:/root/.cron/
      - /Users/marcin/Docker/local_share/:/root/.local_share/
      - /Users/marcin/Docker/local_share/certificates/:/usr/share/nginx/certificates/  
    working_dir: /usr/share/nginx/html/
    links:
      - db
    container_name: test.php
    hostname: test.local
    ports:
      - "336:22"
      - "8081:80"
      - "18080:443"    
    environment:
      - VIRTUAL_HOST=test.local   
      - CERT_NAME=default
      - HTTPS_METHOD=noredirect
    networks:
      - proxy
      - default
  db:
    build: /Users/marcin/Docker/definitions/mysql/5.7
    environment:
       - MYSQL_ROOT_PASSWORD=pass
       - MYSQL_DATABASE=
       - MYSQL_USER=
       - MYSQL_PASSWORD=
    expose:
       - 3306
    volumes:
      - /Users/marcin/Docker/projects/test.local/mysql/data/:/var/lib/mysql/
      - /Users/marcin/Docker/projects/test.local/mysql/conf.d/:/etc/mysql/conf.d/source
      - /Users/marcin/Docker/projects/test.local/mysql/log/:/var/log/mysql/
    ports:
      - "33060:3306"
    container_name: test.db
    hostname: test.local

If you were upgrading from a v1 syntax (without a version defined), you will find that docker switches from running everything on the same network without dns to running each compose project or stack on a dedicated network with dns. To run your apps on other networks, you'll need to explicitly configure that. In the above example, only the web container was placed on the proxy network, and both are on the default network created for this project or stack.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • Thanks, this is what I found just a moment before your answer but your answer is totally correct and might also help others – Marcin Nabiałek Feb 09 '18 at 12:55
  • This was also my problem - I had created a new network without realizing it, and as soon as I made jWilder join that network everything started resolving! – Raven Dreamer Jan 17 '19 at 15:38
  • Could you please help, I got a similar problem and haven't been able to fix it. [Here is my question](https://stackoverflow.com/q/65667557/1883256). – Pathros Jan 11 '21 at 18:51