Transferring Files Between host and container

Docker provides convenient ways to copy files between host and container. Open another Docker Quickstart Terminal. If your host is GNU/Linux, just open another terminal.

To copy file from container to host, issue the following command in the host terminal:

docker cp CONTAINER:SRC_PATH HOST_PATH

where

  • CONTAINER is the name of the container
  • SRC_PATH is the path of the file in container to copy
  • HOST_PATH is the path of the host to copy to

For example, the following command will copy a file in the container to a Windows path:

docker cp ics-vm:/home/ics/a.txt /d/temp

Note that you can not specify the path in Windows as D:\temp, since : in docker cp command has special meaning.

To copy file from host to container, issue the following command in the host terminal:

docker cp HOST_SRC_PATH CONTAINER:DEST_PATH

where

  • HOST_SRC_PATH is the path of the host file to copy
  • CONTAINER is the name of the container
  • DEST_PATH is the path in the container to copy to

For example, the following command will copy a folder in Windows into the container:

docker cp /d/myfolder ics-vm:/home/ics

Have a try!
  1. New a text file with casual contents in the host.
  2. Copy the text file to the container.
  3. Modify the content of the text file in the container.
  4. Copy the modified file back to the host.

Check whether the content of the modified file you get after the last step is expected. If it is the case, you are done!

However, you may encounter error when you are trying to save the modification in vim in step 3 above:

E45: 'readonly' option is set (add ! to override)

According to the message, the file is read-only, but you may use ! to force saving. Type

:w!

But you receive another error message this time (assuming the file name is a.txt):

"a.txt" E212: Can't open file for writing

It seems that you do not have the permission to write to this file. Type

:q!

to exit vim without saving. Back to shell, type

ls -l

to display detail information of the files. You will see a list like

total 4
-rw-r--r-- 1 root root   13 Sep  1  2016 a.txt

Here are some explanations of what the first column (for example, -rw-r--r--) of the list means. For more information about what each column means, please search the Internet.

You can see that the a.txt file is owned by root, and you do not have permission to modify it. This is because files copy into container will be owned by root. To change the owner of the copied files, issue the following command in the container:

sudo chown -R username a.txt

where username is your username in the container. This time you should modify the file successfully. So, remember to change the owner of files after copying them into the container.

results matching ""

    No results matching ""