23

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

1

5 Answers 5

56

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 <container-name> /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
You are probably right. The traefik image is based on scratch
The third option binds a folder from the host into the container, not the other way around.
@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
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?
12

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

4 Comments

does this work on windows containers? getting username/pwd error.
The documentation of this VS Code feature is available here btw. Your extensions don't carry over though, requiring their installation inside the container.
note that you need to install VS code extension Remote - Containers marketplace.visualstudio.com/…
You will not see the option to "Attach Visual Studio Code" without installing Remote - Containers extension as stated in the above comment.
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.

Comments

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?

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.