Skip to content
Docker

icon picker
Docker Data Management

Persistent Data problem is a huge issue in case of docker containers. Preventio/n of this can be done using two ways:
Volumes
Bind Mounts

Data Volumes

Volumes are stored in a part of the host file system which is managed by docker. Volumes are created and managed by containers. We can also manually create volumes.
List all Volumes
docker volume ls
Starting a mysql container with custom volume name
docker run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True --mount source=mysql-db,destination=/var/lib/mysql mysql
Connecting to mysql container created above
docker exec -it mysql(container name) mysql -uroot -p

We can also connect using mysql

mysql -u root -p <password> -h <hostIP> -P <port>
mysql -u root -p mypassword -h 172.0.0.20 -P 3306

Bind Mounts

File or directory in host machine which is mounted on the container. Basically mapping of host files with container files.
Main diff btw bind mounts and volumes is any non docker process on the host machine can access and make changes to the files or directory in the case of bind mounts but in volumes this is not possible.
Another diff is bind mounts cannot be used in Dockerfile.
Use cases of bind mounts:
Sharing config files from host machine to containers.
Sharing source code or build artifacts btw development env on the docker host and a container.

Start an Nginx with bind mount
docker container run -d --name nginx --mount type=bind,source=$(pwd),target=/app nginx

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.