1

I've setup a Python Flask project to point at an interpreter running in Docker:

Starting the Flask app from PyCharm IDE results in following output:

51be5e336622:python3 -u /opt/project/app.py
Starting server
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
Starting server
 * Debugger is active!
 * Debugger PIN: 328-461-278

Trying to open http://localhost:5000/ results in a page not found error.

If I run the app using Docker with Dockerfile:

FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt

COPY . .

EXPOSE 5000

#CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

CMD [ "python3", "-u" , "/app/app.py"]

The above Dockerfile builds the container PyCharm connects to and is configured within Pycharm as:

enter image description here

Running the Docker container using command docker run -p 5000:5000 services, the app is started on http://localhost:5000/ successfully.

Do I need to map the port 5000 from PyCharm IDE to the Docker container running on port 5000? How to achieve this, there does not seem to be an option within PyCharm IDE?

2
  • are you running the app on MacOS ? Commented Apr 6, 2021 at 8:50
  • @ThanhNguyenVan yes, running on MacOS Commented Apr 6, 2021 at 8:50

1 Answer 1

2

With the help of https://blog.jetbrains.com/pycharm/2017/03/docker-compose-getting-flask-up-and-running/ this is now working.

Dockerfile:

FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt

COPY . .

EXPOSE 5000

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

main method in app.py:

if __name__ == '__main__':

    for handler in logging.root.handlers[:]:
        logging.root.removeHandler(handler)

    logging.basicConfig(level=logging.INFO,
                        filename='./logs/' + str(
                            int(round(time.time() * 1000))) +'trade.log',
                        filemode="a+",
                        format="%(asctime)-15s %(levelname)-8s %(message)s",
                        datefmt='%Y-%m-%d %H:%M:%S')
    logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))

    print('Starting server')
    app.run(host="0.0.0.0", port=5000, debug=True)

docker-compose.yml, note use of port 5000 :

version: '2'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app

Start docker-compose:

docker-compose up

Update PyCharm to use docker compose instance:

enter image description here

Output of executing main in app.py:

Attaching to ml-services_web_1
web_1  | Starting server
web_1  |  * Serving Flask app "app" (lazy loading)
web_1  |  * Environment: production
web_1  |    WARNING: This is a development server. Do not use it in a production deployment.
web_1  |    Use a production WSGI server instead.
web_1  |  * Debug mode: on
web_1  |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1  |  * Restarting with stat
web_1  | Starting server
web_1  |  * Debugger is active!
web_1  |  * Debugger PIN: 828-722-345
web_1  | 172.20.0.1 - - [06/Apr/2021 13:36:32] "GET / HTTP/1.1" 200 -

http://localhost:5000/ is now accessible.

Sign up to request clarification or add additional context in comments.

Comments

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.