Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

docker - Dockerized nginx is not starting

I have tried following some tutorials and documentation on dockerizing my web server, but I am having trouble getting the service to run via the docker run command.

This is my Dockerfile:

FROM ubuntu:trusty

#Update and install stuff
RUN apt-get update
RUN apt-get install -y python-software-properties aptitude screen htop nano nmap nginx

#Add files
ADD src/main/resources/ /usr/share/nginx/html

EXPOSE 80
CMD service nginx start

I create my image:

docker build -t myImage .

And when I run it:

docker run -p 81:80 myImage

it seems to just stop:

docker ps -a

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
90e54a254efa        pms-gui:latest      /bin/sh -c service n   3 seconds ago       Exit 0                                  prickly_bohr

I would expect this to be running with port 81->80 but it is not. Running

docker start 90e

does not seem to do anything.

I also tried entering it directly

docker run -t -i -p 81:80 myImage /bin/bash

and from here I can start the service

service nginx start

and from another tab I can see it is working as intended (also in my browser):

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
408237a5e10b        myImage:latest      /bin/bash           12 seconds ago      Up 11 seconds       0.0.0.0:81->80/tcp   mad_turing 

So I assume it is something I am doing wrong with my Dockerfile? Could anyone help me out with this, I am quite new to Docker. Thank you!

SOLUTION: Based on the answer from Ivant I found another way to start nginx in the foreground. My Dockerfile CMD now looks like:

CMD /usr/sbin/nginx -g "daemon off;"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As of now, the official nginx image uses this to run nginx (see the Dockerfile):

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

In my case, this was enough to get it to start properly. There are tutorials online suggesting more awkward ways of accomplishing this but the above seems quite clean.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...