What a coincidence, I was writing a guide on this subject - for docker and k8s (with libcluster).
Here, I used “cluster_demo” image with phx + mix release.
First of all, to use automatic DNS with FQDN, you should use docker network. Without it, you cannot access a container from another container with container name.
Otherwise, you have to use IP address inside RELEASE_NODE
- which is doable, but needs wrapper (either docker, or env.sh
) and other containers need to know the ip address (which does not stay!)
Here are examples of using docker network for DNS with container name.
# this will create <container name>.my-net DNS entry.
docker network create my-net
By default, RELEASE_DISTRIBUTION
is sname
(allowing non-FQDN), using release name and host id automatically for RELEASE_NAME
docker run --rm \
--name snamenode \
--network my-net \
--env RELEASE_COOKIE=thisissecret \
--env SECRET_KEY_BASE=+y5AreV1firmKw+kB9idUb0gp3lxi3Y5qhMntozh8P9xHS/+iq2wN1LH3ZALFLo7 \
-p 4000:4000 \
cluster-demo
To use name
(with FQDN)
docker run --rm \
--name namenode \
--network my-net \
--env RELEASE_COOKIE=thisissecret \
--env RELEASE_DISTRIBUTION=name \
--env RELEASE_NODE="custom@namenode.my-net" \
--env SECRET_KEY_BASE=+y5AreV1firmKw+kB9idUb0gp3lxi3Y5qhMntozh8P9xHS/+iq2wN1LH3ZALFLo7 \
-p 4001:4001 \
cluster-demo
To run remote
from the same cotainer (docker exec
) - you don’t need to set anything since all are already there
docker exec -it snamenode bin/app remote
docker exec -it namenode bin/app remote
rpc
works well from the same container
docker exec -it snamenode bin/app rpc "%{cookie: Node.get_cookie(), self: Node.self(), list: Node.list()} |> IO.inspect()"
# %{cookie: :thisissecret, list: [], self: :app@2b0c8dcd9e01}
docker exec -it namenode bin/app rpc "%{cookie: Node.get_cookie(), self: Node.self(), list: Node.list()} |> IO.inspect()"
# %{cookie: :thisissecret, list: [], self: :"custom@namenode.my-net"}
To connect the container from another container, you have to set the required RELEASE_*
info
docker run --rm -it \
--name nameremote \
--network my-net \
--env RELEASE_COOKIE=thisissecret \
--env RELEASE_DISTRIBUTION=name \
--env RELEASE_NODE="custom@namenode.my-net" \
cluster-demo \
bin/app remote
docker run --rm -it \
--name remote \
--network my-net \
--env RELEASE_COOKIE=thisissecret \
--env RELEASE_NODE="app@2b0c8dcd9e01" \
cluster-demo \
bin/app remote
If you don’t run both server and remote containers in the same docker network (or without network - which is bridge mode) - then connection between them won’t work while docker exec
(in the same pod) works.