Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
The docker cp
command allows you to copy files and directories between a Docker container and your local host system. This can be useful for transferring data, backing up container data, or importing/exporting files to/from a container. In this tutorial, we'll cover the basics of using the docker cp
command.
Syntax:
docker cp [OPTIONS] CONTAINER:SRC_PATH HOST_DEST_PATH docker cp [OPTIONS] HOST_SRC_PATH CONTAINER:DEST_PATH
OPTIONS
: Optional arguments for the docker cp
command.CONTAINER
: The name or ID of the container you want to copy files to/from.SRC_PATH
: The source path of the file or directory you want to copy.HOST_DEST_PATH
or HOST_SRC_PATH
: The destination or source path of the file or directory on your local host system.DEST_PATH
: The destination path of the file or directory inside the container.Examples:
Copy a file from a container to the host:
docker cp my-container:/path/in/container/file.txt /path/on/host/file.txt
This command copies the file.txt
from the specified container path to the specified host path.
Copy a file from the host to a container:
docker cp /path/on/host/file.txt my-container:/path/in/container/file.txt
This command copies the file.txt
from the specified host path to the specified container path.
Copy a directory from a container to the host:
docker cp my-container:/path/in/container/directory /path/on/host/directory
This command copies the entire directory
from the specified container path to the specified host path.
Copy a directory from the host to a container:
docker cp /path/on/host/directory my-container:/path/in/container/directory
This command copies the entire directory
from the specified host path to the specified container path.
Note that the docker cp
command will preserve the ownership, permissions, and timestamps of the copied files and directories.
This tutorial should give you a basic understanding of how to use the docker cp
command to copy files and directories between Docker containers and your local host system. This can be useful for transferring data, backing up container data, or importing/exporting files to/from a container.
How to Use Docker cp Command:
docker cp
is a command-line tool for copying files and directories between the local filesystem and a container filesystem.docker cp
:docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copying Files to and from Docker Containers with cp:
docker cp
facilitates transferring files between the host and containers.docker cp local_file.txt my_container:/app/container_file.txtExample of copying a file from container to host:
docker cp my_container:/app/container_file.txt ./local_file.txt
Docker cp from Host to Container:
docker cp
.docker cp local_file.txt my_container:/app/container_file.txt
Docker cp from Container to Host:
docker cp
.docker cp my_container:/app/container_file.txt ./local_file.txt
Preserving File Permissions with Docker cp:
-a
or --archive
flag to preserve file permissions during the copy operation.docker cp -a local_directory my_container:/app/
Copying Directories with Docker cp:
docker cp local_directory my_container:/app/
Docker cp and Volumes:
docker cp
can also copy files to and from volumes associated with containers.docker cp local_file.txt my_container:/app/volume_file.txt
Using Docker cp in Docker Compose:
docker cp
for containers defined in the docker-compose.yml
file.docker cp
with Docker Compose:docker-compose cp local_file.txt my_service:/app/container_file.txt
Interactive Mode with Docker cp:
docker cp
interactively by specifying -i
for interactive mode.docker cp -i local_file.txt my_container:/app/container_file.txt