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/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/*; \
rm -f /lib/systemd/system/*udev*; \
rm -f /lib/systemd/system/*initctl*; \
rm -f /lib/systemd/system/*;\
rm -f /lib/systemd/system/*;
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
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>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 ( ) at 2019-09-11 14:50 CEST
Nmap scan report for localhost (
Host is up (0.00020s latency).
Other addresses for localhost (not scanned):
All 1000 scanned ports on localhost ( 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 .
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:

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


FROM local/c7-systemd
RUN yum -y install nginx; yum clean all; systemctl enable nginx
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.


$ 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

<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
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.