Systemctl cannot be used on Ubuntu inside Docker container

Introduction

When creating a system or application, I thought that if it could be built on a Docker container, it would lead to efficiency in development and maintenance, so I tried various things, so I will summarize the basics.

Prerequisites

[Host OS] ・ Ubuntu20.04 LTS (on GCP)

[Soft Image Version] ・ Docker 19.03.13 ・ OS Ubuntu20.04 LTS of container image

What you want to do

    1. installing and configuring docker
  1. Create container in docker
    1. Enter the container and start Apache
  2. Modify the image file and reimage it

1. 1. installing and configuring docker

When installing from the Docker repository, follow the procedure below.

Make sure Docker is not installed

$ sudo dpkg -l docker

Package management tool updates

$ sudo apt update

⇒ If dpkg-query: no packages found matching docker is displayed, it is not installed.

Software installation required for Docker installation

$ sudo apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

Added Docker official GPG public key

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Check if the public key has been added properly

$ sudo apt-key fingerprint

Qiita-no010_img04.jpg

Add repository

Confirm that there is no docker in the repository.

$ cat /etc/apt/sources.list | grep docker

Repository settings

python


$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

Confirm that the repository has been added

python


$ cat /etc/apt/sources.list | grep docker
deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
# deb-src [arch=amd64] https://download.docker.com/linux/ubuntu focal stable

Check available version

python


$ apt-cache madison docker-ce
 docker-ce | 5:19.03.13~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.12~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.11~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.10~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:19.03.9~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages

install docker

Since the repository has been added, ʻupdate again and then run ʻinstall

python


$ sudo apt update
$ sudo apt install docker-ce

Check the installed docker version

python


$ sudo docker version
Client: Docker Engine - Community
 Version:           19.03.13
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        4484c46d9d
 Built:             Wed Sep 16 17:02:52 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Check if the daemon is started

python


$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-09-30 00:22:29 UTC; 30min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 25020 (dockerd)
      Tasks: 9
     Memory: 36.8M
     CGroup: /system.slice/docker.service

Operation check of docker

Hello world Get a sample docker image and run it.

python


$ sudo docker container run hello-world

Check the list of current images and containers

python


#List images
$ sudo docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        9 months ago        13.3kB

#List of containers (including stopped)
$ sudo docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS   
            NAMES
e8a73ecd0c16        hello-world         "/hello"            5 minutes ago       Exited (0) 5 minutes ago           
            condescending_diffie

#Display of running container
$ sudo docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS          
     NAMES

Since I was able to confirm the operation, I deleted the container and image once.

python


$ sudo docker container rm [Container ID]
$ sudo docker image rm [Image ID]

docker settings

Auto start settings

$ sudo systemctl unmask docker.service
$ sudo systemctl enable docker
$ sudo systemctl is-enabled docker

Added to docker group to execute docker command without sudo.

python


#First of all[docker]Group confirmation. * If you do not have a group, you need to create one yourself.
$ cat /etc/group | grep docker
docker:x:998:

# [docker]Add users to the group.
$ sudo usermod -aG docker [username]

After restarting Ubuntu, if you can execute the command without sudo as shown below, you can change the settings.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

2. Create container in docker

Since the container is built using the docker image, first get the docker image. Many images have already been published on the docker site, so search for the desired image with the search command and get the image.

Confirmation of the image to be acquired

python


$docker search [arbitrary keyword]

Get image

python


# docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
$ docker image pull  ubuntu:20.04

20.04: Pulling from library/ubuntu
d72e567cc804: Pull complete 
0f3630e5ff08: Pull complete 
b6a83d81d1f4: Pull complete 
Digest: sha256:bc2f7250f69267c9c6b66d7b6a81a54d3878bb85f1ebb5f951c896d13e6ba537
Status: Downloaded newer image for ubuntu:20.04
docker.io/library/ubuntu:20.04

Create container from image

shell::


$ docker container run -it -d --name test-ubuntu20-4 ubuntu:20.04

#When you want to associate a port number
# docker container run -it -d -p 8080(Ubuntu side port):5000(Port on the container side) --name webgis-server ubuntu:20.04

** Check the running container **

python


$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS          
     NAMES
19b7ac7c40ad        ubuntu:20.04        "/bin/bash"         8 seconds ago       Up 7 seconds                       
     test-ubuntu20-4

3. 3. Enter the container and start Apache

Enter the container (test-ubuntu20-4) created earlier

$ docker attach test-ubuntu20-4

Package management tool updates.

# apt update

Apache installation

# apt install -y apache2

Check the startup status of Apache.

# systemctl status apache2
bash: systemctl: command not found

Since the above message is displayed and it cannot be operated, install the package so that systemctl can be used. (Simple startup etc. can be done with the command service apache2 ~~~~.)

# apt install -y systemd

Check the startup status of Apache again.

# systemctl status apache2
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

There seems to be a problem with PID1, so let's check it.

# ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.5  0.1   4240  3384 pts/0    Ss   16:40   0:00 /bin/bash
root           8  0.0  0.1   5888  2912 pts/0    R+   16:40   0:00 ps aux 

Actually, when you create a container with the docker container run -it -d --name ubuntu: 20.04 command, the COMMAND of [PID: 1] of Ubuntu in the container becomes / sbin / bash. In this case, it seems that the systemctl command cannot be used.

python


#Since it is a test execution, it is not necessary to execute it as a series of work this time.
$ docker container run -it -d --privileged --name webgis-server ubuntu:20.04 /sbin/init

docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process c
aused "exec: \"/sbin/init\": stat /sbin/init: no such file or directory": unknown.

** It may be possible to image Ubuntu where / sbin / init exists! ** **

4. Modify the image file and reimage it

Create / sbin / init as a symbolic link to/ usr / lib / systemd / system /.

# ln -s  /usr/lib/systemd/system/ /sbin/init

Since it is a container with ** Apache2 and Systemd installed and / sbin / init created ** on the original image, create an image from the container that was temporarily taken out of the container and stopped.

python


$ docker commit test-ubuntu20-4 test-ubuntu20-4:add-init
sha256:5f25a8ff7149b22665aeb4d076919ba06d7e5c3f06c77834e60c6a7e042e6bf2

#Check the list of images
$ $ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
test-ubuntu20-4     add-init            91fffb874bf9        About a minute ago   202MB
ubuntu              20.04               9140108b62dc        2 weeks ago          72.9MB
hello-world         latest              bf756fb1ae65        9 months ago         13.3kB

Create container again from image file

Create a container from the image created earlier

$ docker container run -it -d --privileged --name test-ubuntu20-4-2 test-ubuntu2
0-4:add-init /sbin/init

af59767d9b17c60fcb5284ec3669bc61edb5f4a7e5a019b5c572997553d43e9f
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process c
aused "exec: \"/sbin/init\": permission denied": unknown.

It is said that I do not have access rights this time, and it seems difficult to start it with [PID1 = / sblin / init] after all. I've researched various things, but basically systemd cannot be used as a process management tool in the docker container. (There may be nothing you can't do if you force it, but there seems to be no merit to do so.) ・ [Reference Site 1](https://www.it-swarm-ja.tech/ja/boot/ubuntu%E3%81%8C%E8%B5%B7%E5%8B%95%E3%81%97 % E3% 81% AA% E3% 81% 84% EF% BC% 9Aruninit% EF% BC% 9A-sbin-init% EF% BC% 9Apermission-denied-and-bin-sh% EF% BC% 9A0% EF % BC% 9Acan-not-open-splash / 961074669 / amp / ) ・ Reference Site 2

Summary

First of all, I briefly summarized the procedure for installing docker and creating a container, but since the kernel used for operation is different between VM and container, process management tools cannot be used in the same way. When creating a service in a container, understand that the program that daemonizes [PID1 = init] does not work. When managing processes with docker, it seems that ** Supervisor ** is common.

Recommended Posts

Systemctl cannot be used on Ubuntu inside Docker container
Java cannot be installed on Ubuntu 13.04
Run NordVPN on Docker (Windows) Ubuntu container
How to configure ubuntu to be used on GCP
Install Docker on Ubuntu Server 20.04
Because getSupportLoaderManager cannot be used
Oracle Java 8 on Docker Ubuntu
CentOS7 VirtualBOX yum cannot be used
Ubuntu on WSL2: Cannot connect to the Docker daemon at unix
Run React on a Docker container
Run GUI application on Docker container
[PHP] Solved "mb_strpos cannot be used"
Run PureScript on a Docker container
Pg_resetwal can be used to start the PostgreSQL Docker container when WAL is broken and cannot be started.
A memorandum when IME cannot be turned on with VS Code (Ubuntu 20.04)
[Java] Variables declared inside the `for statement` cannot be used outside the` for statement block`
[Rails] Rails new cannot be done from Docker
Launch docker container on EC2 (personal memorandum)
Use docker in proxy environment on ubuntu 20.04.1
Microservices 101-I tried putting Docker on Ubuntu-
Try putting Docker in ubuntu on WSL
Until you run apache on ubuntu on docker
Run Ubuntu + ROS with Docker on Mac
Check when the container cannot be accessed from Host in the Laravel on docker environment using VS Code's Remote container.
Monitor the Docker container and SystemD process on the same host with Zabbix on Ubuntu.
Run GUI application on Docker container (Japanese input)
[Rails 6] method :: delete cannot be used with link_to
Send emails using Docker container on Raspberry Pi 3
Update container image with KUSANAGI Runs on Docker
Docker on Ubuntu18.04 on WSL2 and VSCode installation instructions
I tried running Ansible on a Docker container
[Solution] Java cannot be installed on Windows 10 + ATOK 2017
Why Java String typeclass comparison (==) cannot be used