LoginSignup
10

More than 3 years have passed since last update.

Docker amazonlinux2 で systemctl を使いたい人生だった

Last updated at Posted at 2020-04-06

TLDR;

Dockerfile
FROM amazonlinux:2

RUN yum update -y \
    && yum install systemd-sysv -y

CMD ["/sbin/init"]
docker-compose.yml
version: "2"
services:
  web:
    image: amazonlinux:2
    build: .
    container_name: al2c
    privileged: true
    ports:
      - 8080:80
$ docker-compose up --build -d

amazonlinux2では systemctl がデフォルトでは使えない

amazonlinux2のコンテナを普通に立ち上げただけだと、 systemctl は使えない。
コマンド自体は存在する様だが、権限がないよーと怒られる。

Failed to get D-Bus connection: Operation not permitted

色々情報を読み齧った結果、次の要素が必要であることがわかった。

  • privileged を有効にする
  • /sbin/init で起動する

チャレンジその1

じゃぁこれでいけるだろう。 privilegedtrue にして起動コマンドに /sbin/init を設定した。

docker-compose.yml
version: "2"
services:
  web:
    image: amazonlinux:2
    container_name: al2c
    command: /sbin/init
    privileged: true
    ports:
      - 8080:80

だめでした。

ERROR: for al2c  Cannot start service web: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/sbin/init\": stat /sbin/init: no such file or directory": unknown

/sbin/init なんてものは存在しませんと。先んじて何かを導入してやる必要がありそう。

チャレンジその2

Dockerfile を用意して必要な物を入れてから起動。ついでに起動コマンド(/sbin/init)もこちらに移動した。

Dockerfile
FROM amazonlinux:2

RUN yum update -y \
    && yum install systemd-sysv -y

CMD ["/sbin/init"]
docker-compose.yml
version: "2"
services:
  web:
    image: amazonlinux:2
    build: .
    container_name: al2c
    privileged: true
    ports:
      - 8080:80
$ docker exec -it al2c sh
sh-4.2# systemctl 
  UNIT                                   LOAD   ACTIVE     SUB       JOB   DESCRIPTION
  dev-sda1.device                        loaded activating tentative       /dev/sda1
  dev-ttyS0.device                       loaded inactive   dead      start dev-ttyS0.device
  -.mount                                loaded active     mounted         /
  dev-hugepages.mount                    loaded active     mounted         Huge Pages File System
  dev-mqueue.mount                       loaded active     mounted         POSIX Message Queue File System
...

無事動いたみたいです。:raised_hands:

nginxとかでもよさそう

上では systemd-sysv を入れていますが、 nginx を入れただけでも依存パッケージの解決でいけました。多分 apache でもいけるんじゃないかな、試してませんが。どうせいずれかは入れると思うので、それならそれでよろしいのではないかと存じます。

Dockerfile
FROM amazonlinux:2

RUN yum update -y \
    && amazon-linux-extras install nginx1

CMD ["/sbin/init"]

Amazon Linux では、 nginxamazon-linux-extras コマンドでインストールできます。

お世話になった記事

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10