Docker series (Part 11): Container lifetime & persistent data

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 dele…


This content originally appeared on DEV Community and was authored by Md Shahriyar Al Mustakim Mitul

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

Image description

Volume command means where the volume will reside under a container.

Let's pull the mysql image using

Image description

docker pull mysql

now let's inspect:

docker image inspect mysql

Image description
You can see the volume part.

Now, lets create a container using this image.

docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True mysql

Image description

Now lets inspect the container using

docker container inspect mysql

You can see that the volume resides under mount

Image description
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
Image description

THe volume is c266a73520c0c30bd758b9facba1677540dbbc1446edc7f7dc03cbda84be732c
You can also inspect the volume

Image description

We can not actually realize what is inside this volume. It's some sort of database though.

Now,lets stop our containers

Image description
Our mysql container is stopped.

Now lets check the volume

Image description
Still all the volume is there. Although there is no container up and running.

Image description

Lets remove our container

Image description

And check the volume

Image description
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

Image description

but we have added mysql-db: . THis is the naming format to specify a volume.

Image description

Now lets check the volumes

Image description

You can see the mysql-db volume now which refers to our newly created container. Let's inspect the volume

Image description

Now lets inspect the container we created

docker container inspect mysql

Under the mount, you can see the volume
Image description

This time the Source has a volume id mysql-db .

Image description

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

Image description
If you look into the dockerfile, you can see something like this

Image description
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

Image description

WE have created our container

Image description
The container is up and running

Image description

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

Image description
You can see this one available on the localhost:8080 server

Image description

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.  


This content originally appeared on DEV Community and was authored by Md Shahriyar Al Mustakim Mitul


Print Share Comment Cite Upload Translate Updates
APA

Md Shahriyar Al Mustakim Mitul | Sciencx (2022-07-10T16:50:38+00:00) Docker series (Part 11): Container lifetime & persistent data. Retrieved from https://www.scien.cx/2022/07/10/docker-series-part-11-container-lifetime-persistent-data/

MLA
" » Docker series (Part 11): Container lifetime & persistent data." Md Shahriyar Al Mustakim Mitul | Sciencx - Sunday July 10, 2022, https://www.scien.cx/2022/07/10/docker-series-part-11-container-lifetime-persistent-data/
HARVARD
Md Shahriyar Al Mustakim Mitul | Sciencx Sunday July 10, 2022 » Docker series (Part 11): Container lifetime & persistent data., viewed ,<https://www.scien.cx/2022/07/10/docker-series-part-11-container-lifetime-persistent-data/>
VANCOUVER
Md Shahriyar Al Mustakim Mitul | Sciencx - » Docker series (Part 11): Container lifetime & persistent data. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/10/docker-series-part-11-container-lifetime-persistent-data/
CHICAGO
" » Docker series (Part 11): Container lifetime & persistent data." Md Shahriyar Al Mustakim Mitul | Sciencx - Accessed . https://www.scien.cx/2022/07/10/docker-series-part-11-container-lifetime-persistent-data/
IEEE
" » Docker series (Part 11): Container lifetime & persistent data." Md Shahriyar Al Mustakim Mitul | Sciencx [Online]. Available: https://www.scien.cx/2022/07/10/docker-series-part-11-container-lifetime-persistent-data/. [Accessed: ]
rf:citation
» Docker series (Part 11): Container lifetime & persistent data | Md Shahriyar Al Mustakim Mitul | Sciencx | https://www.scien.cx/2022/07/10/docker-series-part-11-container-lifetime-persistent-data/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.