Erlang in Docker
The official Docker image runs Erlang without touching the packages of the host, which is the quickest way to try a release of OTP that the distribution does not carry yet. The commands on this page are not run in this course container: it has no Docker daemon inside, so each one is marked as a command this environment cannot run.
The official image
The image is called erlang and its tags are OTP releases, so erlang:27 is the current one and erlang:latest follows the newest. Pull the tag you want, never the moving one, for anything a reader has to reproduce:
docker pull erlang:27
The image holds the full runtime and the OTP applications, including the compiler, so a module builds inside it with the same erlc the course uses. It has no editor, and nothing is kept when the container is removed unless a volume holds it.
A container with a shell
-it gives the container a terminal and keeps it in the foreground, --rm removes it when the shell ends, and the command after the image name is what runs inside. With erl the container opens the ordinary Erlang shell:
docker run -it --rm --name erlang-course erlang:27 erl
Everything from the shell lesson then works unchanged. To keep a module, put a folder of the host inside the container with -v "$PWD":/work -w /work and compile there; without a volume the file disappears with the container.
Two containers
Distributed Erlang needs the epmd daemon on port 4369, which the image starts with the runtime. A container that only runs one node needs no published port; a second container that must reach the first one needs both on the same user defined network, and each node needs a name of its own:
docker network create erlang-net
docker run -d --name node-a --network erlang-net erlang:27 erl -name a@node-a -setcookie coursecookie -noshell -eval 'timer:sleep(infinity).'
docker run -it --rm --network erlang-net erlang:27 erl -name b@node-b -setcookie coursecookie
The long names end in the container name, which the network resolves, and the cookie is the same on both sides. Publishing 4369 to the host is only needed when a node outside Docker joins, and -p 4369:4369 on more than one container would clash on the host port.
Stop and remove
In an interactive shell Ctrl-D or q(). ends the Erlang shell, and with it the container, because the shell was the container's command. A container started with -d keeps running until it is stopped by name:
docker stop node-a
docker rm node-a
A file that must survive the container belongs in a volume (-v erlang-data:/data) or in a folder of the host, never in the writable layer of a container that --rm deletes. The install lesson has the package of every system for a machine that keeps its files.
Автор статьи: Arthur Isaev