Docker를 가지고 해당 Container에 SSH 서비스를 운영할수 있는 방법에 대해 설명 드리겠습니다.

Dockerfile 문서 파일을 생성후 아래와 같이 저장 합니다.

root@ruo91:~# nano Dockerfile
#
# SSH Daemon Service
# 
# Images를 선택
FROM     ubuntu:12.04

MAINTAINER Yongbok Kim <ruo91@yongbok.net>

# 기존 미러 서버를 한국 서버로 변경
RUN sed -i 's/archive.ubuntu.com/ftp.neowiz.com/g' /etc/apt/sources.list

# 최신버전으로 업데이트 후 SSH 및 필요 패키지 설치 
RUN apt-get update ; apt-get install -y openssh-server aptitude net-tools curl

# SSH 관련 설정
RUN mkdir /var/run/sshd
RUN sed  -i "/^[^#]*UsePAM/ s/.*/#&/"  /etc/ssh/sshd_config
RUN echo "UsePAM no" >> /etc/ssh/sshd_config

# Root 비밀번호 설정
RUN echo 'root:123456789' |chpasswd

# SSH 포트 설정
# HostOS의 랜덤포트 -> Container 22번 포트를 바라보게 합니다.
# 즉, 외부에서 HostOS의 랜덤 포트로 접속하면 해당 Container에 접속 할수 있습니다.
# 더 자세한건 http://docs.docker.io/reference/builder/#expose 를 참고 하세요.
EXPOSE 22

# SSH 실행
CMD    /usr/sbin/sshd -D

 

Dockerfile 생성 이후에 sshd 라는 이미지를 만듭니다.

root@ruo91:~# docker build --rm -t sshd .
Uploading context 3.584 kB
Uploading context
Step 0 : FROM     74fe38d11401
 ---> 74fe38d11401
Step 1 : MAINTAINER Yongbok Kim <ruo91@yongbok.net>
 ---> Running in e014eef5327e
 ---> 930fa3d390d5
Step 2 : RUN sed -i 's/archive.ubuntu.com/ftp.neowiz.com/g' /etc/apt/sources.list
 ---> Running in da2d95b9222a
 ---> 7e9b0e8d1c76
Step 3 : RUN apt-get update ; apt-get install -y openssh-server
 ---> Running in e11bedea279f
Get:1 http://ftp.neowiz.com precise Release.gpg [198 B]
Get:2 http://ftp.neowiz.com precise-updates Release.gpg [198 B]
Get:3 http://ftp.neowiz.com precise-security Release.gpg [198 B]
Get:4 http://ftp.neowiz.com precise Release [49.6 kB]
Get:5 http://ftp.neowiz.com precise-updates Release [49.6 kB]
Get:6 http://ftp.neowiz.com precise-security Release [49.6 kB]
Get:7 http://ftp.neowiz.com precise/main Sources [1175 kB]
Get:8 http://ftp.neowiz.com precise/restricted Sources [5306 B]
Get:9 http://ftp.neowiz.com precise/universe Sources [6239 kB]
....... Blah blah.......
.........................Blah blah......
................................................
ldconfig deferred processing now taking place
 ---> 0bfc548c5b70
Step 4 : RUN mkdir /var/run/sshd
 ---> Running in 5fe2ff192daf
 ---> fbb91b469aa2
Step 5 : RUN sed  -i "/^[^#]*UsePAM/ s/.*/#&/"  /etc/ssh/sshd_config
 ---> Running in 5928b6e26d12
 ---> aad27b0563f6
Step 6 : RUN echo "UsePAM no" >> /etc/ssh/sshd_config
 ---> Running in 2c7b1bdf9d40
 ---> 619ba6ad2a74
Step 7 : RUN echo 'root:123456789' |chpasswd
 ---> Running in d9aeccface23
 ---> 52c2268728e5
Step 8 : EXPOSE 4000 22
 ---> Running in deb95bfe5566
 ---> 4edce08a3ca7
Step 9 : CMD    /usr/sbin/sshd -D
 ---> Running in fa21cdb8ca07
 ---> bbeeb5952a8f
Successfully built bbeeb5952a8f
Removing intermediate container e014eef5327e
Removing intermediate container da2d95b9222a
Removing intermediate container e11bedea279f
Removing intermediate container 5928b6e26d12
Removing intermediate container deb95bfe5566
Removing intermediate container fa21cdb8ca07
Removing intermediate container 5fe2ff192daf
Removing intermediate container 2c7b1bdf9d40
Removing intermediate container d9aeccface23

 

이후 해당 명령어로 살펴보면 sshd 라는 이미지가 생성이 됩니다.

root@ruo91:~# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
sshd                       latest              bbeeb5952a8f        3 minutes ago       338.9 MB

 

해당 이미지를 Detached mode로 띄워서 실행 해봅시다.

root@ruo91:~# docker run -d -P --name ubuntu_12.04_LTS_SSHD sshd
94574b4abdaf05e305846619cde6d2a27a64364816f94af92267bec2b00506b2

 

그럼, HostOS의 49156번의 포트가 해당 Container의 22번 포트를 바라보고 있는걸 알수 있습니다.

root@ruo91:~# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                   NAMES
94574b4abdaf        sshd:latest         /bin/sh -c '/usr/sbi   6 minutes ago      Up 6 minutes       0.0.0.0:49156->22/tcp   ubuntu_12.04_LTS_SSHD

 

이제 실제로 접속 해보면 잘되는걸 볼수 있습니다.

root@ruo91:~# ssh root@localhost -p 49156

docker-ssh