skip to Main Content

I created a simple application with Flask and am running it on AWS cloud9.

To create the docker, I ran the command docker build -t docker-flask:v1.0.0 . and to run docker docker run -p 5000:5000 docker-flask:v1.0.0. As the port I chose was 5000 and I made the necessary settings in the security group by adding that port in the inbound roles.

  • dockerfile:
FROM python:3.9.6-slim-buster
WORKDIR /app
ADD . /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

Everything runs perfectly, but when I make any modification in my code no change occurs when I compile the project, the way it is I would have to create the docker for each version of my code. The complete project can be accessed: https://github.com/AbadeDiego/flask-app.

Does anyone know what I am doing wrong?

2

Answers


  1. You can use a volumes, so you don’t have to rebuild the image every time you make a change to your application’s source code. More about volumes here: https://docs.docker.com/storage/volumes/

    Example:

    docker run -p 5000:5000 -v "$PWD"/app:/app docker-flask:v1.0.0
    

    Don’t forget to enable auto reload in your flask app so you don’t have to restart app in the container after every change.

    app.run(debug=True)
    

    or shell:

    $ export FLASK_DEBUG=1
    $ flask run
    
    Login or Signup to reply.
  2. When you build the docker file for the first time with v1.0.0, the initial code is picked. But when changes are made to the source code, the running container will not fetch the new source code until and unless you build the docker image again.

    Yes, you can achieve that with versioning. Or you can mount the volume to the docker to the source code so that it automatically fetches the new changes.

    As @KSs rightly mentioned, you can set the config of the Flask app to enable auto-reload.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search