SIGN IN / UP
    opened image

    As we saw from the previous articles, Docker can simplify the deployment of different applications and their execution.

    But sometimes you need to send files from the host machine to a Docker container, and from the container to the host.

    In this article, we will see how to do this.

     


    The following commands assume that you already have Docker installed. If not, you can learn how to do it in the articles: "How to install Docker on Centos 7" or "How to install Docker on Ubuntu 20.04".
     

    Copying files from the host system to the Docker container.


    To copy to and from Docker, we will use the docker cp utility.

    The docker cp utility copies the contents of the source to the destination.

    You can copy from the container's file system to the local computer or vice versa, from the local file system to the container.


    Example: Local host -> Container

    docker cp <path to file on host machine> containerid:<path to file in container>
    


    Example: Container -> Local host

     

    docker cp containerid:<path to file on host machine> <path to file on host machine>
    



    Let's see this in action with a simple example of a Docker image of Nginx.

    We can use the following command:

     

     

    docker run -d --name nginx_copy nginx
    

     


    where:
    nginx_copy - container name (you can use any)
    nginx - image name on Docker Hub that we will use


    If you don't have this container on your server, wait for it to be downloaded. 


     

    Now let's enter the container to see/find where or which file to copy.

     

     

    docker exec -it nginx_copy /bin/bash
    

     


    Now let's display the contents of the /etc/nginx folder in the terminal.

     

     

    ls -la  /etc/nginx/
    

     


    For example, let's say we need to copy the nginx.conf configuration file and we know that it is located at /etc/nginx/nginx.conf. To do this, enter the following command:

     

     

    docker cp nginx_copy:/etc/nginx/nginx.conf ~/nginx.conf
    

     


    Where:

    nginx_copy - the container from which we are copying;
    /etc/nginx/nginx.conf - the path to the file to be copied;
    ~/nginx.conf - the destination and file name (you can specify a different file name)

    Now let's see the result:

     

     

     

    ls -la
    

     



    As we can see, the file was successfully copied to the host. Now we can modify it and then upload it back.

     

     

    How to copy a file to a container.


    Suppose we now need to copy the modified nginx.conf file back to the container. To do this, we can use the following construction:

     

     

     

     

    docker cp ~/nginx.conf nginx_copy:/etc/nginx/nginx.conf_cp
    

     


    If everything is entered correctly, we will be able to see this file in the container.

    Enter the container:

     

     

    docker exec -it nginx_copy /bin/bash
    

     


    And display the contents of the /etc/nginx directory:

     

     

    ls -la
    

     



     

    As we can see, the new file has been copied inside the container.

    Using the same principle, you can transfer data from the container to the host and vice versa.