Tuesday, July 14, 2015

How to create file between container and host in Docker

https://medium.com/@gchudnov/copying-data-between-docker-containers-26890935da3f


Copying data between Docker containers


When running docker there are use-cases when you need to copy files and folders into the container or between containers.

There is a `docker cp` command available since the Docker 1.0 that allows you to copy files and folders out of the container. However, if you need to copy from the host to a container or between containers you’re out of luck now. At least, `docker cp` doesn't support that.

One could create a new image each time or mount a data volume, but it is much faster to copy a bunch of files to the running container.

Docker 1.7.0 should have an extended `docker cp` command to support copying data to containers. Until that, you can use one of the alternative solutions.
In this article, I present you a workaround that relies solely on `docker cp` and `docker exec` to partially fill-in the feature we’re missing.
We consider three file copy scenarios:
  • from a container’s filesystem to the host path (available, Docker 1.0)
  • from the host path to a container’s filesystem (upcoming, Docker 1.7)
  • from one container to the other (upcoming, Docker 1.7)
You can skip the implementation details below and get the source code at the bottom of the article.

http://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container

The cleanest way is to mount a host dir on the container before running your command.

{host} docker run -v /path/to/hostdir:/mnt $container
{host} docker exec -it $container bash
{container} cp /mnt/sourcefile /path/to/destfile

- or -

$ cd /tmp/somefiles
$ tar -cv * | docker exec -i elated_hodgkin tar x -C /var/www
 
- or -
 
tar -cf - foo.sh | docker exec -i theDockerContainer /bin/tar -C /tmp -xf -

Copies the file foo.sh into /tmp of the container.


 
 

No comments: