Docker
Published in Docker
avatar
3 minutes read

Copying files from a Docker container to the host

To copy files from a Docker container to the host machine, you can use the docker cp command. This allows you to transfer files between the container and the host easily.

Using docker cp command

The docker cp command allows you to copy files between the Docker container and the host machine.

# Syntax:
# docker cp <container_name_or_id>:<container_path> <host_path>

# Example: Copy a file from the container to the host
docker cp my_container:/path/to/file.txt /host/path/

# Example: Copy a directory from the container to the host
docker cp my_container:/path/to/directory /host/path/

Replace <container_name_or_id> with the name or ID of your container. <container_path> represents the path to the file or directory inside the container that you want to copy, and <host_path> is the destination path on the host machine where the file or directory will be copied.

Keep in mind that the destination path on the host must be an absolute path.

Example: Copying files from a running container

If you have a running container, you can use the docker cp command as follows:

# Example: Copy a file from a running container to the host
docker cp my_running_container:/app/data.txt /host/destination/

# Example: Copy a directory from a running container to the host
docker cp my_running_container:/app/data_directory /host/destination/

Example: Copying files from a stopped container

Even if the container is stopped, you can still use docker cp to extract files from it:

# Example: Copy a file from a stopped container to the host
docker cp my_stopped_container:/app/data.txt /host/destination/

# Example: Copy a directory from a stopped container to the host
docker cp my_stopped_container:/app/data_directory /host/destination/

Note

Remember that when using docker cp, you are copying files from the container to the host or vice versa. Be cautious when overwriting files on the host to avoid accidental data loss.

0 Comment