Why doesn’t my Docker Compose service map ports correctly when using ‘make run-database’?

0
1
Asked By CuriousCoder92 On

I'm currently diving into a project called stac-fastapi-pgstac, which you can find on GitHub. The Docker Compose file is set to map port 5432 (the default for PostgreSQL) to port 5439. Here's the relevant section from the docker-compose.yml:

```yaml
database:
image: ghcr.io/stac-utils/pgstac:v0.9.2
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=password
- POSTGRES_DB=postgis
- PGUSER=username
- PGPASSWORD=password
- PGDATABASE=postgis
ports:
- "5439:5432"
command: postgres -N 500
```

When I run the command `make run-database`, the container starts but I don't see any port mapping—it runs on 5432. But when I execute `make docker-run`, which uses `docker compose up`, the database container starts with the correct mapping from 5432 to 5439. I thought `docker compose up` should handle the port mapping. What could be going on here?

1 Answer

Answered By DockerNinja42 On

It sounds like you're mixing up the commands!

`docker compose up` is meant to start all services and set up everything according to your configuration, including port mappings. It builds and runs your full setup.

On the other hand, `docker compose run` is intended for one-off tasks within a service, which doesn't set up or expose ports defined in your Docker Compose file. That's why you're seeing 5432 without mapping when using `make run-database`. So to get your intended mapping, stick with `make docker-run` or use `docker compose up -d` to run it in the background without any issues!

TechWhiz99 -

Exactly! Just remember, if you're debugging or need terminal access, using `run` is fine, but always use `up` for the full setup. Just keep an eye on those port mappings!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.