docker-compose postgresql persitent local storage - postgresql

How can I setup postgresql in docker to be persistent into a local folder?
version: '2'
services:
db:
image: postgres:9.6.1-alpine
ports:
- "5432:5432"
environment:
- POSTGRES_USER=a
- POSTGRES_PASSWORD=a
- POSTGRES_DB=a
volumes:
- /var/lib/postgresql:./postgres
volumes:
pgdata:
driver: local
This will not work and yield only
ERROR: for db Cannot create container for service db: invalid volume spec "postgres": invalid volume specification: 'postgres': invalid mount config for type "volume": invalid mount path: 'postgres' mount path must be absolute
ERROR: Encountered errors while bringing up the project.

You need to use absolute mount path, ./postgres should be changed.

Related

volume mongodb with docker-compose

I try to turn docker-compose with volume mongodb; But Iget this error and I don't know the origin of this error any idea:
ERROR: for api_db_1 Cannot start service db: OCI runtime create failed: invalid mount {Destination:mongo-data Type:bind Source:/var/lib/docker/volumes/110cc05fb16f8f6381dea1ff6d17e95e4f8458d98e87e9524514653b19db8e6d/_data Options:[rbind]}: mount destination mongo-data not absolute: unknown
I use image mongo:3.6-xenial for mongo and docker-compose version 1.29.2.
I ended up with the same error.
My problem was that I didn't specify the file path in which the volume was set in.
I believe for your case, you have a service called db that uses volumes called mongo-data.
Something like below...
services:
db:
// other service specifications
volumes:
- mongo-data
What I did to solve was to specify the file path docker can look for, similar to this...
services:
db:
// other service specifications
volumes:
- mongo-data:mongo-data-file-path
I'm with the same error here :-(
ERROR: for firebird_db_1 Cannot start service db: failed to create shim: OCI runtime create failed: invalid mount {Destination:storage Type:bind Source:/var/lib/docker/volumes/7d63ff4c2624358a5278b14e08e911077dae3359709bd5e114b25591dbf6e1a0/_data Options:[rbind]}: mount destination storage not absolute: unknown
my docker-compose file is this:
PS.: this docker-compose file was working until a few weeks ago...
version: "3.2"
services:
db:
image: jacobalberty/firebird
volumes:
- ./storage
restart: on-failure
environment:
TZ: America/Sao_Paulo
ISC_PASSWORD: masterkey
FIREBIRD_DATABASE: dev
FIREBIRD_USER: dev
FIREBIRD_PASSWORD: dev
EnableLegacyClientAuth: 'true'
ports:
- 3050:3050
Assign an absolute path for volume mongo-data in docker-compose file should work.

ERROR: Named volume "mongodb:/data/db:rw" is used in service "mongo" but no declaration was found in the volumes section

I just start learning Docker recently. Trying to set up a service that stores database.
Here's my docker-compose.yml:
version: "3.7"
services:
mongo:
image: mongo
ports:
- 27018:27017
volumes:
- mongodb:/data/db
volumes:
mongo:
As I run docker-compose up ., I got this error:
ERROR: Named volume "mongodb:/data/db:rw" is used in service "mongo" but no declaration was found in the volumes section.
And I did create my named volume mongodb outside. Running docker volume ls would give me:
DRIVER VOLUME NAME
local mongodb
Any ideas? Thanks alot.
For named volumes you should have a "volumes" key on a same level as "services", and any named volumes you are using in your services have to be listed under the "volumes" key (where you missed).
in your case :
version: "3.7"
services:
mongo:
image: mongo
ports:
- 27018:27017
volumes:
- mongodb:/data/db
volumes:
mongodb:
Just a reminder : anonymous volumes, and bind mounts don't need to be specified here.
hope this helped you :)

Volume mount in docker for postgres data

I am trying to insert data into postgres using docker.
I have a folder in my code named data which has insert commands and has one file named init.sql.
I want to insert the data from init.sql present in folder data to tables present in docker.
version: '3.1'
services:
postgres:
image: postgres:11.6-alpine
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_PORT: 5432
volumes:
- ./tables:/docker-entrypoint-initdb.d/
- ./data:/var/lib/postgresql/data
volumes:
data: {}
I am trying this but I get the error:
initdb: directory "/var/lib/postgresql/data" exists but is not empty
I think I am not using the correct use case, I am new to docker compose.
But is there any way, my use case can get satisfied?
This is caused by an improper usage of the volumes syntax for your named volume.
In order to mount a named volume you have to just use its name like this:
volumes:
- data:/var/lib/postgresql/data
If your syntax begins with a . then it will be a bind mount from your host.
volumes:
- ./data:/var/lib/postgresql/data
The above code is mounting the host folder data relative to where you your docker-compose.yml is located.
This docker-compose.yml should do what you expect.
version: '3.1'
services:
postgres:
image: postgres:11.6-alpine
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_PORT: 5432
volumes:
- ./tables:/docker-entrypoint-initdb.d/
- data:/var/lib/postgresql/data
volumes:
data:
If for some reason your volume has been created already, with an empty or no database, your first step should be running:
docker-compose down --volumes
From the documentation:
-v, --volumes Remove named volumes declared in the `volumes`
section of the Compose file and anonymous volumes
attached to containers.
From: https://docs.docker.com/compose/reference/down/

How do I back up docker volume for postgres?

Here is my docker compose file
version: '2'
services:
postgres9:
image: postgres:9.4
expose:
- 5432
volumes:
- data:/var/lib/postgresql/data
volumes:
data: {}
I would like to back up data .
Is there a command for that in docker? I am using mac for my local and linux for my server
Volumes are not anything special in case of Docker. They are simple directories/files, when you are using volumes in compose file docker will create a directory and mount that inside of your container when you run the container. You can see that by:
docker inspect <container name/id>
in the output you will find information about volumes, you can see there the directory on the host OS
To backup your volume you can simply compress and store the directory using tar. To do that you need to know the path of the directory, you can either mount a directory from host OS like:
version: '2'
services:
postgres9:
image: postgres:9.4
expose:
- 5432
volumes:
- /var/lib/postgresql/data:/var/lib/postgresql/data
and you can backup /var/lib/postgresql/data from the host OS either by mounting it another container or from the host OS directly
OR there is another way, you can mount the same volume in another container in readonly mode and backup the directory:
version: '2'
services:
postgres9:
image: postgres:9.4
expose:
- 5432
volumes:
- data:/var/lib/postgresql/data
backuptool:
image: busybox:latest
volumes:
- data:/data:ro
volumes:
data: {}
you can then tar and upload the backup of /data from the backuptool container

How to make persistent storage with docker-compose up-down-up?

I have a multiple container application, that is using the postgres image in docker-compose.yml file. Postgres container has volume on host machine for persistent storage.
When I run docker-compose up at first time all is fine, postgres creates db files in my host folder.
After it I need to shut down application temporarily with docker-compose down if I'll change code of web container.
When I run docker-compose up second time, postgres overwriting all db files, but I need that data not changes. How can I solve this issue?
My docker-compose.yml
version: '2'
services:
web:
build: ./web
command: python3 main.py
volumes:
- ./web:/app
ports:
- "80:80"
depends_on:
- db
- redis
links:
- db:db
- redis:redis
db:
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD:0000
volumes:
- ./pgdb:/var/lib/postgresql/data
redis:
image: redis
ports:
- "6379:6379"
command: redis-server --appendonly yes
volumes:
- ./redisdb:/data
I solve this problem. It occurs probably because I changed permissions for pgdb directory with host root user. By default I couldn't open pgdb in host machine because owner is postgres user. I could be wrong but after I stopped to change the resolutions the problem was gone.