Docker Volumes

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

00:30:00

Difficulty

Beginner

You are working on a project and need to deploy a MySQL container to the development environment. Because you will be working with mock customer data that needs to be persistent, the container will need a volume. Create a volume called mysql_data. Then deploy a MySQL container that will use this volume to store database files.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Docker Volumes

The Scenario

We need to deploy a MySQL container to our development environment. Because we will be working with mock customer data that needs to be persistent, the container will need a volume. Create a volume called mysql_data. Then deploy a MySQL container that will use this volume to store database files.

Log In

Log in to the environment using the credentials provided on the lab page either in a terminal session on your local machine or by clicking Instant Terminal.

Create a Volume Called mysql_data

First we'll use the docker volume command to create a volume called mysql_data:

[cloud_user@host]$ docker volume create mysql_data

Create a MySQL Container

Then we'll run the docker container command to create a MySQL container:

[cloud_user@host]$ docker container run -d --name app-database 
 --mount type=volume,source=mysql_data,target=/var/lib/mysql 
 -e MYSQL_ROOT_PASSWORD=P4ssW0rd0! 
 mysql:latest

Create a volume called mysql_data, then deploy a MySQL container called app-database. Use the mysql latest image, and use the -e flag to set MYSQL_ROOT_PASSWORD to P4sSw0rd0. Use the mount flag to mount the mysql_data volume to /var/lib/mysql. The container should run in the background.

We can confirm that everything worked with:

[cloud_user@host]$ docker container inspect app-database

Conclusion

Well, we got it done. Our developers now have a MySQL container to play with in their development environment. Congratulations!