Will docker cp command work for copying files from host to a Container?

Hi,
I have tried to copy files from a Container to host using docker cp command. It copied successfully.
Then I have tried the same command by changing the arguments to copy from host to Container but no luck.
Showing an error: Error: Path not specified

My question- Is docker cp command is only for copying files from container to host? If yes, please give some ideas to copy from host to container.

Thanks in adv,
Vishwa

Unfortunately cp only works from a container to the host.
The best workaround I know to copy from the host to a container is to mount the file read-only at run time. If you need the file to be available permanently, then copy it into another location within the container so that it gets added to the container’s file system (instead of only being available as a mounted host volume).

You can workaround this with cat and pipes:

$ cat /local/file/path | docker exec -i <running-container-id> sh -c 'cat > /inside/docker/file/path'

Add tar and you will be able to copy folders as well.

Another way is to use ssh/rsync, but this requires ssh server or client inside of your container.

There will be support for this most likely.
There is an open PR to add this feature.
And really it would be support for copying files from the client to a container (so this could be a remote user).

If you are already on the host, you can technically just plop the files into the container fs manually.

Thanks Vlad. It worked.

-Vishwa

Hi Brian,
Can we expect this feature in the next release?

-Vish

The ability to directly copy from the host file system or a stream to a container became available in Docker 1.8 with the merging of PR #13171. With this new feature, docker cp offers the scaffolding to:

  • Copy files between containers.
  • Create an image by copying files from one or more host files, containers, or other images.
  • “Squash” existing images by copying them to a new image.
  • Create minimally sized images by copying only the necessary file system artifacts, from existing host file(s), container(s), and/or image(s) to a new one.

However, these indirect features require composing several Docker CLI commands to realize their implementation, along with code to enable copying from more than a single source and recover gracefully from errors. Therefore to encapsulate their implementation, a bash encoded CLI was written by me (not affiliated with Docker) and is available via the github project dkrcp.