简体   繁体   English

如何使用 Jenkinsfile 将 Docker 映像部署到某个 Docker 容器?

[英]How do i deploy a Docker image to a certain Docker container with a Jenkinsfile?

I have a Jenkinsfile which build and deploys an Angular Image to Docker and stores it there - I use Portainer as an UI here.我有一个 Jenkinsfile,它构建并将 Angular 映像部署到 Docker 并将其存储在那里 - 我在这里使用 Portainer 作为 UI。 Now i want to add a deployment stage to my Jenkinsfile which deploys the Image to a certain container.现在我想向我的 Jenkinsfile 添加一个部署阶段,它将图像部署到某个容器。

I tried serveral solutions from the internet but no of them worked for me.我尝试了互联网上的几种解决方案,但没有一个对我有用。

Here is my Jenkinsfile:这是我的詹金斯文件:

    pipeline {

agent any
tools { 
    nodejs 'NodeJS 16.9.1'  
    dockerTool 'docker-19.03.9'  
}

environment {
    IMAGE = 'name-angular'
    VERSION = 'test'
}
stages {
    stage('Build image') {
        steps {
            script {
                sh 'npm install'
                sh 'npm run buildProd'
                IMAGE = readMavenPom().getArtifactId()
                VERSION = readMavenPom().getVersion()
                echo "Building image ${IMAGE}:${VERSION}..."
                dockerImage = docker.build("name:latest")
            }
        }
    }
    
    stage('Deploy image') {
        steps {
            echo 'Deploying image...'
          
        }
    }
    
    stage('Clean up') {
        steps {
            echo 'Cleaning up...'
        }
    }
}

} }

As you see my deployment stage is completely empty because i even dont know how to start with this.如您所见,我的部署阶段完全是空的,因为我什至不知道如何开始。

I think the dockerImage variable will not be transfer to the other stage, so you can call docker command directly.我认为dockerImage变量不会转移到其他阶段,所以你可以直接调用docker命令。 The below pipeline is an example, so you should fit this to your purpose.以下管道是一个示例,因此您应该根据自己的目的进行调整。

pipeline {

    agent any

    tools { 
        nodejs 'NodeJS 16.9.1'  
        dockerTool 'docker-19.03.9'  
    }

    environment {
        IMAGE = readMavenPom().getArtifactId()
        VERSION = readMavenPom().getVersion()
    }

    stages {
        stage('Build image') {
            steps {
                sh 'npm install'
                sh 'npm run buildProd'

                echo "Building image ${IMAGE}:${VERSION}..."
                sh 'docker build -t ${IMAGE}:${VERSION} .'
            }
        }
  
        stage('Deploy image') {
            steps {
                echo 'Deploying image...'
                sh 'docker push ${IMAGE}:${VERSION}'
            }
        }
    
    post {
        always {
            sh 'docker rmi ${IMAGE}:${VERSION}'
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Jenkinsfile 中运行图像时如何传递 docker 容器参数 - How to pass docker container arguments when running the image in a Jenkinsfile 如何在 Azure 上部署 keycloak docker 容器? - How do I deploy keycloak docker container on Azure? 如何将 blazor docker 容器应用程序部署到多个环境? - How do i deploy a blazor docker container application to multiple environments? 如何从Docker注册表中提取容器映像以部署Azure容器 - How to pull container image from docker registry to deploy azure container 如何指定在 Jenkinsfile 中安装了 docker 的节点类型? - How do I specify a node type that has docker installed in Jenkinsfile? 在 Jenkinsfile 中标记 docker 图像 - tagging docker image in Jenkinsfile 如何将 docker 容器的映像移动到永久性磁盘? - How do I move a docker container's image to a persistent disk? 如何重新启动Docker容器/映像/机器? - How do I restart a Docker container/image/machine? 如何保存对 docker 容器和映像的更改 - How do I save changes to a docker container and image 我想在正在运行的 docker 容器中使用 docker sdk,并想在正在运行的 docker 容器中构建一个 docker 镜像并将其部署在 minikube 中 - I want to use docker sdk inside a running docker container and want to build an docker image inside running docker container and deploy it in minikube
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM