Search code examples
dockerfigdocker-compose

Understanding ports and links in docker compose


From my understanding of docker compose / fig, creating a link between two services/images is one main reason if you do not want to exposes ports to others.

like here db does not expose any ports and is only linked:

web:
  build: .
  links:
   - db
  ports:
   - "8000:8000"   
db:
  image: postgres

Does web thinks db runs on its localhost? Would i connect from a script/program in web to localhost:5432 (standard port from postgresql) to get a database connection?

And if this is correct, how can you change port 5432 to 6432, without exposing? would i just run postgresql on a different port?

Update:

useful links after some input:

http://docs.docker.com/userguide/dockerlinks/

https://docs.docker.com/compose/yml/#links


Solution

  • web thinks db runs on the host pointed to by the env variable DOCKER_DB or something like that. Your services should point to that variable (host), not localhost.

    The db container exposes ports (via EXPOSE) to its linked containers, again in variables. You can run the db on whatever port you want, as long as it's EXPOSEd.