Mount Volume in a container is very important if you want to save your data files. Suppose because of some reason your container stopped working then how will you get your data from the container. That not possible without Volume mapped to your container. We can create and mount a volume in a docker container.
Check Out: Take Backup of a Container
How To Mount Volume To Apache Container:
Today I will show you how to mount volume via command line in a container. I have created an Apache image and with the help of this image, I created an Apache container. I have a directory where I kept this dockerfile.
Step 1: Build an image
docker build -t dockerfile .
As you can see the image has been build with Image ID “f5a7cd5c3bc0“.
Check Out: Create A Container
Step 2: Create a Container using this image id “ f5a7cd5c3bc0 “
I created a Volume on docker host with the name of “vlom” and mounted it with /var/www/html where my data files will reside.
docker run -itd --name apachetest -p 80:80 -v vlom:/var/www/html f5a7cd5c3bc0 /bin/bash
Step 3: Attach the running container
docker attach b9947aefe406
Once you’re inside the container using
docker exec -it apachetest /bin/bash
Create a file inside the directory /var/www/html/
Step 4: Exit from the container and check that abc file should be in vlom directory where it’s mounted.
Check Out: Signup For Docker Hub
You can see that abc file is here. So now you do not need to worry about your data files. they’re safe on the mounted directory.