Reputation: 16309
I'm in the process of setting up my first postgresql docker container, as part of a distributed application using docker compose, and am somewhat confused on the syntax of the compose file. Still pretty new to docker so forgive me if this is straight out of docker 101.
Here's what it looks like for my postgres container in docker-compose.yml:
version: '2'
services:
database:
image: postgres
container_name: database-container-name
environment:
- POSTGRES_PASSWORD=some_password_here
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- PGDATA=/var/lib/postgresql/data/db-files/
ports:
- 5433:5432
volumes:
- ./.db/data:/var/lib/postgresql/data:delegated
- ./.db/init:/docker-entrypoint-initdb.d
The volumes
bit is what throws me for a loop. Can someone explain what's going on there, is the container mapping its /.db/data/
folder to my local /var/lib/postgresql/data
folder? I've looked at some documentation but it's not sinking in.
Also, in the ports section above, what's the deal with 5433:5432
? Does that mean my port 5433 maps to the docker container's port 5432? If so, does this mean if I connnect to psql or pgAdmin on port 5433 on my box it's silently mapping to the postgresql instance in the docker container?
Upvotes: 0
Views: 500
Reputation: 311721
The volumes bit is what throws me for a loop. Can someone explain what's going on there, is the container mapping its /.db/data/ folder to my local /var/lib/postgresql/data folder?
In the volumes
section of your database
service, you're creating a couple of bind mounts between your host and your container. The syntax of each volume entry (in this situation) is HOST_PATH:CONTAINER_PATH:OPTIONS
. So when you see:
- ./.db/data:/var/lib/postgresql/data:delegated
You are mapping the local (to your docker-compose.yml
) .db/data
directory onto /var/lib/postgresql/data
in the container. In other words, changes made in one directory will be visible in the other.
The delegated
option is specific to MacOS; the docs say:
delegated
: The container runtime’s view of the mount is authoritative. There may be delays before updates made in a container are visible on the host.
The volume
section of your service corresponds to the -v
option to docker run
; you may find more interesting in the docker run
documentation in addition to the bind-mount docs I linked above.
Also, in the ports section above, what's the deal with 5433:5432? Does that mean my port 5433 maps to the docker container's port 5432? If so, does this mean if I connnect to psql or pgAdmin on port 5433 on my box it's silently mapping to the postgresql instance in the docker container?
The ports
section is for publishing ports on your host. The syntax is HOST_PORT:CONTAINER_PORT
. So when you see:
- 5433:5432
This is mapping host port 5433
to container port 5432
. In other words, you can connect to your postgres database by connecting to port 5433
on your host. This is probably in place to avoid a conflict if you already had a postgres instance running on your host, which would already be bound to port 5432.
Upvotes: 1