In this guide, you'll learn how to set up an Ubuntu Docker container with SSH access, managed through Portainer. This allows you to SSH into your container just like a regular server.
📋 Prerequisites
- Docker and Portainer installed on your host machine.
- Basic knowledge of Docker and SSH.
🛠️ 1. Update Docker Compose to Include SSH Server
Create or edit your docker-compose.yml:
version: '3.8'
services:
ubuntu-container:
image: ubuntu:latest
container_name: ubuntu-container
tty: true
stdin_open: true
command: /usr/sbin/sshd -D # Start SSH daemon
ports:
- "2222:22" # Host port 2222 maps to container's SSH port
volumes:
- /srv/ubuntu-data:/data # Persistent storage
networks:
- ubuntu_network
restart: unless-stopped
networks:
ubuntu_network:
driver: bridge
📥 2. Deploy or Update the Stack in Portainer
- In Portainer, go to Stacks → Add/Edit Stack.
- Paste your updated docker-compose.yml and Deploy the Stack.
🔧 3. Configure SSH Inside the Container
🖥️ Access the Container Console
- In Portainer, go to Containers → ubuntu-container → Console →
/bin/bash
.
⚡ Install SSH Server & Nano
apt update
apt install -y openssh-server nano
📝 Edit SSH Config
nano /etc/ssh/sshd_config
Ensure these lines are set:
PermitRootLogin yes
PasswordAuthentication yes
Save and exit (Ctrl + O
, then Ctrl + X
).
🔄 Restart SSH Service
service ssh restart
🔑 Set Root Password
passwd
Enter a strong password when prompted.
✅ 4. Verify SSH Setup
📡 Check if SSH is Running
netstat -tulnp | grep ssh
You should see it listening on port 22.
🔗 SSH into the Container
From your host or remote machine:
ssh root@<host_ip> -p 2222
Replace with your Docker host's IP and use the password you set earlier.
🔐 5. (Optional) Enable Password-less SSH (Using SSH Keys)
- Generate SSH keys (if you haven't already):
ssh-keygen
- Copy your public key to the container:
ssh-copy-id -p 2222 root@<host_ip>
Now, you can SSH without a password. 🎉
⚡ 6. Additional Tips
🧭 Verify Port Mapping
Run:
docker ps
You should see:
0.0.0.0:2222->22/tcp
If not, stop and recreate the container with:
docker run -d -p 2222:22 --name ubuntu-container ubuntu
📊 7. Check Ubuntu Version Inside the Container
➤ Without Entering Bash:
docker exec -it ubuntu-container lsb_release -a
Or, if lsb_release is missing:
docker exec -it ubuntu-container cat /etc/os-release
➤ By Entering the Container:
docker exec -it ubuntu-container /bin/bash
lsb_release -a
# or
cat /etc/os-release
Expected output:
NAME="Ubuntu"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 22.04.3 LTS"
VERSION_ID="22.04"
💡 If lsb_release is missing, install it:
apt update && apt install -y lsb-release
🎉 Done!
You now have an Ubuntu Docker container with SSH access, managed via Portainer. 🚀
💬 Got questions or suggestions? Drop them in the comments below!
Top comments (0)