skip to Main Content

I’m having trouble deploying a simple Flask app behind a Nginx reverse proxy. The app is the one found at https://docs.docker.com/compose/gettingstarted/

I’m trying to make the app show up at subdomain.example.com/flask but with the current configuration it doesn’t work. What i’m missing?

Now my docker-compose.yml looks like this:

version: '3'
services:
  nginx:
    container_name: nginx
    build:
      context: ./nginx
    volumes:
      - ./config/:/etc/nginx/
      - ./ssl-cert/:/etc/ssl/private/
    depends_on:
      - web
    ports:
      - 80:80

  web:
    build:
      context: ./composetest
    expose:
      - 5000

  redis:
    image: "redis:alpine"

And here’s my Nginx’s default.conf:

server {
    listen              80;
    server_name         subdomain.example.com;

    location /flask{
        proxy_pass http://web:5000;
    }

}

When i do docker-compose up --build i don’t get any errors from the Nginx container nor the others (ignoring the production-ready related errors obviously).

Here’s what i see when i run the above mentioned command:

redis_1       | 1:C 16 May 2021 14:08:42.327 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1       | 1:C 16 May 2021 14:08:42.335 # Redis version=6.2.3, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1       | 1:C 16 May 2021 14:08:42.335 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1       | 1:M 16 May 2021 14:08:42.336 * monotonic clock: POSIX clock_gettime
redis_1       | 1:M 16 May 2021 14:08:42.337 * Running mode=standalone, port=6379.
redis_1       | 1:M 16 May 2021 14:08:42.337 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1       | 1:M 16 May 2021 14:08:42.337 # Server initialized
redis_1       | 1:M 16 May 2021 14:08:42.337 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1       | 1:M 16 May 2021 14:08:42.338 * Ready to accept connections
web_1         |  * Serving Flask app 'app.py' (lazy loading)
web_1         |  * Environment: production
web_1         |    WARNING: This is a development server. Do not use it in a production deployment.
web_1         |    Use a production WSGI server instead.
web_1         |  * Debug mode: off
web_1         |  * Running on all addresses.
web_1         |    WARNING: This is a development server. Do not use it in a production deployment.
web_1         |  * Running on http://192.168.112.4:5000/ (Press CTRL+C to quit)

2

Answers


  1. You need to have a ports directive for nginx’s service. Even if ports are exposed by an image by default, those do not automatically get bound to the host as in ports.

    services:
      nginx:
        container_name: nginx
        build:
          context: ./nginx
        volumes:
          - ./config/:/etc/nginx/
          - ./ssl-cert/:/etc/ssl/private/
        depends_on:
          - web
        ports:
          - "80:80"
    
    

    After that, you should be able to access nginx at http://localhost

    Login or Signup to reply.
  2. I think everything is fine you exposing port 5000 from your web container and you listing on port 80 for the nginx container

    you should may foreword the port 80 to your local machine.

    just by adding under depends_on: rebuild test and it should works fine

    ports:
          - "80:80"
    

    Add the subdomain: subdomain.example.com also on your local machine under /etc/hosts,
    if your using ubuntu debian machine. by adding the host using any text editor vim nano

    127.0.0.1 subdomain.example.com

    Save and Serve from your browser.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search