这是一篇 docker
实践教程,将 tangxinfa/upload-fiddle 这个 node.js
服务封装成 docker
容器。相关代码已提交到 tangxinfa/upload-fiddle 项目。
编写 Dockerfile
1: FROM dl.dockerpool.com:5000/ubuntu:14.04 2: 3: MAINTAINER tangxinfa <tangxinfa@gmail.com> 4: 5: # Change apt sources 6: ADD .docker/sources.list /etc/apt/sources.list 7: 8: # Install python 9: RUN \ 10: apt-get update && apt-get -y -qq install python wget xz-utils 11: 12: # Install node 13: RUN \ 14: wget http://npm.taobao.org/mirrors/node/v8.8.1/node-v8.8.1-linux-x64.tar.xz &&\ 15: tar xJvf node-v8.8.1-linux-x64.tar.xz --no-same-owner --exclude CHANGELOG.md --exclude LICENSE --exclude README.md --strip-components 1 -C /usr 16: 17: WORKDIR /usr/local/upload-fiddle/ 18: 19: # Install node modules 20: ADD package.json ./ 21: ADD process.json ./ 22: ADD .bashrc ./ 23: RUN \ 24: ["/bin/bash", "-c", "npm config set registry=https://registry.npm.taobao.org; npm config set script-shell=/bin/bash"] 25: RUN \ 26: ["/bin/bash", "-c", "source .bashrc; npm install -g pm2@2.x json; npm install"] 27: 28: # Add project files 29: COPY src ./src 30: COPY config ./config 31: ADD public/*.html ./public/ 32: RUN mkdir /usr/local/upload-fiddle/public/files &&\ 33: chmod a+w /usr/local/upload-fiddle/public/files 34: 35: # Start service. 36: CMD ["/bin/bash", "-c", "npm run pm2 -- ping && npm start && npm run logs"]
创建 docker
镜像
sudo docker build -t 'tangxinfa/upload-fiddle:0.0.1' .
运行 docker
容器
sudo docker run -idt --name='upload-fiddle' -p 127.0.0.1:80:80 'tangxinfa/upload-fiddle:0.0.1'
现在可以通过浏览器访问服务了:http://localhost/
查看容器运行日志
sudo docker logs upload-fiddle -f
打开 docker
容器终端查看进程运行状态
sudo docker exec -ti upload-fiddle /bin/bash npm run pm2 -- list
直接 exit
退出终端不会停止容器。