Debian+Systemd:Dockerイメージの作成

コンテナ内で複数のアプリ・サービスを稼働するため、システムデーモンsystemdを組込んだベースとなるDockerイメージを作成、このベースイメージを雛形にし、必要なサービス(アプリ)を導入した新規イメージを作成します。

Dockerhub Debian
https://hub.docker.com/_/debian

jrei/systemd-debian
https://hub.docker.com/r/jrei/systemd-debian/dockerfile

ベースイメージのDockerfile

project/debian-buster-systemd-image/Dockerfile

FROM debian:buster

ENV container docker
ENV LC_ALL C
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update \
    && apt-get install -y systemd \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN rm -f /lib/systemd/system/multi-user.target.wants/* \
    /etc/systemd/system/*.wants/* \
    /lib/systemd/system/local-fs.target.wants/* \
    /lib/systemd/system/sockets.target.wants/*udev* \
    /lib/systemd/system/sockets.target.wants/*initctl* \
    /lib/systemd/system/sysinit.target.wants/systemd-tmpfiles-setup* \
    /lib/systemd/system/systemd-update-utmp*

VOLUME [ "/sys/fs/cgroup" ]

CMD ["/lib/systemd/systemd"]

ベースイメージの作成

$ docker build --rm -t local/buster-systemd .

ベースイメージから、デーモン起動するサービス・アプリを導入したイメージを作成するためのDockerfile:nginx-busterを作成。

project/nginx-buster

FROM local/buster-systemd

RUN apt-get update && apt-get install -y \
  bash \
  nano \
  tzdata \
  git \
  nginx \
  certbot \
  dhcpcd5 \
  hostapd \
  dnsmasq \
  iptables-persistent \
  vnstat \
  qrencode \
  php-cgi \
  php php-fpm \
  init \
  && cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
  && echo "Asia/Tokyo" > /etc/timezone

# Set working directory
WORKDIR /var/www/html

COPY dnsmasq.conf /etc/

RUN systemctl enable nginx.service hostapd.service php7.3-fpm.service dnsmasq.service
CMD ["/sbin/init"]

docker-composeファイルの作成

project/docker-compose.yml

version: '3.5'

services:
  # nginx https://hub.docker.com/_/nginx?tab=description
  nginx:
    container_name: nginx
    build: 
      context: ./docker_files
      dockerfile: nginx-buster
    tty: true
    privileged: true
    volumes:
      # note) host directory mount into docker container directory (one way:host>container)
      # shared nginx default.conf between host and container
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      # shared the directory /var/www/html in php-fpm container
      - ./html:/var/www/html
      # letsencrypt, cretated by nginx-certbot docker-compose 
      - letsencrypt:/etc/letsencrypt
      # For RaspAP
      - /sys/fs/cgroup:/sys/fs/cgroup:ro
    cap_add:
      - SYS_ADMIN
    restart: unless-stopped
    network_mode: host

volumes:
    letsencrypt:
      external: true

コンテナのビルド・起動

$ docker-compose up -d

ビルド

$ docker-compose build nginx

注)php7.3-fpm.sock エラー

$ docker exec -ti nginx bash

# mkdir /var/run/php
# service php7.3-fpm restart