简体   繁体   中英

Editing Files inside of a Docker Container

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

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.

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.

将 VS Code 附加到 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. :)

附加到 Docker 的 VS Code

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM