본문 바로가기

리눅스

[리눅스] docker container centos에서 systemctl 명령을 실행하는 방법(centos init)

728x90

docker container centos에서 systemctl 명령을 실행하는 방법

호스트 운영체제가 centos일 경우

테스트 환경

$ cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

docker container 시작

docker run -d --privileged --cap-add=SYS_ADMIN --privileged --name centos7 centos:7 /sbin/init

docker container 목록 확인

docker ps -a
$ docker ps -a
CONTAINER ID   IMAGE      COMMAND        CREATED         STATUS         PORTS     NAMES
bd898ff016d1   centos:7   "/sbin/init"   6 minutes ago   Up 6 minutes             centos7

docker container centos7에 접속

docker exec -it centos7 bash
systemctl status
$ docker exec -it centos7 bash     
[root@bd898ff016d1 /]# systemctl status
● bd898ff016d1
    State: running
     Jobs: 0 queued
   Failed: 0 units
    Since: Wed 2023-01-18 00:06:17 UTC; 8min ago
   CGroup: /docker/bd898ff016d12d203e14db9f74539a32b895db122315c3ad7f80c6a072929c99
           ├─1 /sbin/init
           └─system.slice
             ├─systemd-logind.service
             │ └─47 /usr/lib/systemd/systemd-logind
             ├─dbus.service
             │ └─46 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
             ├─systemd-udevd.service
             │ └─27 /usr/lib/systemd/systemd-udevd
             ├─system-getty.slice
             │ └─getty@tty1.service
             │   └─66 /sbin/agetty --noclear tty1 linux
             └─systemd-journald.service
               └─17 /usr/lib/systemd/systemd-journald
[root@bd898ff016d1 /]#
728x90

호스트 운영체제가 ubuntu일 경우

테스트 환경

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.1 LTS
Release:        22.04
Codename:       jammy

** 호스트 운영체제가 우분투일 경우에 centos7 컨테이너 systemctl 명령을 사용할 수 없다.

docker container 시작

docker run -d --privileged --cap-add=SYS_ADMIN --privileged --name centos8 centos /sbin/init

docker container 목록 확인

docker ps -a
$ docker ps -a
CONTAINER ID   IMAGE     COMMAND        CREATED          STATUS          PORTS     NAMES
f2e657ea6557   centos    "/sbin/init"   12 seconds ago   Up 10 seconds             centos8

docker container centos8에 접속

docker exec -it centos8 bash
systemctl status
$ docker exec -it centos8 bash
[root@f2e657ea6557 /]# systemctl status
● f2e657ea6557
    State: degraded
     Jobs: 0 queued
   Failed: 1 units
    Since: Wed 2023-01-18 00:49:15 UTC; 39s ago
   CGroup: /
           ├─init.scope
           │ ├─ 1 /sbin/init
           │ ├─71 bash
           │ ├─85 systemctl status
           │ └─86 [less]
           └─system.slice
             ├─systemd-udevd.service
             │ └─35 /usr/lib/systemd/systemd-udevd
             ├─systemd-journald.service
             │ └─27 /usr/lib/systemd/systemd-journald
             └─dbus.service
               └─52 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
[root@f2e657ea6557 /]#
728x90