When you connect two containers together, you first create a network.
docker network create mynetwork
Then you either create a container using this network or attach a running container to the network.
docker run --rm -d --network mynetwork -e POSTGRES_PASSWORD=password -e POSTGRES_DB=MyDB -e POSTGRES_USER=postgres -p 5433:5432 --name db postgres
#or
docker network connect mynetwork mycontainer
Notice in the first example that we published a different port than the internal port (-p 5433:5432
). This is only for the host, not the other containers in the network. In this example, mycontainer
would still need to use port 5432
(the internal port) to connect to postgres.
Hopefully this will save me some time in the future.