Persistent data means information that is infrequently accessed and not likely to be modified.
SO, basically there are some data which we are not going to change. For example, if we go to mysql's repository:
You can see the Volume command . If we delete the mysql image, still we need to manually delete this Volume part. This
Volume command means where the volume will reside under a container.
Let's pull the mysql image using
docker pull mysql
now let's inspect:
docker image inspect mysql
Now, lets create a container using this image.
docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True mysql
Now lets inspect the container using
docker container inspect mysql
You can see that the volume resides under mount
SO, you can see that the location of the container shows /var/lib/mysql
but the data actually resides in /var/lib/docker/volumes/c266a73520c0c30bd758b9facba1677540dbbc1446edc7f7dc03cbda84be732c/_data
this link. Here you can see the volume folder under the docker folder ,right?
If we check the volumes using
docker volume ls
you can see the last volume is the volume that was created while we created the mysql container
THe volume is c266a73520c0c30bd758b9facba1677540dbbc1446edc7f7dc03cbda84be732c
You can also inspect the volume
We can not actually realize what is inside this volume. It's some sort of database though.
Now,lets stop our containers
Our mysql container is stopped.
Now lets check the volume
Still all the volume is there. Although there is no container up and running.
Lets remove our container
And check the volume
The volumes are still there.
NOw, lets create a new container where we will specify the volume name. The reason is that, from the volume names we can not know actually which container it refers . We don't want any problem in future. Thus, lets create a container with a volume command (-v)
docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql
We are using /var/lib/mysql
which is already used by the container & we saw it previously
but we have added mysql-db:
. THis is the naming format to specify a volume.
Now lets check the volumes
You can see the mysql-db
volume now which refers to our newly created container. Let's inspect the volume
Now lets inspect the container we created
docker container inspect mysql
Under the mount, you can see the volume
This time the Source has a volume id mysql-db
.
We defined the volume ID & destination while created the container, remember?
Also, you can create volumes before creating a container. Use the --help
command to see which commands you can use along with create commands
docker volume create --help
Now .,lets get into dockerfile-sample-2 which we cloned from this repository
We can see what are inside the folder
If you look into the dockerfile, you can see something like this
You can see that we have used nginx image and copied index.html into the working directory.
Now , lets create a container using the same location
docker container run -d --name nginx-container -p 80:80 -v $(pwd):/usr/share/nginx/html nginx
after the -v command (means volume) , we uses $(pwd) to print the working directory and we pasted the link of the working directory
WE have created our container
The container is up and running
So, this is the custom nginx container we created.
Also we can create a normal container which is not customized.
docker container run -d --name nginx-container2 -p 8080:80 nginx
You can see this one available on the localhost:8080 server
Note:
Bind Mount: This is what is used when you are trying to map the files from a directory on the host into a directory in the container.
Top comments (0)