Docker

Share Folder Between Docker Container and Host Machine

Docker is one of the most popular DevOps tool using many companies. This is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data centre VMs, or the cloud.

Many times we came to the situation where we need to share a folder between running Docker containers and the Host machine. This approach is useful when we want to copy/share some files from Docker containers to the Host machine.

Let say we have generated some reports inside Docker containers and we need to get it available easily on the Host machine, in this situation we need to mount folder in between Docker containers and the Host machine.

I am considering, you have already got docker container is up and running and you need to just share folder in between running container and the host machine.

Shared folder using single Docker run command

we can use -v flag as below

 docker run-p 80:80 \
-v /home/rohan/reports:/var/www/reports \
--name=nginx \
-d server1 

Where

  • -v This flag tells docker that, there is a folder on my host machine (/home/rohan/reports) which needs to be share with Docker container (/var/www/reports). That’s it Now if you generated any files in this reports folder it will be accessible for both docker containers and the host machine.

Shared folder using a docker-compose command

In docker-compose, you need to mention -v flag as below

server1: build: nginx
volumes:
- /home/rohan/reports:/var/www/reports

and now folder has been shared in between docker container and the host machine.

Leave a comment