All the following three can be modified and you can choose
1. Enter the container to modify it
Use the following commands to enter the inside of the container to modify the file in the form of a command line.
docker exec -it containerID /bin/bash
However, there is no vim in it, so you need to install it yourself. The installation code is as follows. However, this form is not recommended, because the files inside are temporary. After the container is deleted, the configuration will be invalid and need to be configured again.
apt-get update apt-get install vim
2. Modify through docker cp copy
You can copy the file that needs to be modified through the following code, and then copy it back after the modification is completed. This method is actually similar to the first one, except that you don’t need to install vim, but after the container is deleted, the modified content will also become invalid. And the container needs to be restarted to take effect (it seems to be)
#Copy the files in the containersudo docker cp containerID:/etc/mysql/ /home/tom/ #Copy the files in the container backsudo docker cp /home/tom/ containerID:/etc/mysql/
3. Use -v to mount the folder (recommended)
The last method is to use -v to mount (map) the folder inside the container to a local path when starting. In the future, you can directly modify it locally without entering the container.
#Before the colon, the local path (absolute path is required), and after the colon, the path in the container$ docker run --name mytomcat -v /home/www/webapps:/usr/local/tomcat/webapps -d tomcat
Finally restart the container (restart to take effect)
docker restart containerid
Attachment: docker modify read-only files inside the container
To modify a read-only file inside a Docker container, you can use the following steps:
First, use
docker ps
The command views the list of running containers and finds the name or ID of the container to be modified.use
docker exec -it <container name or ID> bash
Commands enter the interactive terminal of the container.Inside the container, find the location of the read-only file you want to modify and open the file with an appropriate editor (such as vi, nano, etc.).
In the editor, make the required modifications and save the file.
Exit the container's interactive terminal, you can use
exit
Order.
Note: The above steps are only applicable to modifying read-only files inside the container. If you need to modify the read-only file when starting the container, you can do so by building a custom Docker image.
Summarize
This is the end of this article about 3 simple methods for Docker to modify internal files in containers. For more related contents of Docker to modify internal files in containers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!