Setting Up an Ubuntu Docker Container with SSH Access

On this article we will discuss how to setup Ubuntu Docker container with SSH access.

Introduction

Docker containers provide a lightweight and efficient way to package and deploy applications. With SSH access, you can remotely manage and configure your containers. In this tutorial, we will walk you through the steps to set up an Ubuntu Docker container with SSH access.

Prerequisites

Before you begin, make sure you have the following:

  • A Linux machine with Docker installed.
  • Basic familiarity with the command line.

The setup Ubuntu Docker containers ssh step is as follow.

Step 1: Install Docker

Update the package index:

sudo apt update

Install Docker’s dependencies:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

Add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Add the Docker repository:

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update the package index again:

sudo apt update

Install Docker:

sudo apt install docker-ce docker-ce-cli containerd.io

Step 2: Pull an Ubuntu Docker Image

Pull the latest Ubuntu image from Docker Hub:

sudo docker pull ubuntu:latest

Step 3: Create a Docker Container and Expose SSH Port

Create a new container with SSH access:

sudo docker run -d -p 2222:22 --name mycontainer ubuntu:latest

This command creates a container named mycontainer using the Ubuntu image and maps port 2222 on the host to port 22 (SSH) on the container.

Step 4: Install OpenSSH Server in the Container

Access the container’s terminal:

sudo docker exec -it mycontainer bash

Update the package index:

apt update

Install OpenSSH server:

apt install openssh-server

Start the SSH service:

service ssh start

Exit the container’s terminal:

exit

Step 5: Connect to the Container via SSH

Open a terminal or SSH client on your local machine.

Connect to the container using SSH:

ssh -p 2222 root@localhost

Enter the password when prompted. The default password for the root user in the Ubuntu Docker image is root.

Alternatively, you can use key-based authentication:

Generate an SSH key pair on your local machine if you haven’t already:

ssh-keygen

Copy your public key to the container:

ssh-copy-id -p 2222 root@localhost

Enter the password when prompted.
After copying the key, you can connect to the container without entering a password:

ssh -p 2222 root@localhost

Conclusion

In this tutorial, you learned how to set up an Ubuntu Docker container with SSH access. With SSH, you can remotely manage and configure your container, making it easier to work with Docker in a distributed environment. Remember to secure your SSH access by using strong passwords or implementing additional security measures like key-based authentication or firewall rules.

(Visited 134 times, 1 visits today)

You may also like