SSH 접속 가능한 데비안 도커 이미지 빌드

1 개요[ | ]

SSH 접속 가능한 debian 도커 이미지 빌드
SSH 접속 가능한 데비안 도커 이미지 빌드

2 dockerfile 작성[ | ]

vi Dockerfile
FROM debian:10-slim

RUN set -x \
&& apt-get update -y \
&& apt-get install -y --no-install-recommends \
  openssh-server \
&& rm -rf /var/lib/apt/lists/* \
&& mkdir /var/run/sshd \
&& echo 'root:P@ssw0rd' | chpasswd \
&& sed 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' -i /etc/ssh/sshd_config \
&& sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

3 docker build[ | ]

$ docker build -t ssh_debian .
Sending build context to Docker daemon  2.048kB
Step 1/10 : FROM debian
latest: Pulling from library/debian
f15005b0235f: Pull complete                                                                                             Digest: sha256:125ab9ab9718f4dba6c3342407bb1923afce4f6b2a12b3a502d818274db9faf9
Status: Downloaded newer image for debian:latest
 ---> 58075fe9ecce
Step 2/10 : RUN apt-get update && apt-get install -y openssh-server
 ---> Running in 81986c343b34
Get:1 http://deb.debian.org/debian buster InRelease [122 kB]
Get:2 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:3 http://deb.debian.org/debian buster-updates InRelease [49.3 kB]
Get:4 http://security.debian.org/debian-security buster/updates/main amd64 Packages [187 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7907 kB]
Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [7380 B]
... (생략)
Step 8/10 : RUN echo "export VISIBLE=now" >> /etc/profile
 ---> Running in 73d8c3292e4f
Removing intermediate container 73d8c3292e4f
 ---> c634c789ff98
Step 9/10 : EXPOSE 22
 ---> Running in 3564de31f638
Removing intermediate container 3564de31f638
 ---> 24feaa1902b3
Step 10/10 : CMD ["/usr/sbin/sshd", "-D"]
 ---> Running in 51073ae7ffa2
Removing intermediate container 51073ae7ffa2
 ---> 3eae6b65c4b5
Successfully built 3eae6b65c4b5
Successfully tagged ssh_debian:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
$ docker image list
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ssh_debian          latest              3eae6b65c4b5        50 minutes ago      172MB
debian              latest              58075fe9ecce        11 days ago         114MB

4 docker run[ | ]

$ docker run -d -P --name ssh_debian_hello ssh_debian
e8ca671afa2a42a134e1a2bd704f62087bd1d81e19b87be6055d50496e8d803f
$ docker ps
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
e8ca671afa2a        ssh_debian          "/usr/sbin/sshd -D"   3 seconds ago       Up 2 seconds        0.0.0.0:32770->22/tcp   ssh_debian_hello

5 SSH 로그인 테스트[ | ]

docker@default:~$ ssh root@localhost -p 32770
The authenticity of host '[localhost]:32770 ([127.0.0.1]:32770)' can't be established.
ECDSA key fingerprint is SHA256:3dPLZvnuZrHELH2XQxiFOf0MRHH6e7p5ltkwJFNjxwo.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:32770' (ECDSA) to the list of known hosts.
root@localhost's password: P@ssw0rd
Linux e8ca671afa2a 4.14.154-boot2docker #1 SMP Thu Nov 14 19:19:08 UTC 2019 x86_64

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

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Apr 11 04:29:55 2020 from 172.17.0.1
root@e8ca671afa2a:~# ps -ef                                                                                             
UID        PID  PPID  C STIME TTY          TIME CMD                                                                     
root         1     0  0 04:28 ?        00:00:00 /usr/sbin/sshd -D                                                       
root        29     1  0 04:44 ?        00:00:00 sshd: root@pts/0                                                        
root        35    29  0 04:44 pts/0    00:00:00 -bash                                                                   
root        38    35  0 04:45 pts/0    00:00:00 ps -ef
root@e8ca671afa2a:~# exit                                                                                               
logout                                                                                                                  
Connection to localhost closed.
docker@default:~$

6 뒷정리[ | ]

docker rm -f ssh_debian_hello 
docker rmi ssh_debian

7 같이 보기[ | ]

8 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}