docker-run nginx并非总是在工作

时间:2017-05-24 06:58:51

标签: nginx docker

出于学习目的,我创建了这个Dockerfile:

FROM ubuntu:16.10

RUN useradd -m --password=node node && \
  apt-get update && \
  apt-get upgrade -y && \
  apt-get install -y curl && \
  apt-get install -y openssl && \
  apt-get install -y nginx && \
  apt-get -qy autoremove

COPY . /home/node/xxx.com

RUN rm /etc/nginx/sites-enabled/default && \
  cd /etc/nginx/sites-enabled && \
  ln -s /home/node/xxx.com/xxx.nginx.conf default

WORKDIR /home/node

RUN curl -sL https://deb.nodesource.com/setup_7.x | bash - && \
  apt-get install -y nodejs 

RUN curl -L https://get.rvm.io | bash -s stable && \
  /bin/bash -l -c "rvm requirements" && \
  /bin/bash -l -c "rvm install 2.0" && \
  /bin/bash -l -c "gem update --system" && \
  /bin/bash -l -c "gem install bundler --no-ri --no-rdoc" && \
  /bin/bash -l -c "gem install compass  --no-ri --no-rdoc" && \
  /bin/bash -l -c "gem install bootstrap -v 4.0.0.alpha6 --no-ri --no-rdoc"

RUN chown -R node:node /home/node/xxx.com

USER node

WORKDIR /home/node/xxx.com

ENTRYPOINT ["/bin/bash", "-l", "-c", "./entry.sh"]

entry.sh

service nginx start
npm install
npm run watch &
compass watch

在我的笔记本电脑(Ubuntu 16.04)上启动。运行容器(使用-p 8000:80)一切都很好,但在我的Workstation(Ubuntu 14.10)上,nginx无法在容器内启动。如果我运行docker exec -it -u root <cid> /bin/bash然后service nginx start,一切都在运行。

如果不使用额外命令怎么办呢?为什么在我的笔记本电脑上而不是在我的工作站上工作?

顺便说一句:

工作站$ docker version

Client:
  Version:      17.03.1-ce
  API version:  1.27
  Go version:   go1.7.5
  Git commit:   c6d412e
  Built:        Mon Mar 27 17:10:36 2017
  OS/Arch:      linux/amd64

Server:
 Version:      17.03.1-ce
 API version:  1.27 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   c6d412e
 Built:        Mon Mar 27 17:10:36 2017
 OS/Arch:      linux/amd64
 Experimental: false

笔记本:

Client:
 Version:      1.12.6
 API version:  1.24
 Go version:   go1.6.2
 Git commit:   78d1802
 Built:        Tue Jan 31 23:35:14 2017
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.6
 API version:  1.24
 Go version:   go1.6.2
 Git commit:   78d1802
 Built:        Tue Jan 31 23:35:14 2017
 OS/Arch:      linux/amd64

1 个答案:

答案 0 :(得分:0)

也许你应该将你的nginx.sh文件复制到容器中并以shell脚本作为入口点启动容器?

  1. 将startskript添加到容器中
  2. 移动并设置为可执行文件
  3. 设置入口点以开始skript

    FROM ubuntu:16.10

    //add start script to temp
    ADD run-nginx.sh  /tmp/
    
    RUN useradd -m --password=node node && \
    apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y curl && \
    apt-get install -y openssl 
    
    && \
      apt-get install -y nginx && \
      apt-get -qy autoremove
    
    COPY . /home/node/xxx.com
    
    RUN rm /etc/nginx/sites-enabled/default && \
      cd /etc/nginx/sites-enabled && \
      ln -s /home/node/xxx.com/xxx.nginx.conf default
    
    WORKDIR /home/node
    
    RUN curl -sL https://deb.nodesource.com/setup_7.x | bash - && \
      apt-get install -y nodejs 
    
    RUN curl -L https://get.rvm.io | bash -s stable && \
      /bin/bash -l -c "rvm requirements" && \
      /bin/bash -l -c "rvm install 2.0" && \
      /bin/bash -l -c "gem update --system" && \
      /bin/bash -l -c "gem install bundler --no-ri --no-rdoc" && \
      /bin/bash -l -c "gem install compass  --no-ri --no-rdoc" && \
      /bin/bash -l -c "gem install bootstrap -v 4.0.0.alpha6 --no-ri --no-rdoc"
    
    //copy to home and set to executable
    RUN mv /tmp/run-nginx.sh / && chmod +x /run-nginx.sh
    
    RUN chown -R node:node /home/node/xxx.com
    
    USER node
    
    WORKDIR /home/node/xxx.com
    
    ENTRYPOINT ["/run-nginx.sh"]