Hands-On Demo With Persistent Data
Tags: tutorial, docker, kubernetes, beginners
Published: 2021-06-30 20:23:11

This is a simple, and beginner-friendly demo to analyze and visualize the data on running containers and [**where actually the data on the running containers go**](https://dev.to/noviicee/where-does-the-data-go-2329)

We first need to create an image in order to build it and run it.
It can be any simple image. If you are new to Docker and Docker Images, you can refer to [here](https://github.com/noviicee/Docker/tree/main/PersistentDataDemo) for the meanwhile and use the files provided if you face issues in creating a docker image or writing a docker file.

Now, the motto is to check [**where does the data go**](https://dev.to/noviicee/where-does-the-data-go-2329) after a running container is killed.

>*Note: I am on Ubuntu 20.04.*
*The source code for the files that I am using is [here](https://github.com/noviicee/Docker/tree/main/PersistentDataDemo/Persistent-Data)*

**Here, we will be covering three cases-**
* mysql, no mount, no volume ---> data is lost after killing the container
* mysql, mount &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ---> data is kept in the local file
* mysql, volume &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ---> data is reserved in the volume

Open up your terminal and navigate to your working directory, i.e, the folder where a ***Dockerfile*** exists, so you can run it.

<hr>

## 1. No mount, No volume
   * Expected:
     Data is lost after killing the container
    
   * Execution:
     Run the following command in your terminal-
     `docker run -d -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql`

![3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7psuge4w0wav6tk8y4z.png)

As we can see, the necessary files are being downloaded and the image is getting build.

Next, we need to get the container-id of the running container.
Use `docker -ps` to list all the running containers on your system.

![4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fosyzb867csuj3thq1sj.png)

Here, I just have a single container so the output shows the id of a single running container.

Now that we have our container up and running, we can run the following command in order to **attach the image to the container**:
`docker exec -it CONTAINER-ID mysql`
Once we are into mysql, we can create a database, and list it by using-
`create database DB_NAME; show databases; exit`

![5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19zexk654hhg4l5tvo5n.png)

![6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e9481jw8pg5hxjwu6jb0.png)

Now you will see that a database is created.
Inside mysql, you can type `/c` to clear out the inputs and `exit` in order to exit from mysql.
To check whether the database is actually created or not, just `ls -a` in your working directory, and it will show you your created database.

Next, for checking *where does the data go*, we need to **delete the container**. Use the container-id by executing `docker -ps` and kill the running container by executing `docker kill CONTAINER_ID`.

The container is now killed. This can also be verified by executing `docker -ps` on the terminal, which shows that no container is up at the current moment.

![7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wi6iru3qzbyaiy5lobi.png)

Now, if you execute `ls -a` in your working directory, you will not be able to see the database or any other file that you might have created.

![8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3nhs9k8452oijv55ij0z.png)

Hence, by using No mount, No volume, all the data is lost after killing the container.

## 2. Mount
   * Expected:
     Data is kept inside of the local folder.
    
   * Execution:
     Run the following command in your terminal-
     `docker run -d -e MYSQL_ALLOW_EMPTY_PASSWORD=true -v$(pwd)/mysql-data:/var/lib/mysql mysql`

Next step is the same- we need to get the container-id of the running container.
Use `docker -ps` to list all the running containers on your system.

![9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mpqkjxut4v31k13aev5f.png)

Now that our container is up and running, we can run the following command in order to **attach the image to the database**:
`docker exec -it CONTAINER-ID mysql`
Basically the tag `-it` is used frequently to tag images in docker.

![10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uhim5tlt2ggh6dv8zn98.png)

Once we are into mysql, we can **create a database, and list the database** by using-
`create database DB_NAME; show databases; exit`

![11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8afazdq6uhqm7cax36u.png)

To check whether the database is actually created or not, just `ls -a` in your working directory, and it will show you your created database.

![12](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/myxuri3jnqmczid9n0w6.png)

In order to check for the data, we **delete the container**. Use the container-id by executing `docker -ps` and kill the running container by executing `docker kill CONTAINER_ID`.

The container is now killed. This can be verified by executing `docker -ps` on the terminal, which shows that no container is up at the current moment.
The full flow can be seen in the snippet below-

![13](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffwheuil87nc4xbkhc4c.png)

Now, if you execute `ls -a` in your working directory, you will notice that the created database still exits in the local folder.

![14](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5o9uaz00vb4x32yqtba.png)

Hence, by using Mount, the data is kept in the local folder even after the container has been killed.

## 3. Volume
   * Expected:
     Data is preserved inside the volume.
    
   * Execution:
     Run the following command in your terminal-
     `docker run -d -e MYSQL_ALLOW_EMPTY_PASSWORD=true -v mysql/data:/var/lib/mysql mysql` in order to start up and run the container.

The next steps are the same as the above two, that is, to get the container-id of the running container using `docker -ps` and then **attach to the database** using the command-
`docker exec -it CONTAINER-ID mysql`

![15](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/myyya28ppcatty0crpm0.png) ![16](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcp8pfq4zcccrpghurka.png)

Now that we are into mysql, we can **create a database, and list the database** by using-
`create database DB_NAME; show databases; exit`

![17](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmkun3ebbeap8dbclx9d.png)

And the database is created.
You can type `/c` to clear out the inputs inside mysql and `exit` in order to exit from mysql.

Now we want to check for the data existence, so we **delete the container**. Use the container-id by using `docker -ps` and kill the running container by executing `docker kill CONTAINER_ID`.

![18](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfmddjdb49q7glo1y7gp.png)

The container is now killed. This can be verified by executing `docker -ps` on the terminal, which shows that no container is up at the current moment.

Now, if you will want to check for the data in the volume-

By simply typing **volume** on the terminal, we will see that there are a bunch of options provided for us in order to help use the *volume* command-

One of those is the `volume ls`. This command lists all the volumes present on our local machine.
We just execute `volume ls` on the terminal. In the output, we can see that our created database ***mysql-data*** still exists even after deleting the container.

![19](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d42n06wq4eujw6m4p4co.png)

Another command that can be used here is the `volume inspect VOLUME-NAME`
On executing the above command, it will display the details on the particular volume. It can be seen in the snippet below-

![20](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57d56td19l8kwale4245.png)

Hence, by using Volume, we can see that the data is kept preserved in the volume even after the container has been killed.

<hr>

This was pretty much for analyzing and getting a basic idea of the persistent-data on running containers.

If you are new to the topic you can refer to -> [Where does the data go](https://dev.to/noviicee/where-does-the-data-go-2329)
New to Docker and Containers? Refer to -> [Beginner's guide to Docker](https://dev.to/noviicee/beginner-s-guide-to-docker-42co)

>*Some additional resources for help:*
[Inspect Docker Volume](https://docs.docker.com/engine/reference/commandline/volume_inspect/)
[Digital Ocean on Docker Volumes](https://www.digitalocean.com/community/questions/how-to-check-the-disk-usage-of-all-running-docker-containers) <br>

Thanks for reading :)