This setup provides a Docker container running an SSH server based on Ubuntu 22.04.
# Build and start the container
docker-compose up -d
# View logs
docker-compose logs -f
# Stop the container
docker-compose down# Connect via SSH (password: sshpassword)
ssh -p 2222 sshuser@localhost- Username:
sshuser - Password:
sshpassword - Port: 2222 (host) → 22 (container)
⚠️ Security Warning: Change the default password in production environments!
Edit the Dockerfile and change the password in the RUN command:
RUN echo 'sshuser:YOUR_NEW_PASSWORD' | chpasswdThen rebuild:
docker-compose up -d --builddocker exec -it ssh-server passwd sshuser- Create an
authorized_keysfile with your public key:
# Copy your public key to authorized_keys
cat ~/.ssh/id_rsa.pub > authorized_keys- Uncomment the volume mount in
docker-compose.yml:
volumes:
- ./authorized_keys:/home/sshuser/.ssh/authorized_keys:ro- Restart the container:
docker-compose down
docker-compose up -d- Connect without password:
ssh -p 2222 sshuser@localhostTo use a different host port, edit docker-compose.yml:
ports:
- "YOUR_PORT:22" # Change YOUR_PORT to desired portssh-user-data: Persists the user's home directory data across container restarts
- Change default password immediately
- Use SSH key authentication instead of passwords
- Disable password authentication in
/etc/ssh/sshd_configafter setting up keys - Use a firewall to restrict access to the SSH port
- Keep the image updated regularly
docker-compose psdocker-compose logs ssh-serverdocker exec -it ssh-server /bin/bash# Test connection
ssh -p 2222 -v sshuser@localhost
# Test with specific key
ssh -p 2222 -i ~/.ssh/id_rsa sshuser@localhostEdit the Dockerfile and add additional users:
RUN useradd -rm -d /home/newuser -s /bin/bash -g root -G sudo -u 1002 newuser && \
echo 'newuser:newpassword' | chpasswdAdd installation commands in the Dockerfile:
RUN apt-get update && \
apt-get install -y git vim curl && \
apt-get cleanModify the SSH configuration in the Dockerfile:
RUN echo "MaxAuthTries 3" >> /etc/ssh/sshd_config && \
echo "ClientAliveInterval 300" >> /etc/ssh/sshd_config && \
echo "ClientAliveCountMax 2" >> /etc/ssh/sshd_config# Stop and remove container, networks, and volumes
docker-compose down -v
# Remove the image
docker rmi sysdocs-ssh-server