18

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
  • 9,942
  • 4
  • 21
  • 30
Lolling Banana
  • 211
  • 1
  • 2
  • 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

5 Answers5

49

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

There's also a bind mount which will mount a host folder into the container

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

Files created in that folder in the container after the mount will be visible on the host BUT if that folder already existed in the container before the mount, it will be shadowed by the host folder making it inaccessible to either host or container.

Tamlyn
  • 22,122
  • 12
  • 111
  • 127
yamenk
  • 46,736
  • 10
  • 93
  • 87
  • 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
  • 6
    The third option binds a folder from the host into the container, not the other way around. – Tamlyn Oct 04 '18 at 10:18
  • @Tamlyn Good point! I used 3rd option thinking it's for binding from container to host and was wondering why it's not working. I think yamenk should edit their answer – xperator Sep 29 '22 at 09:27
  • In option two, when you pull files out of docker container, what is . and where do I find the files on the Windows file system? – SquishyRhode Jun 20 '23 at 18:52
8

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,176
  • 15
  • 25
  • 1
    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
  • 1
    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
  • You will not see the option to "Attach Visual Studio Code" without installing Remote - Containers extension as stated in the above comment. – James Baird Feb 12 '23 at 18:04
0

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.

0

You can expose the container over port 22 and then edit whatever file you want over ssh.

vim scp://user@myserver[:port]//path/to/file.txt

See more: How to edit file within Docker container or edit a file after I shell into a Docker container?

logbasex
  • 1,688
  • 1
  • 16
  • 22
0

If you want to add a line to a file such as a line of config you can use echo like this and append it to a file:

echo 'super-setting: "10.96.85.221:9200"' >> config.yml

This only works if you want to add extra lines and not if you want to edit an existing line.

I've found it a quick and easy way to make and test temp changes to a file in a docker container.

Richard Garside
  • 87,839
  • 11
  • 80
  • 93