Use Jenkins to build inside Docker and then create a Docker image.

Introduction

I was building inside a Maven container using Jenkins. Anyway, I wanted to create a Docker image all at once. It took longer than I expected.

Repository configuration

src
Dockerfile
Jenkinsfile
pom.xml

I have the source and the above files in my git repository.

Dockerfile

FROM tomcat:jdk14-openjdk

COPY target/*.war /usr/local/tomcat/webapps/ 

The war file created by the build is stored under webapps. Manage in the same repository as your code.

Jenkinsfile

pipeline {
    agent any
    stages {
        stage('delete_workspace') {
            steps {
                 deleteDir()
            }
        }
        stage('build') {
            agent {
                docker {
                    label 'master'
                    image 'maven:3.6.3-openjdk-14'
                }
            }
            steps {
                // Run Maven on a Unix agent.
                sh "mvn clean package"
            }
            post {
                success { 
                archiveArtifacts 'target/*.war'          
                }
            }
        }
        stage('docker build') {
            agent { label 'master'}
            steps {
                sh 'docker build -t test-tomcat:0.1 .'
            }
            ////When deleting after the fact.
            //     post {
            //     always {
            //         deleteDir()
            //     }
            // }
        }
    }
}

Created with Declarative Pipeline.

stage

Agent specification

pipeline {
    agent any
    stages {

If there is no agent any, the job failed, so it is described.

delete_workspace

stage('delete_workspace') {
    steps {
         deleteDir()
    }
}

There is a pattern to delete the workspace in advance and always do it after the fact, but when an error occurs in the job, it is difficult to isolate personally if it is deleted, so it is a group to do it in advance of the job.

build

agent {
    docker {
        label 'master'
        image 'maven:3.6.3-openjdk-14'
    }
}

The maven container is running on the master node. It took me a while to realize that the node designation was in this position.

steps {
         // Run Maven on a Unix agent.
         sh "mvn clean package"
    }
    post {
        success { 
        archiveArtifacts 'target/*.war'
                             
        }
    }

I'm building maven and saving the artifacts.

docker build

    stage('docker build') {
        agent { label 'master'}
        steps {
            sh 'docker build -t test-tomcat:0.1 .'
        }
        ////When deleting after the fact.
        //     post {
        //     always {
        //         deleteDir()
        //     }
        // }
    }

Since docker was started on the master, specify agent as the master. Since the artifacts built with the maven container and the Dockerfile are stored under the workspace of the host, Run docker build as it is.

Summary

I didn't know that the build result would remain in the host workspace even if I built without the container, so It took a long time to create. I also found that I had to write in Scripted Pipeline to do various things with docker in jenkins. As far as jenkins is concerned, it's simpler to call Ansible and docker-compose commands than to work hard with pipeline. I felt that maintainability was also high. Which one is it actually?

References

https://www.jenkins.io/doc/book/pipeline/docker/

Recommended Posts

Use Jenkins to build inside Docker and then create a Docker image.
[Introduction to Docker] Create a Docker image for machine learning and use Jupyter notebook
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
Build a Node-RED environment with Docker to move and understand
Create a lightweight STNS Docker image
[Splunk] Build Splunk Enterprise (use docker image)
Create a flyway jar with maven and docker build (migrate) with docker-maven-plugin
Create a Docker Image for redoc-cli and register it on Docker Hub
Create Docker to use Edge TPU compiler
Docker Compact Manual (4: Create a custom image)
A shell script that builds a Docker image and pushes it to ECR
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
How to create a small docker image of openjdk 11 (ea) application (1GB → 85MB)
Test, build, and push your Docker image to GitHub Container Registry using GitHub Actions
Build a development environment to create Ruby on Jets + React apps with Docker
How to deploy to Heroku from a local docker image
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to create a header or footer once and use it on another page
Create a Docker container to convert EPS to PGF source
Display a loading image in JavaFX and then display another image
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
How to build a Jenkins server with a Docker container on CentOS 7 of VirtualBox and access the Jenkins server from a local PC
Try to build a Java development environment using Docker
If you want to make a Java application a Docker image, it is convenient to use jib.
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
Maybe it works! Create an image with Docker and share it!
How to create a lightweight container image for Java apps
[Docker] Build a site on Hugo and publish it on GitHub
Create a Docker image with the Oracle JDK installed (yum
Build Docker Image lightweight and fast with CodeBuild with Santa Banner
[Personal notes] How to push a Docker image to GitHub Packages
I tried to create a padrino development environment with Docker
Study Java: Use Timer to create something like a bomb timer
How to create and launch a Dockerfile for Payara Micro
[Enum_help] Use enum_help to create a select box displayed in Japanese!
How to create a method
Create a private repository in Amazon ECR and push/pull the image
Use Docker and Keycloak to specify the access token and execute the API
Make a C compiler to use with Rust x CLion with Docker
Build a container version of Minecraft server and use https-enabled Dynmap
I tried to build a laravel operating environment while remembering Docker
[For those who create portfolios] How to use binding.pry with Docker
How to use StringBurrer and Arrays.toString.
Create a Vue3 environment with Docker!
Build a Node.js environment with Docker
Preparing to create a Rails application
Migrate Docker image to another server
How to use equality and equality (how to use equals)
[Java] How to create a folder
How to make a Jenkins plugin
[Artifactory] How to use Docker repository
Try to create a server-client app
Challenge to install WSL2 and docker
How to build CloudStack using Docker
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!
Make a daily build of the TOPPERS kernel with Gitlab and Docker
How to quickly create a reverse proxy that supports HTTPS with Docker
Wait for PostgreSQL to start with Docker and then start the WEB service
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Enable Docker build cache on GitHub Action and deploy to Amazon ECS
I tried to build a Firebase application development environment with Docker in 2020