Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App in Docker does not update

Tags:

python

docker

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"]
like image 259
Jonathan Avatar asked Jan 05 '23 12:01

Jonathan


1 Answers

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
like image 170
BMitch Avatar answered Jan 12 '23 00:01

BMitch