Create a Docker container with SSH access

Introduction

Basics

Let us download the image with centos

docker pull centos

Let us make sure the image has appeared in the list

docker images

Let us create a docker folder and change into it

mkdir docker
cd docker

Let us create a centos-ssh folder and change into it

mkdir centos-ssh
cd centos-ssh

Let us create a file with the ssh settings sshd_config and start editing it

touch sshd_config
nano sshd_config

Make a small config, for example like this:

Port 22 Protocol 2 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_dsa_key HostKey /etc/ssh/ssh_host_ecda_key HostKey /etc/ssh/ssh_host_ed25519_key LoginGraceTime 120 PermitRootLogin yes StrictModes yes RSAAuthentication yes PubkeyAuthentication yes UsePAM yes

Create a file with the Docker settings - Dockerfile and edit it

touch Dockerfile
nano Dockerfile

FROM centos RUN yum -y update RUN yum install -y openssh-server RUN ssh-keygen -A ADD ./sshd_config /etc/ssh/sshd_config RUN echo root:Secret123 | chpasswd CMD /usr/sbin/sshd -D

Build the image

docker build -t andrei-ubuntu/centos_ssh .

Now run it

docker run -it -d --name node1 -p 2201:22 andrei-ubuntu/centos_ssh

Make sure its status is Up

docker ps -a

If you do not know which IP you are on, execute

ip a

Connect over SSH

ssh root@192.168.0.100 -p 2201

Article author: Andrei Olegovich