How to edit files in stopped/not starting docker container

BashDocker

Bash Problem Overview


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.)

https://stackoverflow.com/questions/24553790/how-to-edit-docker-container-files-from-the-host looks relevant but is outdated.

Bash Solutions


Solution 1 - Bash

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

Solution 2 - Bash

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.

  1. 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.

Solution 3 - Bash

You can edit container file-system directly, but I don't know if it is a good idea. First you need to find the path of directory which is used as runtime root for container. Run docker container inspect id/name. Look for the key UpperDir in JSON output.

That is your directory.

Solution 4 - Bash

If you are trying to restart an stopped container and need to alter the container because of misconfiguration but the container isn't starting you can do the following which works using the "docker cp" command (similar to previous suggestion). This procedure lets you remove files and do any other changes needed. With luck you can skip a lot of the steps below.

  1. Use docker inspect to find entrypoint, (named Path in some versions)

  2. Create a clone of the using docker run

  3. Enter clone using docker exec -ti bash (if *nix container)

  4. Locate entrypoint file location by looking though the clone to find

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreas ReiffView Question on Stackoverflow
Solution 1 - BashtomurieView Answer on Stackoverflow
Solution 2 - BashAndreas ReiffView Answer on Stackoverflow
Solution 3 - BashTejas SaradeView Answer on Stackoverflow
Solution 4 - Bashuser6830669View Answer on Stackoverflow