DEV Community

Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on

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 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
Enter fullscreen mode Exit fullscreen mode

now let's inspect:

docker image inspect mysql
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Image description

Now lets inspect the container using

docker container inspect mysql
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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.  
Enter fullscreen mode Exit fullscreen mode

Top comments (0)