In Docker, Volumes are a mechanism used to store data generated by the container, this mechanism can help you restore your data in case of a container failure or unwanted shut down of the container.
The way they work is by specifying a path on the host where the container can persist its data and there are different ways to mount a volume
1 - Using Docker run
docker run -v path_host:/path_container image-name
docker run -v ~/backup:/var/www/html my_app
2 - Using Docker compose
In the docker-compose.yml file, a typical volume setup might look like this:
mysql: image: mysql:5.7 container_name: mysql ports: - 3306:3306 volumes: - "./backup/mysql:/var/lib/mysql/"
In this example, we create a MySQL container with a volume to back up the database.
Notice the colon
: That separates two paths.
- The left side of the colon represents your local folder as a relative path.
- The right side represents a folder in your Docker container.