Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit files in stopped/not starting docker container

Tags:

bash

docker

Trying to fix errors and debug problems with my application that is split over several containers, I frequently edit files in containers:

  • either I am totally lazy and install nano and edit directly in container or

  • I docker cp the file out of the container, edit it, copy it back and restart the container

Those are intermediate steps before coming to new content for container build, which takes a lot longer than doing the above (which of course is only intermediate/fiddling around).

Now I frequently break the starting program of the container, which in the breaking cases is either a node script or a python webserver script, both typically fail from syntax errors.

Is there any way to save those containers? Since they do not start, I cannot docker exec into them, and thus they are lost to me. I then go the rm/rmi/build/run route after fixing the offending file in the build input.

How can I either edit files in a stopped container, or cp them in or start a shell in a stopped container - anything that allows me to fix this container?

(It seems a bit like working on a remote computer and breaking the networking configuration - connection is lost "forever" this way and one has to use a fallback, if that exists.)

How to edit Docker container files from the host? looks relevant but is outdated.

like image 419
Andreas Reiff Avatar asked Sep 23 '15 22:09

Andreas Reiff


People also ask

How do I resume a stopped docker container?

Enter the docker start command with the Container ID in the command line to resume the Container.

Can you start a stopped docker container?

When we try to run /bin/sh on a stopped container using docker exec , Docker will throw a No such container error. We have to transform the stopped Docker container into a new Docker image before we can inspect the internals of the container. We can transform a container into a Docker image using the commit command.


2 Answers

I had a problem with a container which wouldn't start due to a bad config change I made. I was able to copy the file out of the stopped container and edit it. something like:

docker cp docker_web_1:/etc/apache2/sites-enabled/apache2.conf . 

(correct the file)

docker cp apache.conf docker_web_1:/etc/apache2/sites-enabled/apache2.conf 
like image 92
tomurie Avatar answered Sep 18 '22 08:09

tomurie


Answering my own question.. still hoping for a better answer from a more knowledgable person!!

There are 2 possibilities.

1) Editing file system on host directly. This is somewhat dangerous and has a chance of completely breaking the container, possibly other data depending on what goes wrong.

2) Changing the startup script to something that never fails like starting a bash, doing the fixes/edits and then changing the startup program again to the desired one (like node or whatever it was before).

More details:

1) Using

docker ps 

to find the running containers or

docker ps -a 

to find all containers (including stopped ones) and

docker inspect (containername) 

look for the "Id", one of the first values.

This is the part that contains implementation detail and might change, be aware that you may lose your container this way.

Go to

/var/lib/docker/aufs/diff/9bc343a9..(long container id)/ 

and there you will find all files that are changed towards the image the container is based upon. You can overwrite files, add or edit files.

Again, I would not recommend this.

2) As is described at https://stackoverflow.com/a/32353134/586754 you can find the configuration json config.json at a path like

/var/lib/docker/containers/9bc343a99..(long container id)/config.json 

There you can change the args from e. g. "nodejs app.js" to "/bin/bash". Now restart the docker service and start the container (you should see that it now correctly starts up). You should use

docker start -i (containername) 

to make sure it does not quit straight away. You can now work with the container and/or later attach with

docker exec -ti (containername) /bin/bash 

Also, docker cp is rather useful for copying files that were edited outside of the container.

Also, one should only fall back to those measures if the container is more or less "lost" anyway, so any change would be an improvement.

like image 43
Andreas Reiff Avatar answered Sep 19 '22 08:09

Andreas Reiff