Installing Erlang in a Docker Container
Running Erlang as a Docker container is often preferred over a local installation because it keeps your host system clean [...]
Running Erlang as a Docker container is often preferred over a local installation because it keeps your host system clean and allows you to switch between versions (e.g., OTP 25 vs. OTP 27) instantly.
The most efficient way to do this is by using the official Erlang image from Docker Hub.
1. Quick Start: Interactive Shell
If you just want to drop into an Erlang shell (erl) to test some code, run this command in your terminal:
docker run -it --rm erlang
-it: Starts the container in interactive mode with a TTY (so you can type).--rm: Automatically deletes the container when you exit the shell.erlang: The image name. It defaults to the latest stable version of Erlang/OTP on Debian.
2. Specific Versions & Lightweight Images
You can specify versions or use Alpine Linux for a much smaller footprint (around 30–50MB vs 300MB+).
Specific Version: docker run -it --rm erlang:26
Ultra Lightweight: docker run -it --rm erlang:alpine
Slim (Debian): docker run -it --rm erlang:slim
3. Running a Local Script
If you have an Erlang file (e.g., hello.erl) on your Linux machine and want to run it inside the container:
docker run -it --rm -v "$PWD":/app -w /app erlang erlc hello.erl \
&& erl -noshell -s hello start -s init stop-v "$PWD":/app: Mounts your current directory to/appinside the container.-w /app: Sets the working directory inside the container to/app.
4. Using a Dockerfile (For Projects)
For a persistent application, create a Dockerfile in your project root:
Dockerfile
# Use the official Erlang image
FROM erlang:26-alpine
# Create app directory
WORKDIR /usr/src/app
# Copy your source code
COPY . .
# Compile the application
RUN rebar3 compile
# Start the Erlang shell or application
CMD ["rebar3", "shell"]
To build and run it:
- Build:
docker build -t my-erlang-app . - Run:
docker run -it --rm my-erlang-app
The Cookie and Networking
If you plan on connecting multiple Erlang nodes across containers, remember that Erlang nodes need a shared secret (cookie). You can pass this via environment variables or command flags:
erl -sname node1 -setcookie my_secret_cookie