Docker : Conteneur avec ssh

1/ Création du répertoire de travail.

util01@station66:~$ mkdir -p DOCKER/contssh
util01@station66:~$ cd DOCKER/contssh/

2/ Création du fichier de configuration.

Créer :


Ajouter :

FROM ubuntu:latest

RUN apt update

RUN apt install  openssh-server sudo -y

RUN useradd -rm -d /home/util01 -s /bin/bash -g root -G sudo -u 1000 util01

RUN usermod -aG sudo util01

RUN service ssh start

RUN  echo 'util01:mot2passe' | chpasswd


CMD ["/usr/sbin/sshd","-D"]

3/ Construction de l'image.

util01@station66:~/DOCKER/contssh$ docker build -t container01 .

4/ Lancement de l'image.

util01@station66:~/DOCKER/contssh$ docker run -p 2222:22 container01

5/ Connexion à l'image.

util01@station66:~/DOCKER/contssh$ docker ps
CONTAINER ID   IMAGE         COMMAND               CREATED          STATUS          PORTS                                             NAMES
3b59b57789d5   container01   "/usr/sbin/sshd -D"   28 seconds ago   Up 27 seconds   2222/tcp,>22/tcp, :::2222->22/tcp   condescending_payne

6/ Afficher l'adresse ip du conteneur.

util01@station66:~/DOCKER/contssh$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 3b59b57789d5

7/ Connexion ssh sur le conteneur.

util01@station66:~/DOCKER/contssh$ ssh util01@
The authenticity of host ' (' can't be established.
ECDSA key fingerprint is SHA256:ATuP7g7rWrXpZ8i4C3zaFVaIWlN7qtMC0KylbcaOTRw.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/home/util01/.ssh/known_hosts).
util01@'s password: 
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 4.15.0-99-generic x86_64)
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.


8/ Arrêter un conteneur

util01@station66:~/DOCKER/contssh$ docker stop 1eaf9f0d98bf