[Docker] コンテナ上のアプリを動かす

dockerの場合はdaemonで動かすのではなく、アプリを直接動かすのが基本
起動時のコマンドが全てのプロセスの原点となる
-> bashを抜けるとコンテナが停止する

$ sudo docker run -it –name centos centos:7.7.1908 bash

start.sh

#!bin/sh
handle() {
	echo 'handle sigterm/sigint'
	exit 0
}
trap handle TERM INT

nginx -g "daemon off;" &
wait

Dockerfile

From python:3.7.5-slim
RUN pip install flask=1.1.1
COPY ./server.py /server.py
ENV PORT 80
STOPSIGNAL SIGINT
CMD ["python", "-u", "/server.py"]

### supervisorで複数の子プロセス(アプリ本体)を管理

From centos:7.7.1908
RUN yum install -y epel-release \
	&& yum install -y http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm \
	&& yum -y install nginx openssh-server supervisor \
	&& rm -rf /var/cache/yum/* && yum clean all
RUN ssh-keygen -A
COPY supervisord.conf /etc/supervisord.conf
EXPOSE 22 80
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D
autostart=true
autorestart=true

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true

うーん、結構奥が深いな…