0

I have a flask app that runs perfectly fine when using the simple terminal command flask run, with the following directory structure:

flask-demo
│   Dockerfile
│   requirements.txt   
│.  start.sh
└───app
    │   __init__.py
    │   other-extensions.py
    │
    └───microservice-folders
    │   │   ...
    └───templates   
    │   │   ...

The content of __init__.py contains

**imports**

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    #manage login
    ***

    # Initialize Flask extensions here
    ***


    bs = Bootstrap(app) #flask-bootstrap

    # Register blueprints here
    ***

    return app


The content of Dockerfile is:

FROM python:3.9

WORKDIR /python-docker

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .


CMD ["flask", "run", "--host=0.0.0.0"]

This is what I always start with, and then from here I've tried many different iterations of how to actually run the docker container, but currently I am using a start.sh script that looks like this:

#!/bin/zsh
app="docker.test"
docker build -t ${app} .
docker run -d -p 56733:80 \
  --name=${app} \
  -v $PWD:/app ${app}

And I then run zsh start.sh in terminal. The container starts, as I get this output from docker ps

CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS                   NAMES
40c6b6dfca3a   docker.test   "flask run --host=0.…"   5 seconds ago   Up 3 seconds   0.0.0.0:56733->80/tcp   docker.test

However, when navigating to http://localhost:56733, I get the error "Safari cannot open the page http://localhost:56733 because it dropped the connection. This sometimes occurs when the server is busy. Wait a few minutes, then try again".

I've tried different iterations of Dockerfiles where I expose different ports, I've tried not using a start.sh file and just using a docker run -d -p 56733:56733 docker.test command, and I always get the same issue of the page just not loading.

EDIT: When I fixed the port problem by routing -p 8080:5000, I still get a completely blank screen.

EDIT2: I just had a friend pull my project from git and run it on his computer, and it actually works for him on his non-mac device. But it still does not work for me on my Mac.

ai.jennetta
  • 1,076
  • 2
  • 10
  • 25
  • The second `docker run -p` port number needs to match the port that the process inside the container is using. For Flask, that default port is 5000. – David Maze May 14 '23 at 13:04
  • @David Maze, yep when I do this its the same issue – ai.jennetta May 14 '23 at 13:24
  • you need to [access the container](https://stackoverflow.com/questions/30172605/how-do-i-get-into-a-docker-containers-shell) using a shell and see the error messages you get when running `flask run --host=0.0.0.0` through that. If that works without error, it is something with the ports. Also to make sure you are running on 5000 or 80 or whatever, you can add `--port 80` to the command – Matthias May 15 '23 at 09:17
  • Have you cleaned the browser cache @ai.jennetta ? – tremendows Jun 20 '23 at 15:44

0 Answers0