本文共 5371 字,大约阅读时间需要 17 分钟。
// Load the http module to create an http server.var http = require('http');// Configure our HTTP server to respond with Hello World to all requests.var server = http.createServer(function (request, response) {response.writeHead(200, {"Content-Type": "text/plain"});response.end("Hello World\n");});// Listen on port 8000, IP defaults to "0.0.0.0"server.listen(8000);// Put a friendly message on the terminalconsole.log("Server running at http://127.0.0.1:8000/");
{"name": "hello-world","description": "hello world","version": "0.0.1","private": true,"dependencies": {"express": "3.x"},"scripts": {"start": "node app.js"}}
Dockerfile
: FROM google/nodejsWORKDIR /appADD package.json /app/RUN npm installADD . /appEXPOSE 8000CMD []ENTRYPOINT ["/nodejs/bin/npm", "start"]
$ docker build -t my_nodejs_image .$ docker run -p 8000:8000 my_nodejs_image
http://127.0.0.1:8000/
,看看是否可以看到“Hello World”! fig.yml
吧。Fig是一个编排工具,我们使用一个命令就可以将所有中心化的服务部署好。 只是个开发工具,如果想要在生产环境使用,需要用更复杂的自动化工具替代它。 fig.yml
: registry:image: registryenvironment:- STORAGE_PATH=/registryvolumes:- registry-stuff:/registryports: - "5000:5000"
$ fig up
http://localhost:5000
。 # Build an image$ docker build -t localhost:5000/containersol/nodejs_app# Push it to the registry$ docker push localhost:5000/containersol/nodejs_app
Forbidden. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add –insecure-registry 10.0.0.26:5000 to the daemon’s arguments.
/etc/default/docker
,然后重启Docker Daemon: DOCKER_OPTS="--insecure-registry localhost:5000"
fig.yml
文件里,重启系统: jenkins:image: containersol/jenkins_with_dockervolumes:- jenkins-stuff:/var/jenkins_home- .:/var/jenkins_data- /var/run/docker.sock:/var/run/docker.sock- /usr/bin/docker:/usr/bin/dockerports: - "8081:8080"registry:image: registryenvironment:- STORAGE_PATH=/registryvolumes:- registry-stuff:/registryports: - "5000:5000"
FROM jenkinsMAINTAINER ContainerSolutionsUSER root#TODO the group ID for docker group on my Ubuntu is 125, therefore I can only run docker commands if I have same group id inside. # Otherwise the socket file is not accessible.RUN groupadd -g 125 docker && usermod -a -G docker jenkins USER jenkins
$ docker build -t containersol/jenkins_with_docker .
build.sh
包含额外的镜像版本信息。每个Jenkins build会为Docker镜像创建新的标签。需要注意,我并不是说这是版本化的正确方式。这个领域很复杂,可能会在以后专门写文章讨论,但是现在就跳过吧。 #!/bin/bashif [ -z "${1}" ]; thenversion="latest"elseversion="${1}"ficd nodejs_appdocker build -t localhost:5000/containersol/nodejs_app:${version} .cd ..
push.sh
会push之前步骤构建出的镜像。使用和之前脚本一样的版本。 #!/bin/bashif [ -z "${1}" ]; thenversion="latest"elseversion="${1}"fidocker push localhost:5000/containersol/nodejs_app:"${version}"
build.sh
和 push.sh
。Jenkins在URL http://localhost:8081
上运行。 ./build.sh ${BUILD_ID}./push.sh ${BUILD_ID}
转载地址:http://pyuix.baihongyu.com/