Dockerized nginx没有启动 [英] Dockerized nginx is not starting
问题描述
我已经尝试了关于dockerizing我的Web服务器的一些教程和文档,但是我无法通过docker运行命令来运行服务。
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.
这是我的Docker文件:
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
我创建我的图像:
docker build -t myImage .
当我运行它:
docker run -p 81:80 myImage
似乎只是停止:
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
我期望这是运行与端口81-> 80,但它不是。运行
I would expect this to be running with port 81->80 but it is not. Running
docker start 90e
似乎没有做任何事情。
我也尝试直接输入
docker run -t -i -p 81:80 myImage /bin/bash
从这里我可以开始服务
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
所以我认为我的Dockerfile有错误?有人可以帮助我吗,我对Docker来说很新。谢谢!
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!
解决方案:根据Ivant的答案,我发现在前台启动nginx的另一种方式。我的Dockerfile CMD现在看起来像:
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;"
推荐答案
只要您指定的命令与Docker容器一样运行 CMD
, ENTRTYPOINT
或通过命令行运行。在您的情况下,服务
命令立即完成,整个容器关闭。
Docker container runs as long as the command you specify with CMD
, ENTRTYPOINT
or through the command line is running. In your case the service
command finishes right away and the whole container is shut down.
解决此问题的一种方法是直接从命令行启动nginx(确保你不作为守护进程运行它)。
One way to fix this is to start nginx directly from the command line (make sure you don't run it as a daemon).
另一个选项是创建一个启动服务的小脚本然后永远睡觉。如下:
Another option is to create a small script which starts the service and then sleeps forever. Something like:
#!/bin/bash
service nginx start
while true; do sleep 1d; done
并运行它,而不是直接运行服务
命令。
and run this instead of directly running the service
command.
第三个选项是使用像 runit 或类似的程序,而不是正常的服务。
A third option would be to use something like runit or similar program, instead of the normal service.
这篇关于Dockerized nginx没有启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!