supervisor + ssh + cron 알파인 도커 이미지 빌드

1 개요[ | ]

supervisor + ssh + cron 알파인 도커 이미지 빌드 만들기

2 supervisord.conf 작성[ | ]

[unix_http_server]
file=/var/run/supervisor.sock
chmod=0700
[supervisord]
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock

[program:sshd]
command = /usr/sbin/sshd -D
[program:cron]
command = crond -f -L /var/log/cron.log
→ 상단은 supervisor 자체에 대한 설정. supervisorctl을 사용하려면 이 정도는 설정해줘야 한다.
→ 하단은 supervisor로 구동할 프로그램에 대한 설정

3 Dockerfile 작성[ | ]

vi Dockerfile
FROM alpine:3.11
RUN set -x \
&& apk add --no-cache \
  dcron \
  openssh \
  supervisor \
&& mkdir -p /var/run/sshd \
&& sed 's/#PermitRootLogin.*/PermitRootLogin yes/' -i /etc/ssh/sshd_config \
&& sed 's/#PasswordAuthentication.*/PasswordAuthentication yes/' -i /etc/ssh/sshd_config \
&& echo 'root:P@ssw0rd' | chpasswd \
&& ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa \
&& ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa \
&& echo '* * * * * date >> /test.txt' > /etc/crontabs/root

COPY supervisord.conf /etc/
EXPOSE 22
CMD ["supervisord","-n","-c","/etc/supervisord.conf"]

4 docker build[ | ]

$ docker build -t alpine_supervisor_ssh_cron .
Sending build context to Docker daemon  3.584kB
Step 1/5 : FROM alpine:3.11
3.11: Pulling from library/alpine
aad63a933944: Pull complete 
Digest: sha256:b276d875eeed9c7d3f1cfa7edb06b22ed22b14219a7d67c52c56612330348239
Status: Downloaded newer image for alpine:3.11
 ---> a187dde48cd2
Step 2/5 : RUN set -x && apk add --no-cache   dcron   openssh   supervisor && mkdir -p /var/run/sshd && sed 's/#PermitRootLogin.*/PermitRootLogin yes/' -i /etc/ssh/sshd_config && sed 's/#PasswordAuthentication.*/PasswordAuthentication yes/' -i /etc/ssh/sshd_config && echo 'root:P@ssw0rd' | chpasswd && ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa && ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa && echo '* * * * * date >> /test.txt' > /etc/crontabs/root
 ---> Running in 2a2c71f07e5d
+ apk add --no-cache dcron openssh supervisor
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/21) Installing dcron (4.5-r4)
(2/21) Installing openssh-keygen (8.1_p1-r0)
(3/21) Installing ncurses-terminfo-base (6.1_p20200118-r3)
... (생략)
Step 3/5 : COPY supervisord.conf /etc/
 ---> 76fd2d46a667
Step 4/5 : EXPOSE 22
 ---> Running in b0f3070f30d3
Removing intermediate container b0f3070f30d3
 ---> 28ca847700a3
Step 5/5 : CMD ["supervisord","-n","-c","/etc/supervisord.conf"]
 ---> Running in cf02495b35b6
Removing intermediate container cf02495b35b6
 ---> 05bf55717e08
Successfully built 05bf55717e08
Successfully tagged alpine_supervisor_ssh_cron:latest

5 docker run[ | ]

$ docker run -d -P --name my_container alpine_supervisor_ssh_cron
086b52c74a1853e80ea9bd53429ee9c954ae6cfbe97282b3101701fd3a26c4fa
$ docker ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                   NAMES
086b52c74a18        alpine_supervisor_ssh_cron   "supervisord -n -c /…"   8 seconds ago       Up 8 seconds        0.0.0.0:32773->22/tcp   my_container
$ docker logs my_container
2020-04-15 11:13:30,915 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
2020-04-15 11:13:30,922 INFO RPC interface 'supervisor' initialized
2020-04-15 11:13:30,922 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2020-04-15 11:13:30,923 INFO supervisord started with pid 1
2020-04-15 11:13:31,926 INFO spawned: 'cron' with pid 7
2020-04-15 11:13:31,930 INFO spawned: 'sshd' with pid 8
2020-04-15 11:13:32,936 INFO success: cron entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2020-04-15 11:13:32,937 INFO success: sshd entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

6 테스트[ | ]

$ ssh root@localhost -p 32773
The authenticity of host '[localhost]:32773 ([127.0.0.1]:32773)' can't be established.
RSA key fingerprint is SHA256:q7w2+foZY8GvhciCs9Ah0lCKPxbUoUevU/VQRuYrhS4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:32773' (RSA) to the list of known hosts.
root@localhost's password: P@ssw0rd
Welcome to Alpine!

The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org/>.

You can setup the system with the command: setup-alpine

You may change this message by editing /etc/motd.

086b52c74a18:~#
086b52c74a18:~# ps -ef
PID   USER     TIME  COMMAND
    1 root      0:00 {supervisord} /usr/bin/python3 /usr/bin/supervisord -n -c /etc/supervis
    7 root      0:00 crond -f -L /var/log/cron.log
    8 root      0:00 /usr/sbin/sshd -D
   10 root      0:00 sshd: root@pts/0
   12 root      0:00 -ash
   13 root      0:00 ps -ef
086b52c74a18:~# supervisorctl status
cron                             RUNNING   pid 7, uptime 0:01:31
sshd                             RUNNING   pid 8, uptime 0:01:31
086b52c74a18:~# crontab -l
* * * * * date >> /test.txt
086b52c74a18:~# cat /test.txt
Wed Apr 15 11:14:01 UTC 2020
Wed Apr 15 11:15:01 UTC 2020
086b52c74a18:~# exit
Connection to localhost closed.
$

7 같이 보기[ | ]

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