我有一个java项目,我正在生成.jar文件。我的问题是,使用docker和jenkins构建和部署项目的最佳实践是什么?它是用一个坞映像构建jar,然后将Jar文件放到另一个坞映像中,然后从生产env中的最后一个映像运行一个容器,还是有更好的方法来做到这一点?
发布于 2017-09-06 08:13:53
发布于 2019-01-29 19:01:24
因此,假设Docker和Jenkins被设置为如前所述,我会说您的方法包括:在一个容器中构建Jar,获取Jar并使用它构建一个映像,然后根据该映像为您的环境构建容器。
关于如何设置它的一些想法:
让一个码头主机(或蜂群)运行,并在上面启动Jenkins服务。这是我正在运行的一个撰写文件:
version: '3.7'
services:
jenkins-local:
user: root
image: jenkinsci/blueocean
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ../../..:/var/projects
- ./jenkins_home:/var/jenkins_home
ports:
- "9000:8080"
volumes:
jenkins-home:重要的部分是将主机的/var/run/docker.sock安装到容器的/var/run/docker.sock的卷。这允许Jenkins通过Docker套接字访问主机的Docker守护进程(确保"user“是"root")。在这里可以找到一些关于这方面的阅读:https://getintodevops.com/blog/the-simple-way-to-run-docker-in-docker-for-ci
我之所以选择jenkinsci/“蓝海”图像,是因为它允许使用蓝海管道。https://jenkins.io/projects/blueocean/
在管道中,您可以编写Jenkinsfiles来定义构建管道:
node {
stage('checkout scm') {
checkout scm
}
stage('Build artifact') {
// Connect to our Nexus Docker repository with the nexus_jenkins_user user credentials managed in Jenkins
docker.withRegistry('https://registry.swarm', 'nexus_jenkins_user') {
// Run a Maven builder image and attach it to the nexus network
// Add a volume to cache the maven-repo in for consecutive runs
docker.image('registry.swarm/builder/maven:maven-3.3.9').inside('-v maven-repo:/root/.m2 --network=nexus_nexus') {
sh 'mvn clean package -Dmaven.test.failure.ignore=true'
}
}
}
stage('archive artifacts and save test results') {
archiveArtifacts artifacts: "target/*.jar", fingerprint: true
junit 'target/surefire-reports/*.xml'
}
stage('Build run container image, push Docker image to nexus') {
docker.withRegistry('https://registry.swarm', 'nexus_jenkins_user') {
docker.build("app/my-service:current").push()
}
}
}由于所有事情都发生在Jenkins Workspace中,所以Dockerfile的唯一重要行是以下一行:
COPY ./target/my-service-*.jar ./my-service.jar
CMD [ "java", "-Duser.timezone=UTC", "-jar", "my-service.jar"]这将捕获jar,复制它并将其用于映像中的run命令。
https://devops.stackexchange.com/questions/1930
复制相似问题