3

I am trying to access Flask app from the Docker compose getting started tutorial from my local host but without making changes to this pruned Dockerfile:

# syntax=docker/dockerfile:1
FROM python:3.9-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt

This if my docker-compose.yml:

    version: "3.9"

services:
  web:
    build: .
    command: flask run
    volumes: 
      - type: bind
        source: .
        target: /code 
    environment:
      - ENV FLASK_APP=app.py
        ENV FLASK_RUN_HOST=0.0.0.0
    ports:
      - target: 5000
        published: 8000      
    networks:
      - counter-net
  redis:
    image: "redis:alpine"
    networks:
      - counter-net
      
networks:
  counter-net:
  
volumes:
  volume-net:

When I use docker compose up I can see: Running on http://127.0.0.1:5000 but I cannot access it on Running on 127.0.0.1:8000 or localhost:8000

I can see 2_counter-net when I list networks and if relevant earlier I tried creating a volume but removed this when I changed the source to . and it came up without error.

How can I correct my config please?

3
  • 1
    If it says running on http://127.0.0.1:... it will be inaccessible from outside its own container; also see Deploying a minimal flask app in docker - server connection issues. Your Compose environment: has incorrect syntax and this is potentially contributing to the problem. Commented Oct 11, 2022 at 18:20
  • 3
    (You should be able to successfully remove all of the networks:, volumes:, and command: settings without affecting your application. Do make sure you've included a correct CMD in your Dockerfile.) Commented Oct 11, 2022 at 18:20
  • Thanks for the information, an environment catch, though leaving the Dockerfile unedited is a requirement here. Commented Oct 13, 2022 at 16:03

1 Answer 1

2

You are trying to use a bridge network so that ports opened in the container can be forwarded to ports on your host computer. It's true that you could remove the user-defined network and just rely on the default bridge network (by removing all the "networks" sections from the YAML file). That should solve your problem. However, Docker doesn't recommend this approach for production.

The other option is to add a bridge driver to your user-defined network specification:

networks:
  counter-net:
    driver: bridge

And David is right, you should fix the YAML in your environment.

    environment:
      - FLASK_APP=app.py
      - FLASK_RUN_HOST=0.0.0.0
Sign up to request clarification or add additional context in comments.

2 Comments

The default network that Compose provides is a "user-defined bridge network" in the language of the base Docker layer. There's nothing wrong with using it. driver: bridge is probably the default for a Compose network and you shouldn't need to specify it (it's not harmful but not helpful either).
I've run a small test and David is correct. You do not need to specify the driver in user-created docker compose networks in order to access containers from the host. Unfortunately, I cannot delete my answer because it has been accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.