1

I edited a file in a running docker container and restarted it, unfortunately my last edit was not correct. So every time I start the container with:

docker start <containerId>

It always exits immediately.

Now I can not even modify my edit back, since

docker exec -it <containerId> bash

can only run on a running docker.

The question is how can I change it and restart the container now? Or I had to abandon it and start a new container from an existing image?

techraf
  • 64,883
  • 27
  • 193
  • 198
xidui
  • 79
  • 2
  • 7
  • 1
    You can't, you will need to start from a fresh container – tpbowden Mar 30 '16 at 12:33
  • You *can*, but probably *shouldn't* unless you really know that it's what you want to be doing. (See below) – ThatsNinja Mar 30 '16 at 12:45
  • While this can be useful in developer test conditions, I'd consider having to do this a smell. Ideally, it should be possible to throw a away the container and start fresh. – Raphael Jun 09 '20 at 15:19

2 Answers2

2

You didn't supply any details regarding your container's purpose, or what you modified. Conceptually, you could create the file that needs to be modified in a place on your filesystem and mount that file into the container as a volume when you start it, like:

docker run -it -v /Users/<path_to_file>:<container_path_to_file> <container>

Hovever, this is bad form, as your container loses portability at that point without committing a new image.

Ideally, changes that need to be made inside of a Docker container are made in the Dockerfile, and the container image re-built. This way, your initial, working container state is represented in your Dockerfile code, making your configuration repeatable, portable, and immutable.

ThatsNinja
  • 374
  • 1
  • 9
  • yeah, you are right. I just think changing a config and rebuild an image is someshow slow. I want to see whether a change is valid, so I edit it directly inside container and see the result for debug. – xidui Mar 30 '16 at 13:33
  • Sure, and the best way I can think to do that is with a volume mount, as above. If you've ruined a file in an existing container image, using that technique to replace the bad file with a working one will at least get you into the container. You can then test your changes by editing the file outside the container, and not worry that you're going to lock yourself out. – ThatsNinja Mar 30 '16 at 17:39
0

The file system of exited containers can still be changed. The preferable way is probably:

docker cp <fixedFile> <containerId>:<brokenFile>

But you can also circumvent docker completely; see here.

Raphael
  • 9,779
  • 5
  • 63
  • 94