A lightweight Alpine Linux container running an OpenSSH server. Configure the SSH listening port and multiple user accounts through environment variables.
docker run -d \
--name sshd \
-p 222:22 \
-e SSH_USER1=testuser \
-e SSH_PASSWORD1='your-strong-password' \
sshd-docker
Connect to the SSH server:
ssh -p 222 testuser@localhost
Replace sshd-docker with the image name you use.
Define each user with a numbered pair of environment variables:
docker run -d \
--name sshd \
-p 222:22 \
-e SSH_USER1=alice \
-e SSH_PASSWORD1='alice-password' \
-e SSH_USER2=bob \
-e SSH_PASSWORD2='bob-password' \
-e SSH_USER3=charlie \
-e SSH_PASSWORD3='charlie-password' \
sshd-docker
Connect using any configured account:
ssh -p 222 alice@localhost
ssh -p 222 bob@localhost
ssh -p 222 charlie@localhost
User configuration is processed sequentially, starting at 1. Processing stops at the first undefined or empty SSH_USER<n>. Any subsequent user variables are ignored.
Every configured username must have a corresponding non-empty password. Otherwise, the container exits with an error.
Use SSH_PORT to change the port on which the SSH server listens inside the container.
docker run -d \
--name sshd \
-p 2222:2222 \
-e SSH_PORT=2222 \
-e SSH_USER1=testuser \
-e SSH_PASSWORD1='your-strong-password' \
sshd-docker
Connect using:
ssh -p 2222 testuser@localhost
Docker port mapping follows the format HOST_PORT:CONTAINER_PORT. The published container port must match SSH_PORT.
| Variable | Required | Default | Description |
|---|---|---|---|
SSH_PORT | No | 22 | SSH server listening port inside the container. Valid range: 1–65535. |
SSH_USER1 | Yes | None | Username for the first SSH account. |
SSH_PASSWORD1 | Yes | None | Password for the first account. |
SSH_USER2 | No | None | Username for the second SSH account. |
SSH_PASSWORD2 | Yes, if user 2 is configured | None | Password for the second account. |
SSH_USER<n> | No | None | Username for account number n. |
SSH_PASSWORD<n> | Yes, if user n is configured | None | Password for account number n. |
1.SSH_USER<n> terminates the user list.View SSH server logs:
docker logs sshd
Stop the container:
docker stop sshd
Remove the container:
docker rm sshd
To keep the SSH server host keys across container recreation, persist /etc/ssh/ using a Docker volume. Without persistent storage, host keys generated inside the container may change when the container is restarted, causing SSH clients to report a host key mismatch.
Create a named volume:
docker volume create sshd-host-keys
Start the container with the volume mounted:
docker run -d \
--name sshd \
-p 222:22 \
-v sshd-host-keys:/etc/ssh \
-e SSH_USER1=testuser \
-e SSH_PASSWORD1='your-strong-password' \
sshd-docker
The first startup generates the host keys if they are missing. The named volume retains the files in /etc/ssh/ when the container is removed and recreated.
Content type
Image
Digest
sha256:d4670267c…
Size
6.5 MB
Last updated
15 days ago
docker pull tordarus/sshd-alpine