5

I would like to use this image https://hub.docker.com/_/amazonlinux to script my EC2 machine

Here is my Dockefile :

FROM amazonlinux:2

MAINTAINER "xxxxx" <xxxxx@xxxx.xx>

RUN yum -y update
RUN yum -y install httpd

WORKDIR /var/www/html

CMD [ "/usr/sbin/httpd","-D","FOREGROUND" ]

systemctl do not works :

bash-4.2# systemctl list-units
Failed to get D-Bus connection: Operation not permitted
bash-4.2# systemctl status httpd.service
Failed to get D-Bus connection: Operation not permitted

I cannot restart

bash-4.2# systemctl restart httpd.service
Failed to get D-Bus connection: Operation not permitted

/etc/os-release

bash-4.2# cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"

No problem on real ec2 :

[ec2-user@ip-xxxxxxxxx]$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"

[ec2-user@ip-xxxxxxxxx]$ systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           └─php-fpm.conf
   Active: active (running) since mer. 2020-05-20 16:36:19 UTC; 1h 50min ago
     Docs: man:httpd.service(8)
 Main PID: 3047 (httpd)
   Status: "Total requests: 7; Idle/Busy workers 100/0;Requests/sec: 0.00106; Bytes served/sec:  20 B/sec"
   CGroup: /system.slice/httpd.service
           ├─3047 /usr/sbin/httpd -DFOREGROUND
           ├─3075 /usr/sbin/httpd -DFOREGROUND
           ├─3076 /usr/sbin/httpd -DFOREGROUND
           ├─3077 /usr/sbin/httpd -DFOREGROUND
           ├─3078 /usr/sbin/httpd -DFOREGROUND
           ├─3079 /usr/sbin/httpd -DFOREGROUND
           └─3495 /usr/sbin/httpd -DFOREGROUND

mai 20 16:36:19 xxxxxxxxx.internal systemd[1]: Starting The Apache HTTP Server...
mai 20 16:36:19 xxxxxxxxx.internal systemd[1]: Started The Apache HTTP Server.

It's a famous image, has anyone found a solution ?

bingo
  • 150
  • 3
  • 10
  • 1
    Yes, as a general rule `systemctl` just doesn't work in Docker (and similarly `service` and directly running `/etc/init.d` scripts won't do what you expect). The container _is_ the HTTP daemon; you don't need to try to run systemd commands, get a shell inside it, or other similar tasks you'd do on a full Linux VM. – David Maze May 20 '20 at 20:28

1 Answers1

0

The "httpd" is run as PID-1 of the container.

If you do "docker stop (container)" then httpd will get a SIGTERM. If you do "docker restart (container)" then it will cycle through docker-stop / docker-start also recreating the httpd daemon.

https://docs.docker.com/engine/reference/commandline/restart/

Using systemd in a container is not needed here. If you really want to do that then there are multiple options. It could be to run a full systemd in a container or to use another service manager instead.

https://github.com/gdraheim/docker-systemctl-images/blob/master/centos-httpd.dockerfile

Guido U. Draheim
  • 3,038
  • 1
  • 20
  • 19