Store Docker volume on external hard drive - docker

I am trying to store the data of my container on an 'external hard drive' (/dev/xvdd) that is mounted at /mnt/datadbs.
My docker-compose.yml looks like this:
version: "3":
services:
...
volumes:
prometheus-data:
driver: local
driver_opts:
type: btrfs
device: /mnt/dataebs
When I start the container, I am getting the following error:
ERROR: for prometheus Cannot create container for service prometheus: failed to mount local volume: mount /mnt/dataebs:/var/lib/docker/volumes/ubuntu_prometheus-data/_data: block device required
Can someone point me in the right direction? Eventually, I want to be able to store several docker volumes on the 'external hard drive'.

Try changing your named volume declaration type to "bind" instead of "btrfs".
So it would be something like this:
volumes:
prometheus-data:
driver: local
driver_opts:
type: none
device: /mnt/dataebs
o: bind
You can also bind mount directly in your service declaration, so something like this:
app:
image: appimage
ports:
- "8080:8080"
volumes:
- /mnt/dataebs:/container/path

Related

Mount nfs to container with docker compose

Does anyone know how to mount an NFS to a docker container using docker compose? I keep getting the same error each time I run 'docker compose up':
Error response from daemon: error while mounting volume '/var/lib/docker/volumes/test_data/_data': failed to mount local volume: mount /etc/hapee-2.2/certs:/var/lib/docker/volumes/test_data/_data, data: addr=10.15.50.27,nolock,soft: invalid argument
Exports file from the NFS appears to be set up correctly. I've tried deleting '/home/' from the file path as well. IP address is redacted.
/etc/hapee-2.2/certs <ipaddressofpc>(rw,sync,no_subtree_check,no_root_squash)
I keep suspecting the docker-compose.yml, but I'm not sure what the issue with it would be
version: '3.7'
services:
hapee:
image: haproxy:2.2
container_name: test
restart: always
ports:
- "80:80"
- "443:443"
- "8888:8888"
volumes:
- data:/etc/hapee-2.2/certs
logging:
options:
max-size: 100m
max-file: "3"
volumes:
data:
driver_opts:
type: "nfs"
o: "addr=10.15.50.27,nolock,soft,ro"
device: "/etc/hapee-2.2/certs"
Alternatively, does anyone know another method of mounting SSL certificates to an HA Proxy container with docker compose?
Thanks!

Folders for some Docker named volumes are empty?

I have a few Docker named volumes set up like this:
---
version: '3.8'
services:
broker:
...
volumes:
- volume_kafka:/var/lib/kafka
materialize:
...
volumes:
- volume_mzdata:/var/lib/mzdata
volumes:
volume_kafka:
driver: local
driver_opts:
type: none
o: bind
device: /var/lib/kafka
volume_mzdata:
driver: local
driver_opts:
type: none
o: bind
device: /var/lib/mzdata
When I go to /var/lib/mzdata on the host, it's the same as /var/lib/mzdata in the container. If I docker-compose down, then docker-compose up, everything in /var/lib/mzdata is the same as before.
However, /var/lib/kafka on host is missing all the files from /var/lib/kafka in the container. If I docker-compose down, then docker-compose up, some data is missing and there's a bunch of errors. It's not clear whether it reused /var/lib/kafka (but the files are invisible to me) or if it's storing the data elsewhere.
If the files on in /var/lib/kafka on the host, but it's invisible to me, how can I view them? sudo didn't work. If they're stored elsewhere, how can I find them?

How to specify volume host path in docker-compose?

I want to share a volume among multiple containers, and specify the path for this volume on the host.
I used the following settings:
version: '3'
services:
service1:
image: image1
volumes:
- volume1:/volume1
service2:
image: image2
volumes:
- volume1:/volume1
volumes:
volume1:
driver: local # meaning?
driver_opts:
o: bind # meaning?
type: none # meaning?
device: /volume1 # the path on the host
But I am not sure of the driver: local, type: none and o: bind options.
I would like to have a regular volume (like without specifying any driver nor driver_opts), just being able to specify the path on the host.
You're looking for a bind mount. Specifying the volumes key means that you're creating a volume in the Docker machine for persistent storage. Despite the name, a volume is not necessarily related to volumes.
Use something like:
version: '3'
services:
service1:
image: image1
volumes:
- type: bind # Host and Docker machines have identical views to the same directory; changes propagate both ways
source: . # Host machine directory to mount
target: /app # Docker machine directory to be mapped to

Docker Named Volumes

What's the right way to mix named volumes with and without local host path in docker compose v3?
This way I'm getting YML error:
volumes:
/mnt/volume-part1:/volume
conf:
vhost:
html:
certs:
Then I'd like to refer to volume inside containers...
For named volumes, you need to declare the volume name under the dedicated volumes section in the compose file. For a mount, you don't declare it in that section:
Consider the following compose file:
version: "3"
services:
db:
image: db
volumes:
- data-volume:/var/lib/db
- /mnt/volume-part1:/volume
volumes:
data-volume:
As you can see the named volume data-volume needes to be declared in the volumes section before being assiged to the container.
Whereas the directory mount is directly mounted onto the container.
UPDATE
If you don't want to replicate the machine path on all the container, you can use a clever trick to specify where exactly the named volume will be created as such:
version: "3"
services:
db:
image: db
volumes:
- data-volume:/var/lib/db
- volume-part1:/volume
volumes:
data-volume:
volume-part1:
driver_opts:
type: none
device: /mnt/volume-part1
o: bind
As you can see above, we have created a named volume volume-part1 and specified where this volume will be backuped on the host machine.

How to set a path on host for a named volume in docker-compose.yml

Example below creates dbdata named volume and references it inside db service:
version: '2'
services:
db:
image: mysql
volumes:
- dbdata:/var/lib/mysql
volumes:
dbdata:
driver: local
(from https://stackoverflow.com/a/35675553/4291814)
I can see the path for the volume defaults to:
/var/lib/docker/volumes/<project_name>_dbdata
My question is how to configure the path on host for the dbdata volume?
With the local volume driver comes the ability to use arbitrary mounts; by using a bind mount you can achieve exactly this.
For setting up a named volume that gets mounted into /srv/db-data, your docker-compose.yml would look like this:
version: '2'
services:
db:
image: mysql
volumes:
- dbdata:/var/lib/mysql
volumes:
dbdata:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/srv/db-data'
I have not tested it with the version 2 of the compose file format, but https://docs.docker.com/compose/compose-file/compose-versioning/#version-2 does not indicate, that it should not work.
I've also not tested it on Windows...
The location of named volumes is managed by docker; if you want to specify the location yourself, you can either "bind mount" a host directory, or use a volume plugin that allows you to specify a path.
You can find some details in another answer I posted recently; https://stackoverflow.com/a/36321403/1811501

Resources