Use multiple Docker containers without exposing ports

If you’d like to have separate redis and postgres containers you will need to omit the 2221 part of that expose section to be able to ssh into both containers.

You can use dynamic port allocation with docker like this:

Postgres container:

expose:
# - "5432:5432"
  - "127.0.0.1::22"

Redis container:

expose:
# - "6379:6379"
  - "127.0.0.1::22"

A helpful page on this can be found here

I just thought I would document this here as this is a page I’ve kept coming back to whilst setting up multiple containers.

Thanks for the original topic @jspdng

3 Likes