Search code examples
linuxdockercopy-paste

create the file or copy the file from docker container to host


Below is my code:

docker container run -it ubuntu /bin/bash -c "touch /root/test.txt"

What I wanted is this file to be created at my host. Can someone help me, please?


Solution

  • One option is to mount a directory from the host in the container. Touch the .txt file in the location where you created the mount, inside the container.

    This example will create on the docker host, in the working directory, the test.txt file:

    docker run -it -v $(pwd):/root/mout-from-host ubuntu /bin/bash -c "touch /root/mout-from-host/test.txt"
    
    ls
    test.txt <<--file on host, created by the container