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

Docker cp 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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  1. How to Use Docker cp Command:

    • Description: docker cp is a command-line tool for copying files and directories between the local filesystem and a container filesystem.
    • Code: Syntax of docker cp:
      docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
      docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
      
  2. Copying Files to and from Docker Containers with cp:

    • Description: docker cp facilitates transferring files between the host and containers.
    • Code: Example of copying a file from host to container:
      docker cp local_file.txt my_container:/app/container_file.txt
      
      Example of copying a file from container to host:
      docker cp my_container:/app/container_file.txt ./local_file.txt
      
  3. Docker cp from Host to Container:

    • Description: Copy files from the host to a container using docker cp.
    • Code: Example of copying a file from the host to a container:
      docker cp local_file.txt my_container:/app/container_file.txt
      
  4. Docker cp from Container to Host:

    • Description: Copy files from a container to the host using docker cp.
    • Code: Example of copying a file from a container to the host:
      docker cp my_container:/app/container_file.txt ./local_file.txt
      
  5. Preserving File Permissions with Docker cp:

    • Description: Use -a or --archive flag to preserve file permissions during the copy operation.
    • Code: Example of preserving file permissions:
      docker cp -a local_directory my_container:/app/
      
  6. Copying Directories with Docker cp:

    • Description: Copy entire directories from the host to a container or vice versa.
    • Code: Example of copying a directory from host to container:
      docker cp local_directory my_container:/app/
      
  7. Docker cp and Volumes:

    • Description: docker cp can also copy files to and from volumes associated with containers.
    • Code: Example of copying to a container volume:
      docker cp local_file.txt my_container:/app/volume_file.txt
      
  8. Using Docker cp in Docker Compose:

    • Description: Docker Compose supports docker cp for containers defined in the docker-compose.yml file.
    • Code: Example of using docker cp with Docker Compose:
      docker-compose cp local_file.txt my_service:/app/container_file.txt
      
  9. Interactive Mode with Docker cp:

    • Description: Use docker cp interactively by specifying -i for interactive mode.
    • Code: Example of interactive mode:
      docker cp -i local_file.txt my_container:/app/container_file.txt