How to copy data from host to docker container.?

Docker is an open platform for developing, shipping, and running applications. Docker is designed to deliver your applications faster. With Docker you can separate your applications from your infrastructure and treat your infrastructure like a managed application. Docker helps you ship code faster, test faster, deploy faster, and shorten the cycle between writing code and running code.

Docker does this by combining kernel containerization features with workflows and tooling that help you manage and deploy your applications.

Also See: Docker Basic Commands

Copy data From Docker Host to Docker Container

Now we will see how we can copy data from docker host to running docker container, there is a simple command for copying data lets see

docker cp file.txt 494cb2bc85b0:/opt
In above command we copied file.txt file in our running docker conatiner 494cb2bc85b0 in /opt directory now we can see in our opt directory that file in following image

Screen Shot 2016-08-26 at 3.32.39 PM

Copy Data while running Container

We can also copy data from docker host to docker container while we run docker container, we also call it mounting volume inside docker container. Let’s see how we can do that:

docker run -v /root/test -it centos /bin/bash

In above command we mounted /root/test directory in our centos container and we can get /test in our container, /test directory should be there in the directory where you execute this command. We can also give mount point in docker container also let’s see

docker run -v /root/test:/mnt -it centos /bin/bash

Now we have mounted /root/test directory inside our container on /mnt directory.

Mounting Volume in Dockerfile

Now we will mount a volume in Dockerfile lets get started, we need to add the following line in our docker file:

VOLUME [ /root/test ]

Now when we create a image from Dockerfile /root/test is already mounted in our image and also when we create container from this image we will get /root/test mounted in container..

With the use of above commands you can easily copy data from host to to our docker container..

If you face any problem feel free to comment below..

 

Leave a Reply

Your email address will not be published.


*