4

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?

tgogos
  • 23,218
  • 20
  • 96
  • 128
Kid_Learning_C
  • 2,605
  • 4
  • 39
  • 71
  • 4
    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? – MyTwoCents Nov 25 '19 at 09:16
  • you need to re-run the python script so the changes taking place, the easiest way is docker restart my_container – LinPy Nov 25 '19 at 09:18
  • @LinPy Docker restart will not pick the users changes. It will reload whats packed with docker. The user wants to login into bash and change the script using a vi , save it and expecting docker to relaunch itself. – Srini M Nov 25 '19 at 10:44
  • I generally debug applications running in Docker the same way I debug other compiled and deployed applications: reproduce the issue in a non-Docker development tree, write a test for it, fix the test, and only then redeploy it in Docker. As @MarkBramnik's answer notes, if you tweak things inside a container, they'll get lost as soon as you delete the container (which is a very routine activity), and often containers don't have a usable text editor at all (it's not needed to run the actual packaged application). – David Maze Nov 25 '19 at 13:08

2 Answers2

5

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.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
4

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
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • as it seems this is the only way of doing it, right? i mean create the container, bind the volume and, restart it , it would be very cool if there was something like nodemon ( for js files ) in python, any updates? – amdev Sep 07 '20 at 21:00
  • @a_m_dev I know docker better than python. If there's a python method to do this outside of a container, you could do the same with docker. But this requires the language to provide the solution. – BMitch Sep 08 '20 at 01:01
  • Specific to python, there are ways to write the application to support this: https://stackoverflow.com/q/3862871/596285 (I've also seen this done in flask). – BMitch Sep 08 '20 at 01:06