How to edit files in stopped/not starting docker container

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.


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

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.

Tags:

Docker

Bash