Accessing Docker Services from Host

Viewed 42

I have a simple web app with a docker-compose.yml configuration like this:

version: "3"
services:
  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres
    ports:
      - "5432:5432"

I can access the database service within the web app container with postgres://db:5432 because both containers share networking.

I'd like to access the database service from my host machine using postgres://db:5432. How do I map the db:5432 host from the container to db:5432 on my local host machine? I've tried adding 127.0.0.1:5432 db:5432 to my /etc/hosts file which does not seem to work.

1 Answers
Related