Why Is My Docker Port Mapping Not Working with Compose?

0
17
Asked By CuriousCat99 On

I'm diving into a project called stac-fastapi-pgstac and I'm having some trouble with Docker. The docker-compose file I'm using shows that port 5432 is supposed to be mapped to port 5439. Here's a snippet from my docker-compose.yml file:

```
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 `make run-database`, it starts a container that shows it's running on port 5432, but there's no port mapping happening. In the Makefile, this is what it looks like:
```
.PHONY: run-database
run-database:
docker compose run --rm database
```

But when I use `make docker-run`, which here is defined as:
```
.PHONY: docker-run
docker-run: image
docker compose up
```
It properly maps the db container with port 5432 to 5439, just as expected. I thought `docker compose up` would handle this mapping automatically, especially since 5439 is only referenced in the docker-compose.yml file. Can someone help me understand what's happening here?

3 Answers

Answered By DockerDude42 On

You're running into the classic difference between `docker compose up` and `docker compose run`.

When you use `docker compose up`, it starts your entire application stack as defined in your docker-compose.yml. This encompasses everything including port mappings, dependencies, and so on.

On the flip side, `docker compose run` is used for executing a one-time command in a container. It runs the specified service in an isolated context, which doesn't handle port mappings as it spins up an independent container for that specific task. That’s why you don’t see the port mapping when using `make run-database`—it’s only creating a new, single-use container without the full environment setup.

NerdyNina -

Exactly! It's all about the context in which you're calling those commands. `docker compose up` is meant to bring everything to life while `run` is for quick tasks without all the extras.

Answered By TechieTom On

Check what you see with `docker ps` after running the commands. The container from `make run-database` might not be running, which is probably why you're not seeing the port forwarding. Also, switching to `docker compose up -d` when you need your container to run in the background could save you some headaches!

Answered By UsageWizard On

Just to clarify the port mapping: `ports:
- 5439:5432` means that port 5439 on your host routes to port 5432 in the container. This is to expose the default PostgreSQL port (5432) to 5439 on your local machine. But yeah, when you're using `docker compose run`, it skips the port mappings entirely. Make sure to use `docker compose up` for the mappings to take effect!

LostInDockerLand -

I appreciate the clarification! I wasn't aware that `run` would skip the mapping. I'll definitely keep that in mind going forward.

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.