Docker is a free tool that helps developers put their apps in containers that can run anywhere. SSH is a way to securely connect to another computer and run commands. This article shows you how to set up an Ubuntu Docker container with SSH access.
Prerequisites
Before we begin, you need the following software installed:
- Docker: You can install Docker by following the instructions in the official Docker documentation.
- SSH client: For Linux and MacOS, the SSH client is usually pre-installed. Windows users can use PuTTY or the built-in SSH client in Windows 10 and newer versions.
Step 1: Pull the Ubuntu Image
The first step is to pull the Ubuntu image from Docker Hub. Docker Hub is a cloud-based registry service that allows you to link code repositories, build details and more. Use the following command:
docker pull ubuntu
Step 2: Create a Dockerfile
The Dockerfile is a text document that contains all the commands a user can call on the command line to assemble an image. You can create a new Dockerfile and open it in a text editor. Here is a sample Dockerfile to set up SSH in an Ubuntu container:
# Use the official image as a parent image
FROM ubuntu
# Update the system, install OpenSSH Server, and set up users
RUN apt-get update && apt-get upgrade -y && \
apt-get install -y openssh-server
# Create user and set password for user and root user
RUN useradd -rm -d /home/ubuntu -s /bin/bash -g root -G sudo -u 1000 ubuntu && \
echo 'ubuntu:secret_password' | chpasswd && \
echo 'root:secret_password' | chpasswd
# Set up configuration for SSH
RUN mkdir /var/run/sshd && \
sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd && \
echo "export VISIBLE=now" >> /etc/profile
# Expose the SSH port
EXPOSE 22
# Run SSH
CMD ["/usr/sbin/sshd", "-D"]
Replace “secret_password” with the password you want to use for SSH. Save this Dockerfile.
Step 3: Build the Docker Image
Once your Dockerfile is ready, you can build your Docker image. Use the following command:
docker build -t ubuntu-ssh .
This command will build an image from your Dockerfile and tag it as ubuntu-ssh.
Step 4: Run the Docker Container
Now you’re ready to run your Docker container with the following command:
docker run -d -p 2222:22 ubuntu-ssh
This will run your Docker container in detached mode (-d) and map your machine’s port 2222 to your Docker container’s port 22, which is the default SSH port.
Step 5: Connect to the Docker Container via SSH
Finally, you can connect to your Docker container via SSH. Use the following command:
ssh ubuntu@localhost -p 2222
You’ll be prompted to enter the password you set earlier in the Dockerfile. Once you’ve entered it, you’ll be connected to your Ubuntu Docker container via SSH!
As you also have set the ‘root’ user password, you can switch to root user (if required0
su - root
Enter root user password set in Dockerfile.
Conclusion
Docker and SSH are great tools for development. Using them together makes your work more flexible, secure, and powerful. This guide showed you how to set up an Ubuntu Docker container with SSH access. Now you can easily create and connect to Docker containers. Always use good security practices, like changing default passwords and updating software regularly, to keep your environment safe.
4 Comments
For the error “Permission denied, please try again.”, I got that with ubuntu:20.04. Whereas an older version i.e ubuntu:16.04 works fine. I’m able to do ssh.
Shows this error: “Permission denied, please try again.”
Tried the suggestion of Andris from above comment of adding a new user.
Started a bash session using “docker exec -it /bin/bash” and created a ne user using “useradd -p ”
Even then, the ssh command gave the same error.
Hi Kaushik,
I have updated the Dockerfile, Now SSH is working.
There is a little problem.
root@localhost’s password:
Permission denied, please try again.
I think because root ssh login is disabled default. Let’s create new user and use it to ssh connect.
useradd username