1

Host OS: macOS Mojave

I'm building a simple Docker container for Nginx. nginx will start correctly in interactive mode, but fails silently when invoked by a RUN command in a Dockerfile.

Dockerfile:

FROM ubuntu:18.04

RUN apt-get update && \ apt-get -y install nginx net-tools

WORKDIR /etc/nginx

COPY nginx.conf ./nginx.conf

RUN service nginx start

CMD /bin/bash

Kickoff script with replication of issue:

$ docker build -t blog_nginx . && docker run -it blog_nginx

Sending build context to Docker daemon  62.46kB

Step 1/6 : FROM ubuntu:18.04
 ---> 94e814e2efa8

Step 2/6 : RUN apt-get update &&    apt-get -y install nginx net-tools
 ---> Using cache
 ---> 18300d884828

Step 3/6 : WORKDIR /etc/nginx
 ---> Using cache
 ---> 22da684cd328

Step 4/6 : COPY nginx.conf ./nginx.conf
 ---> Using cache
 ---> 76624c016191

Step 5/6 : RUN service nginx start
 ---> Using cache
 ---> 0987fab7a011

Step 6/6 : CMD /bin/bash
 ---> Using cache
 ---> 7bdce4aa6802

Successfully built 7bdce4aa6802

Successfully tagged blog_nginx:latest

root@846f4e47e369:/etc/nginx# ps -ef

UID        PID  PPID  C STIME TTY          TIME CMD

root         1     0  0 01:15 pts/0    00:00:00 /bin/sh -c /bin/bash

root         6     1  0 01:15 pts/0    00:00:00 /bin/bash

root        11     6  0 01:15 pts/0    00:00:00 ps -ef

root@846f4e47e369:/etc/nginx# service nginx start

 * Starting nginx nginx                                                                                       

root@846f4e47e369:/etc/nginx# ps -ef

UID        PID  PPID  C STIME TTY          TIME CMD

root         1     0  0 01:15 pts/0    00:00:00 /bin/sh -c /bin/bash

root         6     1  0 01:15 pts/0    00:00:00 /bin/bash

root        31     1  0 01:15 ?        00:00:00 nginx: master process /usr/sbin/nginx

nobody      32    31  0 01:15 ?        00:00:00 nginx: worker process

root        34     6  0 01:16 pts/0    00:00:00 ps -ef
0

Everything inside one RUN command is isolated and executed inside a temporary container.

During docker build each Dockerfile instruction is executed in a temporary container. Once that the instruction is completed, the temporary container is stopped and everything running inside it killed.

That's why you won't have an nginx instance running in the next RUN instruction. You will accomplish that by running both instructions in one command:

RUN service nginx start && ps -ef

Once docker build is complete, there is nothing left running inside the image. A new process will start with docker run ... and you need to decide how it starts by defining an ENTRYPOINT or CMD.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.