In a Docker environment, masting data volumes is a must as data is not saved if it’s not mapped to any volumes. Using volumes will make it easier to back up and migrate to other systems. At the same time sharing among multiple containers is much safer.
Using volumes is often a better choice when you want to save persisting data in a container because a volume does not increase the size of the containers that it is using it.
Local-Volumes with docker-compose
A single docker-compose with a simple volume looks like this:
version: "2"
services:
guacamole:
image: oznu/guacamole:latest
container_name: guacamole
volumes:
- app_config:/config
ports:
- 9063:8080
restart: unless-stopped
volumes:
app_config:
The same volume can be created directly outside a compose with the command “docker volume create” and then referenced inside your docker-compose.yml file.
version: "2"
services:
guacamole:
image: oznu/guacamole:latest
container_name: guacamole
volumes:
- app_config:/config
ports:
- 9063:8080
restart: unless-stopped
volumes:
app_config:
external: true
SMB/NFS-Volumes with docker-compose
Using SMB or NFS share unlike a local volume will allow you to store your persisted data on a shared network drive without needing larger drive space on your Docker container system.
In docker-compose.yml, this is how to set up an SMB volume mount.
version: "2"
services:
guacamole:
image: oznu/guacamole:latest
container_name: guacamole
volumes:
- app_config:/config
ports:
- 9063:8080
restart: unless-stopped
volumes:
app_config:
driver_opts:
type: cifs
o: username=_myuser_,password=_mypass_,rw
device: //192.168.200.19/share/docker/guacamole
In docker-compose.yml, this is how to set up an NFS volume mount.
version: "2"
services:
guacamole:
image: oznu/guacamole:latest
container_name: guacamole
volumes:
- app_config:/config
ports:
- 9063:8080
restart: unless-stopped
volumes:
app_config:
driver_opts:
type: nfs
o: addr=192.168.200.19,rw
device: /share/docker/guacamole
Volumes Binding with docker-compose
The directory’s path on the host system is by default /var/lib/docker/volumes/_data, where a random ID assigned to the volume as its name is generally used when binging container volumes to the host system path.
version: "2"
services:
guacamole:
image: oznu/guacamole:latest
container_name: guacamole
volumes:
- ./config:/config
ports:
- 9063:8080
restart: unless-stopped
Or you can bind it to any folder on the docker system.
version: "2"
services:
guacamole:
image: oznu/guacamole:latest
container_name: guacamole
volumes:
- /path_to/contaniner/data:/config
ports:
- 9063:8080
restart: unless-stopped
Bind mount is discouraged when want to try to make the containers portable. Its recommend using SMB or NFS when managing docker containers’ persistent data.