Lab : Docker storage

Q6: Run a mysql container again, but this time map a volume to the container so that the data stored by the container is stored at /opt/data on the host.
Use the same name : mysql-db and same password: db_pass123 as before. Mysql stores data at /var/lib/mysql inside the container.

I tried the newer --mount option,

$ docker run -d --name=mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 --mount type=bind source=/opt/data target=/var/lib/mysql mysql

However this error with the below error,
invalid argument “type=bind” for “–mount” flag: target is required

OR

$ docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 --mount type=bind,source=/opt/data,target=/var/lib/mysql mysql
docker: Error response from daemon: invalid mount config for type “bind”: bind source path does not exist: /opt/data.

How should we use this.

##Note: i used the -v option and that works all right.

For this question you can simply use the following command as shown in the hint

docker run -v /opt/data:/var/lib/mysql -d --name mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 mysql

But if you want to use mount option you can run the following command and it will work fine

docker run -d --name=mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 --mount type=bind,source=/opt/data,target=/var/lib/mysql mysql

Thanks.

Note that I did try the --mount command you mentioned, i.e.
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 --mount type=bind,source=/opt/data,target=/var/lib/mysql mysql

but it throws error

$ docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 --mount type=bind,source=/opt/data,target=/var/lib/mysql mysql
docker: Error response from daemon: invalid mount config for type “bind”: bind source path does not exist: /opt/data.

yes since you don’t have a directory named “data”
but if you try to make directory it will work fine

cd /opt
mkdir data
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 --mount type=bind,source=/opt/data,target=/var/lib/mysql mysql