Kid_Learning_C
Kid_Learning_C

Reputation: 3623

Do docker containers pick up code changes on-the-fly?

I have my docker containers up and running. There is one container running some python code and I found that it is causing some bug. I want to add some lines of code (mainly more logs) to a python script within that particular container.

I want to just go into the container by docker exec -ti container_name bash and start to edit code by nano my_python_script.py. Does the running container pick up these changes automatically, on-the-fly?

Or do I need to do something for these changes to come into effect, i.e. to print the new logging information?

Upvotes: 4

Views: 4147

Answers (2)

BMitch
BMitch

Reputation: 264276

Answering because there's some misinformation in other answers here. The correct answer is in the comment from MyTwoCents:

It will behave same way as it would when you do it on your system. So if you edit nano my_python_script.py will it do change automatically on your system?

Put quite simply, if your application dynamically updates itself when the files on the filesystem change, then the same will happen when you modify those files inside of a container.


The misinformation in Mark's answer concerns me:

Docker container is immutable, so whatever changes you do in filesystem of the container itself won't survive the restart of container

That is not accurate. Docker images are stored as a collection of filesystem layers that are immutable. Containers are ephemeral, files changed inside the container will revert when the container is deleted and replaced with a new container. However, restarting a container effectily stops and restarts the process inside the container with the same filesystem state of that container. Any changes that happen to the container's filesystem are maintained for the life of the container. Volumes are used when you want changes to survive beyond the life of a single container.


The correct answer is that you most likely need to restart the python process inside the container to see the filesystem changes, and stopping the python process will stop the container. It becomes an awkward process to keep exec'ing into a container to make changes and then exit to restart the container for the changes to appear. Therefore, a more typical developer workflow is to mount the filesystem of code into the container so you can make changes directly on your host, where those changes will not be logs, and then restart the container. Since python does not require compiling your code into a binary, you'll see your changes on every restart. If your Dockerfile contained lines like:

FROM python
COPY . /code
CMD python /code/my_python_script.py

You could make that a volume with:

docker run --name container_name -v "$(pwd):/code" image_name

And then to restart with changes:

docker restart container_name

Upvotes: 4

Mark Bramnik
Mark Bramnik

Reputation: 42501

A couple of facts about docker containers:

  1. Docker container lives as long as the process it runs lives usually.
  2. Docker container is immutable, so whatever changes you do in filesystem of the container itself won't survive the restart of container (I'm not talking about volumes, its more advanced stuff)

Based on these facts:

The question basically boils down to whether the changes in my_python_script.py that you do "on-the-fly" requires the restart of python process. And this really depends on what/how exactly do you run the python.

If it requires the restart - then no, you won't be able to see the logs. The restart won't help either, because of fact "2" - you'll lost the changes (additional log prints in this case).

If Python is able to dynamically reload the script and run it within the same process (without the restart of the container) then you can do that.

Upvotes: 5

Related Questions