I have a docker compose file for a website, which amongst a bunch of other containers for various purposes, includes a mysql database that will have persistent data. At the moment the compose file specifies a relative path for the data, e.g.:
mysql: image: mysql:5.7 container_name: sqldb volumes: - ./mysql_data/_data:/var/lib/mysql
and the folder structure:
--mysql_data --static_content docker-compose.yml
which means that at any point I can move the whole site (including persisted content) to another server by copying the whole folder and running docker-compose up.
But reading about docker volumes it sounds like it is the preferred method (plus relative bind mount paths don't seem to be supported using "docker run", but work in compose) so I'm wondering if I need to change this approach to use volumes? Is there something inherently wrong with this relative binding approach? If I do switch to volumes, when moving the containers do I have to manually move the volumes (e.g. this method How to port data-only volumes from one host to another?)?
There are two types of volumes to consider: Named volumes have a specific source from outside the container, for example, awesome:/bar . Anonymous volumes have no specific source, therefore, when the container is deleted, you can instruct the Docker Engine daemon to remove them.
Stops containers and removes containers, networks, volumes, and images created by up .
Volumes are the preferred, and extensively used, mechanisms for persisting data generated by Docker containers. Docker volumes basically create a link between one of the local folders on the system and the folder on the docker container.
Though both methods are similar, there is a slight difference. Docker manages Volumes and is usually not affected by other processes running on the same host. In contrast, Bind Mounts are just directories on the host file system and may be modified by other processes other than docker.
There are four possible options to mount any volume:
Here is the example for above:
version: '3'
services:
sample:
image: sample
volumes:
- ./relative-path-volume:/var/data-two
- /home/ubuntu/absolute-path-volume:/var/data-one
- docker-volume-default-path-volume:/var/data-three
- docker-volume-absolute-path-volume:/var/data-four
volumes:
docker-volume-default-path-volume: {}
docker-volume-absolute-path-volume:
driver: local
driver_opts:
o: bind
type: none
device: /home/path/of/your/folder
Relative Path: ./relative-path-volume:/var/data-two
Absolute Path: /home/ubuntu/absolute-path-volume:/var/data-one
Docker Volume Default Path: docker-volume-default-path-volume:/var/data-three
Docker Volume with Absolute Path: docker-volume-absolute-path-volume:/var/data-four
This works for any server as we customize the volume device property to the respective directory path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With