0

i have several flask applications which i want to run on a server as separate docker containers. on the server i already have several websites running with a reverse proxy and the letsencrypt-nginx-proxy-companion. unfortunately i can't get the containers to run. I think it is because of the port mapping. When I start the containers on port 80, I get the following error message "[ERROR] Can't connect to ('', 80)" from gunicorn. On all other ports it starts successfully, but then I can't access it from outside.

what am I doing wrong?

docker-compose.yml

version: '3'

services:
  db:
    image: "mysql/mysql-server:5.7"
    env_file: .env-mysql
    restart: always

  app:
    build: .
    env_file: .env
    expose:
      - "8001"
    environment:
      - VIRTUAL_HOST:example.com
      - VIRTUAL_PORT:'8001'
      - LETSENCRYPT_HOST:example.com
      - LETSENCRYPT_EMAIL:foo@example.com
    links:
      - db:dbserver
    restart: always

networks:
  default:
    external:
      name: nginx-proxy

Dockerfile

FROM python:3.6-alpine

ARG CONTAINER_USER='flask-user'

ENV FLASK_APP run.py
ENV FLASK_CONFIG docker

RUN adduser -D ${CONTAINER_USER}
USER ${CONTAINER_USER}

WORKDIR /home/${CONTAINER_USER}

COPY requirements requirements
RUN python -m venv venv
RUN venv/bin/pip install -r requirements/docker.txt

COPY app app
COPY migrations migrations
COPY run.py config.py entrypoint.sh ./

# runtime configuration
EXPOSE 8001
ENTRYPOINT ["./entrypoint.sh"]

entrypoint.sh

#!/bin/sh
source venv/bin/activate
flask deploy
exec gunicorn -b :8001 --access-logfile - --error-logfile - run:app

reverse-proxy/docker-compose.yml

version: '3'
services:
  nginx:
    image: nginx
    labels:
      com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: "true"
    container_name: nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /srv/www/nginx-proxy/conf.d:/etc/nginx/conf.d
      - /srv/www/nginx-proxy/vhost.d:/etc/nginx/vhost.d
      - /srv/www/nginx-proxy/html:/usr/share/nginx/html
      - /srv/www/nginx-proxy/certs:/etc/nginx/certs:ro

  nginx-gen:
    image: jwilder/docker-gen
    command: -notify-sighup nginx -watch -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
    container_name: nginx-gen
    restart: always
    volumes:
      - /srv/www/nginx-proxy/conf.d:/etc/nginx/conf.d
      - /srv/www/nginx-proxy/vhost.d:/etc/nginx/vhost.d
      - /srv/www/nginx-proxy/html:/usr/share/nginx/html
      - /srv/www/nginx-proxy/certs:/etc/nginx/certs:ro
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - /srv/www/nginx-proxy/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro

  nginx-letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: nginx-letsencrypt
    restart: always
    volumes:
      - /srv/www/nginx-proxy/conf.d:/etc/nginx/conf.d
      - /srv/www/nginx-proxy/vhost.d:/etc/nginx/vhost.d
      - /srv/www/nginx-proxy/html:/usr/share/nginx/html
      - /srv/www/nginx-proxy/certs:/etc/nginx/certs:rw
      - /var/run/docker.sock:/var/run/docker.sock:ro
    environment:
      NGINX_DOCKER_GEN_CONTAINER: "nginx-gen"
      NGINX_PROXY_CONTAINER: "nginx"
      DEBUG: "true"

networks:
  default:
    external:
      name: nginx-proxy
  • I believe other service is running on port 80 of your host. That's why you can't map. You can use lsof command to check what is running on port 80. sudo lsof -i -P -n | grep LISTEN – Htin Aung May 30 '20 at 07:46
  • docker is listening on port 80 on my host: docker-pr 9767 root 4u IPv6 43337957 0t0 TCP *:80 (LISTEN) I thought that I can use every port inside my docker and Nginx will map this via virtual_port? I have two other containers running, both are showing me 80/tcp when I run 'docker ps' –  May 30 '20 at 07:57
  • You host's port 80 is taking by nginx container according to reverse-proxy/docker-compose.yml. So, you can't run another application on host's port 80 again. You container are running and expose container's port 80. You can map container's port 80 to other host's ports (eg. 8080). – Htin Aung May 30 '20 at 08:36
  • how do my other containers do that? at 'httpd' i don't expose any ports. also my nextcloud runs without releasing a port. but all containers tell me when I run docker ps that they listen on port 80. –  May 30 '20 at 08:52
  • Your objective is to run multiple containers on server, right? Cannot run multiple services on same port. I think your port 80 containers are Nginx images. Run Python image on other port. So, you have 2 containers running on same server already. – Htin Aung May 30 '20 at 09:13
  • i currently have httpd and nextcloud containers running. these containers all run on port 80 (docker ps shows 80/tcp for each container). in the docker-compose.yml of the httpd container i set expose: - 80 and VIRTUAL_PORT=80 and on the nextcloud container only VIRTUAL_PORT=80. is it possible that i don't need the expose and docker does it over the virtual port? or do i have to change something in my network? –  May 30 '20 at 11:04
  • i completely agree with you that i can't use port 80 multiple times, but if i use a different port, i can't access it. i'm a bit confused that i already have two different containers running on port 80. i thought that each container is in an different network and therefore i can use port 80 more than once and my reverse proxy connects to these networks. –  May 30 '20 at 11:08

0 Answers0