docker share OS volume between services - docker-compose

This is an extract from Volume configuration reference, in docker docs.
version: "3"
services:
db:
image: db
volumes:
- data-volume:/var/lib/db
backup:
image: backup-service
volumes:
- data-volume:/var/lib/backup/data
volumes:
data-volume:
Can I have something similar mapped to a custom directory, instead of /var/lib/docker/volumes/?
Something like this:
version: "3"
services:
db:
image: db
volumes:
- data-volume:/var/lib/db
backup:
image: backup-service
volumes:
- data-volume:/var/lib/backup/data
volumes:
data-volume: /home/user/db

It is possible. See https://github.com/docker/docker/issues/19990#issuecomment-248955005
You will have to create the volume with docker cli.

Related

Database is not recreated in docker after removing the volume

After I delete the volume of the db service by running docker volume rm <volume_name>, the database is not recreated when I am rebuilding the container by docker-compose up -d --build. What is the reason ?
My docker-compose file looks like this:
version: "2"
services:
db:
image: 'postgres:latest'
ports:
- "5432:5432"
env_file:
- .env
volumes:
- db:/var/lib/postgresql/data:rw
restart: "always"
volumes:
db: {}

Why can't I use azure file share in my docker-compose to store mongodb data in Azure Container Instance?

When using a docker ACI context, the following docker-compose file fails. The mongodb container continuously restarts.
version: "3.9"
services:
mongodb:
image: mongo:5.0.6
env_file: mongo.env
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=changeit
ports:
- 27017
volumes:
- dbdata:/data/db
volumes:
dbdata:
driver: azure_file
driver_opts:
share_name: mongodb-data
storage_account_name: kpncoqyuxumoetuftz
If I don't use the azure_file storage it will run ok (But of course the data won't be persistent)
I am not sure why I can't mount to the default directory /data/db but to get this to work I had to mount to a different directory and then replace the default command with one that takes a parameter.
Working version is below:
version: "3.9"
services:
mongodb:
image: mongo:5.0.6
command: ["mongod", "--dbpath=/dbdata"]
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=changeit
ports:
- 27017
volumes:
- dbdata:/dbdata
volumes:
dbdata:
driver: azure_file
driver_opts:
share_name: mongodb-data
storage_account_name: kpncoqyuxumoetuftz

Docker-compose volume error: incorrect format, should be external:internal[:mode]

I have a docker-compose yml file which is giving me the following error:
ERROR: Volume <path>db:/db:rw has incorrect format, should be external:internal[:mode]
I've seen multiple posts about this issue but never a resolution. Working on macOS with the following yml file:
volumes:
db:
rdb:
services:
volumes-provisioner:
image: hasnat/volumes-provisioner
environment:
PROVISION_DIRECTORIES: "1001:1001:0755:/db"
volumes:
- "./db:/db:rw"
volumes-provisioner2:
image: hasnat/volumes-provisioner
environment:
PROVISION_DIRECTORIES: "999:999:0755:/data"
volumes:
- "./rdb:/data:rw"
redis:
image: "redislabs/redisgraph:2.2.6"
ports:
- "6379:6379"
volumes:
- "./rdb:/data:rw"
depends_on:
- volumes-provisioner2
insight:
image: "redislabs/redisinsight:1.7.1"
depends_on:
- volumes-provisioner
- redis
volumes:
- "./db:/db:rw"
ports:
- "8001:8001"
Any ideas?
It's probably your version of docker. Make sure you've upgraded to latest, then add a docker version statement to the top of your file (version: "3.9"). I've modified your file below to include this, and I also removed the quotes around the images. After that it works.
version: "3.9"
volumes:
db:
rdb:
services:
volumes-provisioner:
image: hasnat/volumes-provisioner
environment:
PROVISION_DIRECTORIES: "1001:1001:0755:/db"
volumes:
- "./db:/db:rw"
volumes-provisioner2:
image: hasnat/volumes-provisioner
environment:
PROVISION_DIRECTORIES: "999:999:0755:/data"
volumes:
- "./rdb:/data:rw"
redis:
image: redislabs/redisgraph:2.2.6
ports:
- "6379:6379"
volumes:
- "./rdb:/data:rw"
depends_on:
- volumes-provisioner2
insight:
image: redislabs/redisinsight:1.7.1
depends_on:
- volumes-provisioner
- redis
volumes:
- "./db:/db:rw"
ports:
- "8001:8001"
I got this error when I was running docker-compose from a Windows Subsystem for Linux (WSL) terminal running on Windows. If I run docker-compose from Windows Powershell, it works.

Docker compose clone volume into src code

I have following simple docker-compose file:
version: "3.7"
services:
db:
image: postgres:12
container_name: project-back_postgres
environment:
POSTGRES_PASSWORD: pass
POSTGRES_USER: user
POSTGRES_DB: project-back
volumes:
- type: bind
source: ./db-data
target: /var/lib/postgres/data
ports:
- "5432:5432"
My false expectation of this volume definition was, that volume will be available in folder db-data. However the folder db-data does not contain any volumes and remains empty.
How can I achieve this behavior that folder db-date contains volume of Postgres?
you can try mounting volume in following way
volumes:
- ./db-data:/var/lib/postgres/data

How to migrate the volumes of a Docker-Compose Postgres to another host?

I have a Docker Compose file with some services. One of them is the database which I would like to back up the volumes and migrate all the data to another machine.
My docker-compose.yml looks like this
version: '3'
services:
service1:
...
serviceN:
db:
image: postgres:11
ports:
- 5432:5432
networks:
- postgresnet
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- postgresql:/var/lib/postgresql
- postgresql_data:/var/lib/postgresql/data
volumes:
postgresql:
postgresql_data:
networks:
postgresnet:
driver: bridge
How could I backup the data of postgresql and postgresql_data volunes and migrate them to another machine?
Easiest way is to share external volumes between your docker-compose files.
First create volume
docker volume create shared-data
Next modify you yml:
...
volumes:
postgresql:
postgresql_data:
external:
name: shared-data
...
Now your postgresql_data is mapped to external volume and everything you save there could be visible from outside. Just create same configuration in another docker-compose.yml and enjoy.