1

I'm sure this question has been answered before, but I cannot find a simple solution anywhere. I have multiple docker-compose projects running on a single host, with unique host port mappings.

I'd like to access a Flask API from outside the server, but cannot work out how to do this. There seems to be an option when running docker-compose run -p, but I was of the understanding that docker-compose up -d is the preferred and robust way to run compose.

I tried changing network_mode: "bridge" to network_mode: "host", but this didn't help.

Let's say the IP of the host is 123.23.23.111, I would like to access the api service externally via something like http://123.23.23.111:5004, as well as the Flower monitor via http://123.23.23.111:5559.

It's all running on a private network, and I would like another server on the private network to access the API, but no internet access is needed. https will be added later.

version: '3.7'
services:
  api:
    build:
      context: ./
      dockerfile: ./api/Dockerfile
    restart: always
    ports:
     - "5004:5000"
    network_mode: "host"
    depends_on:
      - redis
  worker:
    user: nobody
    build:
      context: ./
      dockerfile: ./worker/Dockerfile
    depends_on:
      - redis
  monitor:
    build:
      context: ./
      dockerfile: ./monitor/Dockerfile
    ports:
     - "5559:5555"
    network_mode: "host"
    entrypoint: flower
    command: --port=5555 --broker=redis://redis:6379/0
    depends_on:
      - redis
  redis:
    image: redis

The Dockerfile in api/Dockerfile

FROM python:3.6-alpine

ENV CELERY_BROKER_URL redis://redis:6379/0
ENV CELERY_RESULT_BACKEND redis://redis:6379/0

ENV HOST 0.0.0.0
ENV DEBUG true

COPY ./api/requirements.txt /api/requirements.txt
COPY .env /api
WORKDIR /api

# install requirements
RUN pip install -r requirements.txt

# expose the app port
EXPOSE 5001

RUN pip install gunicorn

COPY ./api/app.py /api/app.py
COPY ./api/worker.py /api/worker.py

# run the app server
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "3", "app:app"]

Also interesting:

sudo docker network ls
NETWORK ID          NAME                          DRIVER              SCOPE
eb03130b85ea        bridge                        bridge              local
15f8a2e5cd21        host                          host                local
d80f015461c3        myapp_default                 bridge              local
0dd1b3ace731        none                          null                local
1

ports should expose it the way you want if you leave network_mode out. Just don't set it to host or none. Also, make sure it's not your host's firewall preventing access.

Port mapping is incompatible with network_mode: host

From: https://docs.docker.com/compose/compose-file/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.