I am beginner docker user. I installed docker and postgresql in Mac OS. and why most of documents mention the directory /var/lib/postgresql/data as an volume setting??? Because in my local directory, there is not existed /var/lib/postgresql.. Is it default option? or am I missing something?
CodePudding user response:
Yes, correct, /var/lib/postgresql
does not exist on your local computer, but it does in the created container. The volumes parameter is used to associate the local data with the container data, in order to preserve the data in case the container crashes
For example:
volumes:
- ./../database/main:/var/lib/postgresql/data
Above we link the local directory from the left side to the container directory
CodePudding user response:
If you are using official PostgreSQL image from Docker Hub, then you can check the contents of its Dockerfile
. E.g. here is a fragment of postgres:15 image responsible for data directory:
ENV PGDATA /var/lib/postgresql/data
# this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values)
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA"
VOLUME /var/lib/postgresql/data
As you can see Postgres is configured to have data in that directory. And to persist the data even if container is stopped and removed, the volume is created. Volumes have lifetime independent of the container which allows them to "survive".