docker run -d --name "mysql-1" -v "/var/lib/mysql" mysql
This command creates a new container from the mysql
image. It also creates a new data volume, which it then mounts in the container at /var/lib/mysql
. This volume helps any data inside of it persist beyond the lifetime of the container. That is to say, when a container is removed, its filesystem changes are also removed. If a database was storing data in the container, and the container is removed, all of that data is also removed. Volumes will persist a particular location even beyond when its container is removed.
It is possible to use the same volume in multiple containers with the --volumes-from
command line option:
docker run -d --name="mysql-2" --volumes-from="mysql-1" mysql
The mysql-2
container now has the data volume from mysql-1
attached to it, also using the path /var/lib/mysql
.