Create Docker Container with SSH Access using Dockerfile

Membuat Docker Container dengan Akses SSH menggunakan Dockerfile

  • Buat folder ubuntu-ssh
root@sys-ops:~# mkdir ubuntu-ssh && cd ubuntu-ssh
  • Buat file Dockerfile
root@sys-ops:~/ubuntu-ssh# nano Dockerfile
  • Gunakan image ubuntu:22.04
  • update repository dan install openssh-server nano net-tools
  • set password root P@ssw0rd!123
  • allow root login dan allow auth password
  • publish port 22
# use image ubuntu 22.04
FROM ubuntu:22.04

# update repository & install open ssh server
RUN apt update && apt upgrade -y && apt install -y openssh-server nano net-tools

# create directory /var/run/sshd
RUN mkdir /var/run/sshd

# set password root
RUN echo 'root:P@ssw0rd!123' | chpasswd

# allow root login
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/#PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config

RUN sed 's@session\s*required\s* optional' -i /etc/pam.d/sshd

# publish port 22 ssh

# run ssh on background
CMD ["/usr/sbin/sshd", "-D"]
  • Build image dockerfile dengan nama image ubuntu-ssh
root@sys-ops:~/ubuntu-ssh# docker build -t ubuntu-ssh .

Sending build context to Docker daemon   2.56kB
Step 1/9 : FROM ubuntu:22.04
 ---> d2e4e1f51132
Step 2/9 : RUN apt update && apt upgrade -y && apt install -y openssh-server nano net-tools
 ---> Using cache
 ---> 0ce3cb6f3d50
Step 3/9 : RUN mkdir /var/run/sshd
 ---> Using cache
 ---> 8efb730944fb
Step 4/9 : RUN echo 'root:P@ssw0rd!123' | chpasswd
 ---> Using cache
 ---> 26c21ad0e9a7
Step 5/9 : RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
 ---> Running in 59b62f111842
Removing intermediate container 59b62f111842
 ---> 32dc614a3ae2
Step 6/9 : RUN sed -i 's/#PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
 ---> Running in 950febc29b48
Removing intermediate container 950febc29b48
 ---> fe0a32d2e693
Step 7/9 : RUN sed 's@session\s*required\s* optional' -i /etc/pam.d/sshd
 ---> Running in 71f9844cd395
Removing intermediate container 71f9844cd395
 ---> 07efcf587816
Step 8/9 : EXPOSE 22
 ---> Running in f56ab0fcd988
Removing intermediate container f56ab0fcd988
 ---> c1bb6348ccc5
Step 9/9 : CMD ["/usr/sbin/sshd", "-D"]
 ---> Running in 38f3940d9900
Removing intermediate container 38f3940d9900
 ---> 732e30b6fcd0
Successfully built 732e30b6fcd0
Successfully tagged ubuntu-ssh:latest
  • Jalanakan container menggunakan image ubuntu-ssh dengan nama container: ubuntu2204-ssh
root@sys-ops:~/ubuntu-ssh# docker run -d -p 2022:22 --name ubuntu2204-ssh --restart always ubuntu-ssh
  • Cek status container
root@sys-ops:~/ubuntu-ssh# docker ps -a
CONTAINER ID   IMAGE                           COMMAND                  CREATED              STATUS              PORTS                                   NAMES
8c869f3d277c   ubuntu-ssh                      "/usr/sbin/sshd -D"      About a minute ago   Up About a minute>22/tcp, :::2022->22/tcp   ubuntu2204-ssh
  • Cek IP address container ubuntu2204-ssh
root@sys-ops:~/ubuntu-ssh# docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ubuntu2204-ssh
  • Akses ssh ke dalam container ubuntu2204-ssh
root@sys-ops:~/ubuntu-ssh# ssh [email protected]

The authenticity of host ' (' can't be established.
ED25519 key fingerprint is SHA256:TYI9zLedyvl7XuzIb3xbbvv70j4l8L94u5Qxj6zzvJA.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '' (ED25519) to the list of known hosts.
[email protected]'s password:  <masukan password root>
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-33-generic x86_64)

 * Documentation:
 * Management:
 * Support:

This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, you can run the 'unminimize' command.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc//copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@8c869f3d277c:~# cat /etc/issue
Ubuntu 22.04 LTS \n \l



A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.