Docker series (Part 11): Container lifetime & persistent data
Tags: docker
Published: 2022-07-10 16:50:38

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](https://github.com/docker-library/mysql/blob/master/5.7/Dockerfile.debian):
You can see the Volume command . If we delete the mysql image, still we need to manually delete this Volume part. This 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8kivzkqwu7hjo40ixwn4.png)

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

Let's pull the mysql image using 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7cjtd60kvjva336r1ul3.png)

```
docker pull mysql
```
now let's inspect:

```
docker image inspect mysql
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1v8olp9xo1ycs5xibirj.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fks4pkzuw1repnnp656b.png)

Now lets inspect the container using

```
docker container inspect mysql
```
You can see that the volume resides under mount

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v15otkfxggovk1p7zxll.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qq41ntooz20hkcbwc5ri.png)

THe volume is `c266a73520c0c30bd758b9facba1677540dbbc1446edc7f7dc03cbda84be732c`
You can  also inspect the volume

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ml63scq45y57j6hcmd95.png)

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

Now,lets stop our containers 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1f8p22gfoqeq4oxtoxw.png)
Our mysql container is stopped. 

Now lets check the volume

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9owos1jyebd8lq99d41.png)
Still all the volume is there. Although there is no container up and running.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21c239kj24jts3ge7nct.png)

Lets remove our container

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mg46s3pyyu72o9hv62nl.png)

And check the volume

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pwm7yesnz9fxcywmmovp.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tyjstm24j1j47ga9yi0t.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2czyhmg61t86j03yqoky.png)

Now lets check the volumes

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qx7fjg3vuft8xthuy0yg.png)
  
You can see the `mysql-db` volume now which refers to our newly created container. Let's inspect the volume


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2r4miqjn2q86p9jkhzv.png)
 
Now lets inspect the container we created

```
docker container inspect mysql

```
Under the mount, you can see the volume
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4yblhetnmcelnpteau0.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erx01lebrj3z4gdhz5dv.png)
 
     
 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](https://github.com/bretfisher/udemy-docker-mastery)

We can see what are inside the folder

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d03exd8uxf1mxh4fnxf5.png)
If you look into the dockerfile, you can see something like this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2cbwzasm8aic88qowp6x.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dqf5le5c7bn4evt53cl.png)

WE have created our container

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o0bhw82vwzkjoxsdi80y.png)
The container is up and running

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8989bsfzrwvhnvs1uy5.png)
  
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arblesjsm4iyx6vptbwn.png)
You can see this one available on the localhost:8080 server

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtomc7r3sg4jioiztrte.png)

```
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.  
```