Docker Community Forums

Share and learn in the Docker community.

How to start nginx on docker container through the Dockerfile and without systemctl

I am running a docker image exactly as Centos / Docker Hub e.g.:

FROM centos:7
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]

Then I simply install nginx just as in the documentation e.g.:

FROM local/c7-systemd
RUN yum -y install nginx; yum clean all; systemctl enable nginx
EXPOSE 80
CMD ["/usr/sbin/init"]

The container is build and it is running e.g.:

<CONTAINER ID> proxy-nginx:1.0.0 "/usr/sbin/init" 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp

I ssh to the container to start the proxy but I am getting this error:

$ sudo docker exec -it <CONTAINER ID> /bin/bash
[root@<CONTAINER ID> /]# systemctl start nginx
Failed to get D-Bus connection: Operation not permitted

I even install nmap to scan the ports just as described here (5.8. Verifying Which Ports Are Listening).

But the response is that no ports are open:

# nmap -sT -O localhost

Starting Nmap 6.40 ( http://nmap.org ) at 2019-09-11 14:50 CEST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00020s latency).
Other addresses for localhost (not scanned): 127.0.0.1
All 1000 scanned ports on localhost (127.0.0.1) are closed
Too many fingerprints match this host to give specific OS details
Network Distance: 0 hops

OS detection performed. Please report any incorrect results at http://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 1.91 seconds

I then check the configurations (default) of the nginx and it looks okay:

# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

The only way that I found on how to launch the proxy is to ssh to the container after and run:

# /usr/sbin/nginx

How can I automate this process without the need to ssh to the container?

I tried to include:

CMD ["/usr/sbin/nginx"]

But it is not working.

Has anyone found a solution?

Finally I found the solution from here (Running NGINX Plus in a Docker Container)

Simply include in Dockerfile:

STOPSIGNAL SIGTERM
CMD ["nginx", "-g", "daemon off;"]

Sample:

FROM local/c7-systemd
RUN yum -y install nginx; yum clean all; systemctl enable nginx
EXPOSE 80
STOPSIGNAL SIGTERM
CMD ["nginx", "-g", "daemon off;"]

After that the container starts immediately and you can telnet to the container on port 80 or test with curl.

Sample:

$ telnet <IP> 80
Trying <IP>...
Connected to <IP>.
Escape character is '^]'.
^]
HTTP/1.1 400 Bad Request
Server: nginx/1.12.2
Date: Wed, 11 Sep 2019 14:11:50 GMT
Content-Type: text/html
Content-Length: 173
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
Connection closed by foreign host.

Hope that someone in the future can benefit from this post and not spend so much time for no reason.