Derrick
是一个可以帮助开发者快速完成应用容器化的工具。Derrick
主要关注的是希望容器化的开发者如何在本地进行开发的问题。开发者可以通过使用 Derrick 加速本地应用容器化迁移的过程。
依赖:
Derrick 安装:
# 安装 derrick
$ pip install python-derrick
# 报 fatal error: Python.h: No such file or directory 错误,请安装下面包(CentOS 例子)
$ yum install -y python-devel
# 下载 derrick-nodejs-demo node 项目
$ git clone https://github.com/ringtail/derrick-nodejs-demo
# 选择需要部署的平台,支持 Kubernetes 和 Swarm
# 因机器上没有安装 Kubernetes,这里选择 Swarm
$ derrick config
? Which Orchestration Engine would you like to choose? Swarm
# 运行 Derrick
$ derrick init
? Please input image name with tag (such as "registry.com/user/repo:tag"): yangpeng2468/test:v3
Derrick detect your platform is NodeJs and compile successfully.
# 查看生成的配置
$ git status
# 位于分支 master
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# .dockerignore
# Dockerfile
# Jenkinsfile
# derrick_conf
# docker-compose.yml
# kubernetes-deployment.yaml
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
# 本地验证容器化结果
$ derrick up
Swarm is the default Orchestration Engine.
Creating network "derrick-nodejs-demo_default" with the default driver
Building web
Step 1/14 : FROM node:8-alpine AS base
---> 26881633664e
Step 2/14 : WORKDIR /app
---> Running in 2d1bf9d12f4f
Removing intermediate container 2d1bf9d12f4f
---> b123be5925b6
Step 3/14 : COPY package.json .
---> ba2c32fa8283
Step 4/14 : RUN npm set progress=false && npm config set depth 0
---> Running in 3f20024fd462
Removing intermediate container 3f20024fd462
---> f100fe6b5639
Step 5/14 : RUN npm install --only=production --registry=https://registry.npm.taobao.org
---> Running in 44660d8e2f98
npm WARN deprecated json3@3.3.2: Please use the native JSON object instead of JSON 3
> ejs@2.7.4 postinstall /app/node_modules/ejs
> node ./postinstall.js
Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)
npm notice created a lockfile as package-lock.json. You should commit this file.
added 59 packages from 43 contributors in 2.481s
Removing intermediate container 44660d8e2f98
---> 950daf993e48
Step 6/14 : RUN cp -R node_modules prod_node_modules
---> Running in c306e49790f3
Removing intermediate container c306e49790f3
---> 822c75e9a625
Step 7/14 : RUN npm install --registry=https://registry.npm.taobao.org
---> Running in d0c4ee623cd0
added 34 packages from 291 contributors in 4.053s
Removing intermediate container d0c4ee623cd0
---> 2fdc9f38bb35
Step 8/14 : FROM base As test
---> 2fdc9f38bb35
Step 9/14 : COPY . /app
---> aa81f12f20c1
Step 10/14 : RUN npm test
---> Running in 5202ce0a6f9d
> derrick-nodejs-generator@0.0.0 test /app
> node_modules/mocha/bin/mocha
Array
#indexOf()
✓ should return -1 when the value is not present
1 passing (6ms)
Removing intermediate container 5202ce0a6f9d
---> 9ea003d029a4
Step 11/14 : FROM base AS release
---> 2fdc9f38bb35
Step 12/14 : COPY --from=base /app/prod_node_modules /app/node_modules
---> 74e76976eb8f
Step 13/14 : COPY . /app
---> 9fd22d80079a
Step 14/14 : CMD ["npm","start"]
---> Running in 18a736e9b63f
Removing intermediate container 18a736e9b63f
---> 1e9cda2d6697
Successfully built 1e9cda2d6697
Successfully tagged yangpeng2468/test:v3
Creating derrick-nodejs-demo_web_1 ... done
Your application has been up to running! You can run `docker ps` to get exposed ports.
# 查看运行容器
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------
derrick-nodejs-demo_web_1 docker-entrypoint.sh npm start Up 0.0.0.0:32769->3000/tcp
浏览器访问 172.17.0.6:32769
,展示如下页面
# 查看 k8s deployment 配置
$ cat kubernetes-deployment.yaml
---
apiVersion: v1
kind: Service
metadata:
annotations:
derrick.service.type: nodeport
derrick.version: 0.0.24
labels:
derrick.service: derrick-nodejs-demo
name: derrick-nodejs-demo
spec:
ports:
- name: "3000"
port: 3000
targetPort: 3000
selector:
derrick.service: derrick-nodejs-demo
type: NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
derrick.version: 0.0.24
labels:
derrick.service: derrick-nodejs-demo
name: derrick-nodejs-demo
spec:
replicas: 1
template:
metadata:
labels:
derrick.service: derrick-nodejs-demo
spec:
containers:
- image: yangpeng2468/test:v3
name: web
ports:
- containerPort: 3000
# 查看 jenkinsfile配置
$ cat Jenkinsfile
pipeline {
environment {
REGISTRY_ENDPOINT = "https://yangpeng2468/v2/"
IMAGE_WITH_TAG = "yangpeng2468/test:v3"
REGISTRY_CERTS = "registry"
}
agent {
node {
label 'nodejs'
}
}
stages {
stage('Build') {
steps {
sh 'npm install --registry=https://registry.npm.taobao.org'
}
}
stage('Test') {
steps {
sh 'npm test '
}
}
stage('Code Quality') {
steps {
script {
try{
checkstyle canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: ''
}catch(e){
echo e
}
}
}
}
stage('Image Build&Publish') {
steps {
echo 'Build Images'
script {
docker.withRegistry("${REGISTRY_ENDPOINT}", "${REGISTRY_CERTS}") {
sh 'docker build -t ${IMAGE_WITH_TAG} .'
sh 'docker push ${IMAGE_WITH_TAG}'
}
}
}
}
}
triggers {
pollSCM('* * * * *')
}
}