4

I am trying to run a simple Flask application inside docker. But it seems like even when I update my app.py code and restart the docker container nothing updates.

I am running docker on OS X. IS this something simple I am missing or this is an expected behavior?

This is what my docerfile looks like:

FROM ubuntu:14.04.3

# install dependencies
RUN apt-get update
RUN apt-get install -y nginx
RUN apt-get install -y supervisor
RUN apt-get install -y python3-pip

# update working directories
ADD ./app /app
ADD ./config /config
ADD requirements.txt /

# install dependencies
RUN pip3 install -r requirements.txt

# setup config
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
RUN rm /etc/nginx/sites-enabled/default

RUN ln -s /config/nginx.conf /etc/nginx/sites-enabled/
RUN ln -s /config/supervisor.conf /etc/supervisor/conf.d/

EXPOSE 80
CMD ["supervisord", "-n"]
Jonathan
  • 2,728
  • 10
  • 43
  • 73
  • 1
    How do you restart the docker container? – pts Aug 22 '16 at 19:27
  • @pts I have tried stopping and restarting the container so closing `docker run -p 80:80 app` and then restarting. Also tried the same with kitematic where I use the UI to restart the container. – Jonathan Aug 22 '16 at 19:31

1 Answers1

9

Docker images (what you get after docker build -t app .) is a "frozen" snapshot. Its not editable; its a snapshot of whatever you add to the image at that point in time.

Now, once you run an image, the contents are expanded (think of it like the archive is unzipped) and then the process you defined in the image runs; and this is a container.

Running containers can be shown by docker ps, and images (things you can use to run new containers) are shown by docker images.

A container can write to the file system, but by default all changes are lost once the container is stopped. These changes are not stored back to the image.

Images are immutable until you rebuild them, and containers continue to use the image they were started with. So with your Dockerfile method of importing your app.py, you need to run the following to update that file:

docker build -t app .
docker stop <container_id>
docker rm <container_id>
docker run -p 80:80 -d --name=my-app app

You'll need to run docker ps -a to get your current container id. By naming your container, you can reference it by "my-app" or any other name you pick going forward.

Note that this is the slow way to do your update. For more efficient developing, use a volume (with MacOS, this must be located under /Users):

docker run -p 80:80 -v $(pwd)/app:/app -d --name=my-app app

Now, anytime you update your app folder, you can restart python assuming it doesn't have an automatic reload included:

docker restart my-app
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • Hi @BMitch, I think removing helps. When I add `$(pwd)/app:/app` but the app does not refresh itself. Would you happen to know an easier way of accomplishing that? – Jonathan Aug 22 '16 at 20:03
  • You need to rebuild the _image_ and then run the _container_. – Burhan Khalid Aug 22 '16 at 20:05
  • 2
    @BMitch - I added some background to your excellent answer, hopefully this will clear things up for the OP. – Burhan Khalid Aug 22 '16 at 20:11
  • This did it! Thanks guys – Jonathan Aug 22 '16 at 20:18
  • @Jonathan if you switch over to using the volume, you need to run the `restart` command at the end after each change. If you'd like to automatically reload on every change to the python script, there are other [python specific solutions described in this question](http://stackoverflow.com/q/437589/596285) – BMitch Aug 22 '16 at 20:21