1

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?

saltyeggs
  • 63
  • 1
  • 7
  • 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](https://stackoverflow.com/questions/30323224/deploying-a-minimal-flask-app-in-docker-server-connection-issues). Your Compose [`environment:`](https://docs.docker.com/compose/compose-file/compose-file-v3/#environment) has incorrect syntax and this is potentially contributing to the problem. – David Maze Oct 11 '22 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.) – David Maze Oct 11 '22 at 18:20
  • Thanks for the information, an environment catch, though leaving the Dockerfile unedited is a requirement here. – saltyeggs Oct 13 '22 at 16:03

1 Answers1

1

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
Nick K9
  • 3,885
  • 1
  • 29
  • 62
  • The [`default` network that Compose provides](https://docs.docker.com/compose/networking/) 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). – David Maze Oct 14 '22 at 13:14
  • 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. – Nick K9 Oct 26 '22 at 13:30