15

How can I edit the config files that are inside of a docker container that has been downloaded on the host?

I am using this tutorial but I am not sure where to find and edit the traefik.toml file

Aaqib
  • 8,882
  • 3
  • 19
  • 28
Lolling Banana
  • 181
  • 1
  • 1
  • 4
  • Does this answer your question? [How do I edit a file after I shell to a Docker container?](https://stackoverflow.com/questions/30853247/how-do-i-edit-a-file-after-i-shell-to-a-docker-container) – Sean Feb 25 '20 at 04:28

3 Answers3

39

There are multiple ways to achieve that:

You can enter the container by running the command:

docker exec -it <container-name> bash

Note however depending on the container you may not have a simple text editor..


Another alternative would be to copy the file you want to edit from the container onto your host by running:

docker cp <container-name>:/path/to/file/in/container .

Edit the file and then copy it back into the container:

docker cp <file> <container-name>:/path/to/file/in/container

Third option is to create a bind mount which will effectively expose the file from the container onto the host

docker run -v $(pwd)/files:/dir/containing/file/in/container ...

This will expose the container folder in the "files" directory, and you can edit the file in the host and it will be directly reflected inside the container.

yamenk
  • 40,626
  • 10
  • 80
  • 81
  • You have my upvote, but it's important to note, option 1 does not work on a traefik container, which is in question. example, you're not even able to do `docker exec -it /bin/sh` which gives you the default shell of the container. I'm slightly convinced (though unable to fully explain) that it doesn't have any shells you can execute to do any form of `docker exec -it` – Arthur Weborg Nov 27 '17 at 21:43
  • You are probably right. The traefik image is based on scratch – yamenk Nov 28 '17 at 08:11
  • 5
    The third option binds a folder from the host into the container, not the other way around. – Tamlyn Oct 04 '18 at 10:18
6

I was running into the same issue and found a nice way to handle this. Using VS Code and the docker extension, get the container running. In the list of Containers, right click on the one you want to edit. Choose: Attach Visual Studio Code.

Attach VS Code to Docker

Another VS Code instance should open up that is directly attached to the container. Click on the Open Folder and navigate to the file you wish to edit. Pour a nice stout, chill for a moment, then get back to coding. :)

VS Code attached to Docker

Erick
  • 1,106
  • 15
  • 25
  • does this work on windows containers? getting username/pwd error. – user1594257 Feb 26 '21 at 19:59
  • The documentation of this VS Code feature is [available here](https://code.visualstudio.com/docs/remote/attach-container) btw. Your extensions don't carry over though, requiring their installation inside the container. – bwdm Apr 19 '21 at 21:02
  • note that you need to install VS code extension Remote - Containers https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers – aykcandem Dec 10 '21 at 11:03
1

Yes, works perfect with Windows containers too.

  1. Run a cmd into a crashing container to prevent exit:

    docker run -dit docker/image cmd

  2. Start VS Code with the docker extension. There is a open and download option for each file, very nice.