我有以下应该启动nginx服务器的Dockerfile:
# Set the base image to Ubuntu
FROM ubuntu
# File Author / Maintainer
MAINTAINER myname "myemail@gemail.com"
# Install Nginx
# Add application repository URL to the default sources
# RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
# Update the repository
RUN apt-get update
# Install necessary tools
RUN apt-get install -y nano wget dialog net-tools
# Download and Install Nginx
RUN apt-get install -y nginx
# Remove the default Nginx configuration file
RUN rm -v /etc/nginx/nginx.conf
# Copy a configuration file from the current directory
ADD nginx.conf /etc/nginx/
# Append "daemon off;" to the configuration file
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# Expose ports
EXPOSE 80
# Set the default command to execute when creating a new container
CMD service nginx start
当我尝试启动使用docker build创建的图像时,出现以下错误:
my-MacBook-Pro:nginx-docker me$ docker run -it myname:nginx-latest
2016-07-15T12:57:00.525640076+02:00 container create 0220107a0060cf61ff7dcb601870086b4ae7a0b7cacd914d63719e9a3d0d9451 (image=myname:nginx-latest, name=sad_knuth)
2016-07-15T12:57:00.542025710+02:00 container attach 0220107a0060cf61ff7dcb601870086b4ae7a0b7cacd914d63719e9a3d0d9451 (image=myname:nginx-latest, name=sad_knuth)
2016-07-15T12:57:00.573835719+02:00 network connect 5e0b48e45380f1548ab524cfa7bfe94ae952ce740df76fda9e35561a258e31ef (container=0220107a0060cf61ff7dcb601870086b4ae7a0b7cacd914d63719e9a3d0d9451, name=bridge, type=bridge)
2016-07-15T12:57:00.654772110+02:00 container start 0220107a0060cf61ff7dcb601870086b4ae7a0b7cacd914d63719e9a3d0d9451 (image=myname:nginx-latest, name=sad_knuth)
2016-07-15T12:57:00.656973139+02:00 container resize 0220107a0060cf61ff7dcb601870086b4ae7a0b7cacd914d63719e9a3d0d9451 (height=42, image=myname:nginx-latest, name=sad_knuth, width=181)
* Starting nginx nginx [fail]
2016-07-15T12:57:00.715136152+02:00 container die 0220107a0060cf61ff7dcb601870086b4ae7a0b7cacd914d63719e9a3d0d9451 (exitCode=1, image=myname:nginx-latest, name=sad_knuth)
2016-07-15T12:57:00.757891577+02:00 network disconnect 5e0b48e45380f1548ab524cfa7bfe94ae952ce740df76fda9e35561a258e31ef (container=0220107a0060cf61ff7dcb601870086b4ae7a0b7cacd914d63719e9a3d0d9451, name=bridge, type=bridge)
my-MacBook-Pro:nginx-docker me$
编辑:
我甚至根据建议尝试了以下内容:
myname-MacBook-Pro:nginx-docker me$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0220107a0060 myname:nginx-latest "/bin/sh -c 'service " 8 minutes ago Exited (1) 8 minutes ago sad_knuth
7252213512f0 myname:nginx-latest "/bin/sh -c 'service " 23 minutes ago Exited (1) 23 minutes ago clever_jennings
53a5e6978978 myname:nginx-latest "/bin/sh -c 'service " 25 minutes ago Exited (1) 25 minutes ago gigantic_dubinsky
10fc9e4b749f myname:nginx-latest "/bin/sh -c 'service " 27 minutes ago Exited (1) 27 minutes ago distracted_almeida
64161feb2b2a 0d73419e8da9 "nginx -g 'daemon off" 3 hours ago Exited (1) 3 hours ago pensive_mcnulty
5c9eab4c7998 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago sharp_kare
myname-MacBook-Pro:nginx-docker me$ docker container 0220107a0060
docker: 'container' is not a docker command.
See 'docker --help'.
myname-MacBook-Pro:nginx-docker me$ docker logs 0220107a0060
* Starting nginx nginx [fail]
答案 0 :(得分:0)
CMD服务nginx启动意味着主要进程在启动nginx后立即退出。
你需要
CMD /usr/sbin/nginx -g "daemon off;"
确保它未以守护程序模式启动 Other alternatives are here,但我更喜欢这个,因为它将进程数限制为nginx。