Hello guys I have tried to mount the persistent volume to the PostgreSQL, but Postgres just screws me over saying
postgres_1 | initdb: directory "/var/lib/postgresql/data" exists but is not empty
postgres_1 | If you want to create a new database system, either remove or empty
postgres_1 | the directory "/var/lib/postgresql/data" or run initdb
postgres_1 | with an argument other than "/var/lib/postgresql/data".
and I can not get my head around why? Here is my docker-compose.yml. I am running on MacOS High Sierra (APFS), Docker CE.
version: '3'
services:
postgres:
image: postgres:10.1-alpine
ports:
- "5432:5432"
networks:
- db
environment:
POSTGRES_USER: x
POSTGRES_PASSWORD: y
volumes:
- pgdata:/var/lib/postgresql/data
networks:
proxy:
driver: bridge
db:
driver: bridge
volumes:
pgdata:
the āvolumeā is a docker term for the connectivity to a host folderā¦ doesnāt have any concept of data in the folder.
THAT is an application (postgres) thing
the directory pgdata should have NO files in you are creating a NEW databaseā¦
if you are REUSING an existing database then there can be files present (but then you would not say ācreate databaseāā¦
In the docker-compose.yml the OP quoted, the volume is a Docker-managed volume, not a bind-mounted host directory.
docker volume ls will show the volumes that exist. Since the default assumption is that they hold valuable data like, say, your database, the default tooling is a little hesitant to delete them; and when you restart the container it will pick up the old volume again. docker volume rm will delete an (unused) volume thatās lying around. You need to give an explicit docker-compose down -v option to cause Compose to remove named volumes.