In recent years, Docker containers have become more and more widely used. They can package applications in the form of containers to facilitate porting to different environments. But in some scenarios, we may need to copy the data or code in the Docker container. This article will introduce how to copy the things in the Docker container.
In some scenarios, we need to copy the data or code in the container to facilitate various needs such as backup, migration, and debugging. In Docker, data and code are mainly stored in the file system of the container. Different containers are isolated from each other and therefore cannot be copied directly. Data and code need to be copied to the host through some commands. Two common methods are introduced below:
The docker cp command can copy local files into the container, and you can also copy the files in the container to the local. The following is how to use this command:
docker cp <容器ID或名称>:<容器内路径> <宿主机路径>
Among them, <Container ID or name>
indicates that copying is required The ID or name of the container, <path within the container>
indicates the file path within the container that needs to be copied, <host path>
indicates the file that needs to be copied to Host path.
For example, copy the /etc/nginx/nginx.conf
file in the container nginx
to the /opt
directory of the host:
docker cp nginx:/etc/nginx/nginx.conf /opt
Use the docker commit command to package the file system in the container into a new image. The specific usage method is as follows:
docker commit -m "commit message" <容器ID或者名称> <新的镜像名称>
Among them, the -m
option indicates the description of this commit operation. <Container ID or name>
indicates the ID or name of the container that needs to be packaged into an image, <New image name>
indicates the name of the new image generated.
For example, package the file system in the container nginx
into a new image:
docker commit -m "backup nginx config" nginx nginx-config-backup
Copying the data and code in the Docker container is This article introduces how to implement common requirements in daily work from two aspects:
Both of the above two methods can realize the need to copy the data and code in the container. You can choose the appropriate method according to the actual situation.
The above is the detailed content of How to copy things in a Docker container. For more information, please follow other related articles on the PHP Chinese website!