I struggle a bit with Docker and ports, but here's my situation. I have a Docker compose file which spins up a Postgres DB and adminer, the admin GUI. Here's my (truncated) config:
# database
db:
image: postgres:14.2
volumes:
- .psqlrc:/root/.psqlrc:ro
- dbdat:/var/lib/postgresql/data
- ./log:/root/log:cached
- ./latest.dump:/latest.dump
environment:
POSTGRES_DB: hlp-api-dev
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
DB_PORT: 5432
ports:
- 5432
healthcheck:
test: pg_isready -U postgres -h 127.0.0.1
interval: 5s
# database admin
dbAdmin:
image: adminer
ports:
- 8080
depends_on:
- db
That all works fine - but HTTP access to adminer is seemingly random port chosen by Docker e.g.
http://localhost:57014
...which I can find out only by clicking the brower button next to its container within Docker Desktop. How can I dictate this port, and why isn't it accessible at
http://localhost:8080
...which is the port listed?
If I try to specify another port, i.e.
ports:
- 8080
- 5000 # my choice
...then it doesn't spin up on any port at all. Even if I click the browser button within DD, I get "connection reset" in the browser.
CodePudding user response:
Yes you can. If you specify two port numbers as xxxx:yyyy
, the first one will be the host port and the second will be the internal container port that the app is listening on. Adminer listens on 8080 so that on is fixed. But you can freely choose the host port. So to access it on http://localhost:5000/, you'd do
# database admin
dbAdmin:
image: adminer
ports:
- 5000:8080
depends_on:
- db
You can do the same on the Postgres container if you want.