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)