Docker is an open-source platform that allows developers to automate the deployment, scaling, and management of applications. It does so by creating lightweight, self-sufficient containers, that can run virtually anywhere. SSH (Secure Shell) is a protocol that provides a secure channel over an unsecured network, typically used to log into another computer over a network and execute commands. This article provides a step-by-step guide on setting up an Ubuntu Docker container with SSH access.

Advertisement

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 powerful tools in the realm of devOps and development. By integrating them, you can create a more flexible, more secure, and more powerful development environment. This comprehensive guide walked you through the process of setting up an Ubuntu Docker container with SSH access. With this knowledge, you can now create and access Docker containers with ease and confidence. Always remember to exercise good security practices, like changing default passwords and regularly updating software, to keep your environments safe and secure.

Share.

4 Comments

  1. 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.

  2. 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.

  3. 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

Leave A Reply


Exit mobile version