YouTip LogoYouTip

Docker Cp Command

# Docker cp Command [![Image 3: Docker Command Manual](#)Docker Command Manual](#) * * * The `docker cp` command is used to copy files or directories between a Docker container and the host machine. The `docker cp` command supports copying files from the container to the host, or from the host to the container. ### Syntax docker cp SRC_PATH CONTAINER:DEST_PATH docker cp CONTAINER:SRC_PATH DEST_PATH * **`SRC_PATH`**: Source path (can be a path inside the container or on the host). * **`CONTAINER`**: The name or ID of the container. * **`DEST_PATH`**: Destination path (can be a path inside the container or on the host). **Copy files from container to host** docker cp my_container:/path/in/container /path/on/host Copies the file or directory at `/path/in/container` inside the container `my_container` to `/path/on/host` on the host machine. **Copy files from host to container** docker cp /path/on/host my_container:/path/in/container Copies the file or directory at `/path/on/host` on the host machine to `/path/in/container` inside the container `my_container`. **Copy directory from container to host** docker cp my_container:/path/in/container /path/on/host Copies the directory at `/path/in/container` inside the container and its contents to `/path/on/host` on the host machine. **Copy directory from host to container** docker cp /path/on/host my_container:/path/in/container Copies the directory at `/path/on/host` on the host machine and its contents to `/path/in/container` inside the container `my_container`. ## Examples ### Copy file from container to host Start a container and create a file: docker run -d --name my_container ubuntu bash -c "echo 'Hello, Docker!' > /hello.txt" Copy a file from the container to the host: docker cp my_container:/hello.txt ./hello.txt Copies the `/hello.txt` file from inside the container to the current directory on the host. View the content of the copied file: cat hello.txt Output: Hello, Docker! ### Copy file from host to container Copy a file to the container: docker cp ./hello.txt my_container:/hello.txt Copies the `hello.txt` file from the host to `/hello.txt` inside the container. Enter the container to view the file content: docker exec -it my_container cat /hello.txt Output: Hello, Docker! ### Notes * The `docker cp` command does not modify the source file or directory; it only performs a copy operation. * The destination path must be a valid path, and the host or container must have sufficient permissions to perform write operations. * When dealing with large files or directories, the copy operation may take some time. The `docker cp` command is a useful tool provided by Docker for copying files or directories between containers and the host machine. This command is suitable for operations such as backups, configuration updates, and log collection, helping users conveniently manage and operate files within containers. * * Docker Command Manual](#) AI is thinking... [](#)(#) (#)[](#)
← Docker Push CommandProp Element Clientwidth β†’