I am using docker and django and postgress for a project. I followed this link here https://docs.docker.com/samples/django/ which basically saves the data from postgress in a directory under the main project like the following code:
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
depends_on:
- db
This code works perfectly on my machine, but now I would like to push this project using git and so when the other party clones it, then can immediately run docker-compose up
and continue where I left off with the same database intact (for example, the superuser in django should exist in the other machine). Is that possible? If yes, how can I achieve that?
I did some tests, but on my end it did not work, the other machine always showed some errors like directory is missing and when those were created, there were other errors like 'x is not a directory'.
Thank you
CodePudding user response:
Try this.
version: " # Your version"
volumes:
pgdata: # empty definition uses volume defaults
services:
db:
image: postgres
volumes:
- type: volume
source: pgdata # The named volume above
target: /var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
depends_on:
- db
Note: One can inspect volumes by using docker volume inspect
subcommand.