DEV Community

Carl Angelo Nievarez
Carl Angelo Nievarez

Posted on

🐳 Setting Up SSH in an Ubuntu Docker Container Using Portainer

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
Enter fullscreen mode Exit fullscreen mode

📥 2. Deploy or Update the Stack in Portainer

  1. In Portainer, go to Stacks → Add/Edit Stack.
  2. Paste your updated docker-compose.yml and Deploy the Stack.

🔧 3. Configure SSH Inside the Container

🖥️ Access the Container Console

  1. In Portainer, go to Containers → ubuntu-container → Console → /bin/bash .

⚡ Install SSH Server & Nano

apt update
apt install -y openssh-server nano
Enter fullscreen mode Exit fullscreen mode

📝 Edit SSH Config

nano /etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode

Ensure these lines are set:

PermitRootLogin yes
PasswordAuthentication yes
Enter fullscreen mode Exit fullscreen mode

Save and exit (Ctrl + O, then Ctrl + X).

🔄 Restart SSH Service

service ssh restart
Enter fullscreen mode Exit fullscreen mode

🔑 Set Root Password

passwd
Enter fullscreen mode Exit fullscreen mode

Enter a strong password when prompted.

✅ 4. Verify SSH Setup

📡 Check if SSH is Running

netstat -tulnp | grep ssh
Enter fullscreen mode Exit fullscreen mode

You should see it listening on port 22.

🔗 SSH into the Container

From your host or remote machine:

ssh root@<host_ip> -p 2222
Enter fullscreen mode Exit fullscreen mode

Replace with your Docker host's IP and use the password you set earlier.

🔐 5. (Optional) Enable Password-less SSH (Using SSH Keys)

  1. Generate SSH keys (if you haven't already):
ssh-keygen
Enter fullscreen mode Exit fullscreen mode
  1. Copy your public key to the container:
ssh-copy-id -p 2222 root@<host_ip>
Enter fullscreen mode Exit fullscreen mode

Now, you can SSH without a password. 🎉

⚡ 6. Additional Tips

🧭 Verify Port Mapping

Run:

docker ps
Enter fullscreen mode Exit fullscreen mode

You should see:

0.0.0.0:2222->22/tcp
Enter fullscreen mode Exit fullscreen mode

If not, stop and recreate the container with:

docker run -d -p 2222:22 --name ubuntu-container ubuntu
Enter fullscreen mode Exit fullscreen mode

📊 7. Check Ubuntu Version Inside the Container

➤ Without Entering Bash:

docker exec -it ubuntu-container lsb_release -a
Enter fullscreen mode Exit fullscreen mode

Or, if lsb_release is missing:

docker exec -it ubuntu-container cat /etc/os-release
Enter fullscreen mode Exit fullscreen mode

➤ By Entering the Container:

docker exec -it ubuntu-container /bin/bash
lsb_release -a
# or
cat /etc/os-release
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

💡 If lsb_release is missing, install it:

apt update && apt install -y lsb-release
Enter fullscreen mode Exit fullscreen mode

🎉 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)