최근 docker로 CentOS 7 테스트를 할 일이 생겼다. 그래서 아래와 같이 CentOS 7 이미지로 bash를 실행하고 systemctl을 입력했는데...

 

[user01@CentOS7 ~]$ docker run -it centos:7 bash
[root@9fa1bb19b89c /]# systemctl
Failed to get D-Bus connection: Operation not permitted

 

Failed to get D-Bus connection: Operation not permitted

 

위 메시지가 뜨면서 에러가 발생하였다.

여러가지 시도를 해본 후 나름 내린 결론은 --privileged 옵션과 -d 옵션으로 init을 실행한 후 다시 exec로 bash를 붙이는 것이다.

 

docker run --privileged -d --name mycent7 centos:7 init
docker exec -it mycent7 bash

 

[user01@CentOS7 ~]$ docker run --privileged -d --name mycent7 centos:7 init
4b0c9143719b5309e183d726ae26553d7b88ee2d7b00624abab359c94951d82a
[user01@CentOS7 ~]$ docker exec -it mycent7 bash
[root@4b0c9143719b /]# systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

 

두줄 입력하기 귀찮다면? bash의 command substitution 기능을 활용하면 된다.

 

docker exec -it $(docker run --privileged -d centos:7 init) bash

 

[user01@CentOS7 ~]$ docker exec -it $(docker run --privileged -d centos:7 init) bash
[root@64e6deb53411 /]# systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

 

아파치를 설치하고 systemctl 명령어로 구동 및 확인하였는데 잘 동작한다.

 

yum -y install httpd
systemctl start httpd
systemctl status httpd

 

[root@64e6deb53411 /]# systemctl start httpd
[root@64e6deb53411 /]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2017-09-07 14:31:59 UTC; 41s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 167 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /docker/64e6deb534119148ef42d04cab3502204617626e47a07d1ffef586dd717dbf2a/system.slice/httpd.service
           ├─167 /usr/sbin/httpd -DFOREGROUND
           ├─168 /usr/sbin/httpd -DFOREGROUND
           ├─169 /usr/sbin/httpd -DFOREGROUND
           ├─170 /usr/sbin/httpd -DFOREGROUND
           ├─171 /usr/sbin/httpd -DFOREGROUND
           └─172 /usr/sbin/httpd -DFOREGROUND
           ‣ 167 /usr/sbin/httpd -DFOREGROUND

Sep 07 14:31:59 64e6deb53411 systemd[1]: Starting The Apache HTTP Server...
Sep 07 14:31:59 64e6deb53411 httpd[167]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.6. Set the 'ServerName' directi...this message
Sep 07 14:31:59 64e6deb53411 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
신고