With the popularity of containerization technology, Docker has become an indispensable part of major companies and developers. When we use Docker to build an image, sometimes we need to transfer files to the image to facilitate operations inside the container. This article will introduce several methods to transfer files to Docker images.
1. Use the COPY instruction in the Dockerfile
Docker official documentation provides the COPY instruction, which can copy local files or directories to the Docker image to quickly generate the image. An example is as follows:
FROM ubuntu:18.04 COPY test.txt /usr/local/test.txt
In the above code, we use Ubuntu 18.04 as the base image and copy the local test.txt file to the /usr/local directory in the image.
2. Use the Docker CP command
The Docker CP command can copy local files to the running container, or copy files from the container to the local. The example is as follows:
Copy local files to the container:
docker cp /path/to/local/file container_name:/path/to/destination
Copy the files in the container to the local:
docker cp container_name:/path/to/file /path/to/destination
3. Use Docker Volume
Docker Volume can mount a local folder to a specified directory in the container, so that data can be shared between the container and the host. An example is as follows:
docker run -v local_folder:/container_folder container_name
The above command mounts the local local_folder folder to the /container_folder directory in the container.
Summary
This article introduces three methods to transfer files to Docker images: Dockerfile, Docker CP command and Docker Volume. These methods can help developers add necessary files inside the container and make information sharing between containers easier and faster. Among them, the COPY instruction in the Dockerfile can transfer files during image building, the Docker CP command can dynamically transfer files during the running of the container, and the Docker Volume can realize data sharing between the container and the host.
The above is the detailed content of How docker transfers files to the image. For more information, please follow other related articles on the PHP Chinese website!