I set up a Docker compose file with two Postgres database services, one for production and one for testing. When exposing the ports from container to host I started with following compose file
networks:
db:
volumes:
db_data:
db_data_test:
services:
postgres:
image: postgres:16
hostname: postgres-hostname
networks:
- db
ports:
- 127.0.0.1:5432:5432
volumes:
- db_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready", "-d", "db_prod"]
interval: 30s
timeout: 60s
retries: 5
start_period: 80s
env_file:
- ../.env
postgres_test:
image: postgres:16
hostname: postgres-hostname
networks:
- db
ports:
- 127.0.0.1:5433:5432
volumes:
- db_data_test:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready", "-d", "db_prod"]
interval: 30s
timeout: 60s
retries: 5
start_period: 80s
env_file:
- ../.env
Using when running my application with these databases, I experience very slow (~18s) execution of the database migration method I use to initially create my database before each unit test using the database.
When I removed the 127.0.0.1 the execution time dropped sharp to what I expect (<1s).
I'm not very familiar with the inner workings of Docker or ports on my host. Would be great if someone can explain what happens if I put 127.0.0.1 before the host port.