How To Mount Same Volume To Multiple In Docker
Suppose, I have an apache container running and I need to create more apache containers using the same data volumes. The mounted drive name is dockerdata. We can use the same volume and mount it in a docker container. Let’s see, how to mount the same volume in a docker container.
First Apache Container:
docker run -itd --name apachecon -p 80:80 -v /root/dockerdata:/var/www/html/ 470671670cac /bin/bash docker ps
You can access the apache container using the below command and if you want to exit the container press ctrl+p+q otherwise the container would be stopped if you directly exist using exit command.
docker attach apachecon
Once you’re inside the container, you can start apache service using /sbin/httpd -D start
Now it’s time to test the apache website hosting.
cd /var/www/html/ echo " testing website " > index.html
you can find the IP address using ip a or ipconfig command and then run curl 172.17.0.2.
Check the index.html file exist on your mount point and I’m also able to access the website outside the container.
Everything is working perfectly and I will create a new apache container using the same mount volume.
docker run -itd --name apachecon1 -p 81:80 -v /root/dockerdata:/var/www/html/ 470671670cac /bin/bash
Create a directory inside /var/www/html using mkdir test.
I’m able to store data in the same mounted directory for all the running containers.
As you see both containers are running.
You’re done docker volume mount volume use in container