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.
π FAQ β Ubuntu Docker Container with SSH Access
1. What does this tutorial help you achieve?
This guide shows you how to run an Ubuntu container in Docker and enable remote command-line access using SSH, allowing you to remotely manage the container environment.
2. Do I need Docker installed before starting?
Yes. You must have Docker installed and running on your machine β typically a Linux host like Ubuntu β before you can create containers and configure SSH access inside them.
3. Why expose the SSH port when creating the container?
When you run the container, you map the host port to port 22 inside the container so SSH connections from outside can reach the containerβs SSH service. For example, -p 2222:22 maps host port 2222 to the containerβs SSH.
4. How do I install and enable SSH inside the container?
After creating the container, you access its shell with docker exec and install the OpenSSH server package. Then you start the SSH service so it can accept incoming connections.
5. How do I connect to the container over SSH?
You use an SSH client from your host system or another machine. For example:
ssh -p 2222 root@localhost
Enter the default password (often root) or set up key-based login for more security.
6. Can I use SSH keys rather than a password?
Yes β you can generate an SSH key pair on your local host and copy your public key into the container so that you can SSH in without typing a password.
7. Is it secure to enable SSH inside a Docker container?
Running SSH adds extra attack surface because containers are supposed to be lightweight and single-purpose. Use strong passwords or SSH keys, change default settings, and consider firewall rules to keep access secure.
8. What happens if I forget to map the SSH port?
If you donβt expose the SSH port during container creation, external SSH clients wonβt be able to reach the SSH service inside the container. Make sure to use the -p flag with docker run.
9. Do I need to build a custom Docker image to enable SSH?
No β you can use the standard Ubuntu image, install SSH inside it, and start the SSH server manually. Alternatively, you could build a custom image that already has SSH configured for convenience.
10. Can I SSH directly into the container without mapping ports?
The typical SSH access requires port exposure. However, if your workflow is local and you only need a shell inside the container, using docker exec -it <container> bash lets you interact with it without SSH.









