How to copy files from host to Docker container?

前端 未结 30 2491
南方客
南方客 2020-11-22 06:58

I am trying to build a backup and restore solution for the Docker containers that we work with.

I have Docker base image that I have created, ubuntu:base

30条回答
  •  盖世英雄少女心
    2020-11-22 07:30

    Docker cp command is a handy utility that allows to copy files and folders between a container and the host system.

    If you want to copy files from your host system to the container, you should use docker cp command like this:

          docker cp host_source_path container:destination_path
    

    List your running containers first using docker ps command:

      abhishek@linuxhandbook:~$ sudo docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              
      PORTS               NAMES
      8353c6f43fba        775349758637        "bash"              8 seconds ago       Up 7 
      seconds                            ubu_container
    

    You need to know either the container ID or the container name. In my case, the docker container name is ubu_container. and the container ID is 8353c6f43fba.

    If you want to verify that the files have been copied successfully, you can enter your container in the following manner and then use regular Linux commands:

            docker exec -it ubu_container bash
    

    Copy files from host system to docker container Copying with docker cp is similar to the copy command in Linux.

    I am going to copy a file named a.py to the home/dir1 directory in the container.

            docker cp a.py ubu_container:/home/dir1
    

    If the file is successfully copied, you won’t see any output on the screen. If the destination path doesn’t exist, you would see an error:

            abhishek@linuxhandbook:~$ sudo docker cp a.txt ubu_container:/home/dir2/subsub
            Error: No such container:path: ubu_container:/home/dir2
    

    If the destination file already exists, it will be overwritten without any warning.

    You may also use container ID instead of the container name:

            docker cp a.py 8353c6f43fba:/home/dir1
    

提交回复
热议问题