Experience CI / CD with katacoda (for beginners) --Part10 (Building Docker Images using Jenkins)

Introduction to CI / CD

In this page, we will practice CI / CD using "katacoda" (https://www.katacoda.com/), a "web service that allows you to launch a study instance for free from a browser". The content follows the link above, so if you have any questions, please go there.

Build docker container with jenkins --Building Docker Images using Jenkins

Here, we introduce an example of using jenkins, which is the core of CI / CD. Check ** Overview ** for a quick look at what you'll learn in this scenario ** If you have any mistakes in understanding, please point it out **

Overview </ font>

-To build a container with jenkins, follow the steps below

  1. Install a specific plugin
  2. Plugin settings
  3. jenkins agent settings
  4. Create a project to be executed by jenkins agent
  5. Project execution

Launch Jenkins First, execute the following command to execute jenkins as a docker container

docker run -d -u root --name jenkins \   //Background execution, root user, container name"jenkins"
    -p 8080:8080 -p 50000:50000 \   //Port 8080 is for web dashboard, port 50000 is for communication with jenkins agent
    -v /root/jenkins_2112:/var/jenkins_home \   //Mount path on the host side and path on the container side
    jenkins/jenkins:2.112-alpine   

This time there is another host terminal If you want to access, ssh connection

$ docker ps
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                                              NAMES
6048602eb5b9        jenkins/jenkins:2.112-alpine   "/sbin/tini -- /usr/…"   2 minutes ago       Up2 minutes        0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   jenkins

After confirming that the container has been successfully history, move from this link (default) To log in, `` admin```, `344827fbdbfb40d5aac067c7a07b9230``` The environment is like this

図1.png

Configure Docker Plugin Here, set the Docker plugin By setting the Docker plugin, jenkins can communicate with the docker daemon via agent (see the schematic diagram below).

図4.png

Click as follows on the dashboard Manage Jenkins> Manage Plugins> available tab Docker> Install without Restart

This plugin is treated as a cloud provider The container (jenkins agent) starts only when communicating with the docker host

Add Docker Agent Here, set the configuration to "start the container" in the agent prepared earlier.

Click as follows on the dashboard Enter `tcp: //172.17.0.67: 2345``` (docker server) in "Docker"> Docker Host URI from Manage Jenkins> Configure System> Add a new cloud Successful communication with Docker daemon with this procedure If you want to check, compare the result of Test Connection of Docker Host URI with the result of typing docker version `` on the console.

図3.png

$ docker version
Client:
 Version:      18.05.0-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   f150324
 Built:        Wed May  9 22:16:25 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.0-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.10
  Git commit:   0520e24302
  Built:        Fri Mar 23 01:48:12 2018
  OS/Arch:      linux/amd64
  Experimental: false

Next, set the agent container

  1. Docker Agent templates
  2. Add Docker Template
  3. Fill in the agent label `` `docker-agent```
  4. Fill in the container image benhall / dind-jenkins-agent: v2
  5. Volume mount settings / var / run / docker.sock: /var/run/docker.sock
  6. Specify the connection method with the agent container Connect with SSH
  7. save

図2.png

Create Build Project Here, set the container to be executed using the "jenkins-agent" mentioned earlier. The procedure is as follows

  1. Create a job with `` `Create new jobs```
  2. Select `` `Freestyle project``` and select "Katacoda Jenkins Demo" as the project name.
  3. When creating the container, select "dokcer-agent" (created in the previous step) that can make a request to the Docker daemon.
  4. Specify Git repository as the container image to be executed.
  5. Select `Execute Shell``` from `` Add Build Step```, describe the details of build as follows, and Save
ls
docker info
docker build -t katacoda/jenkins-demo:${BUILD_NUMBER} .
docker tag katacoda/jenkins-demo:${BUILD_NUMBER} katacoda/jenkins-demo:latest
docker images

図1.png

Build Project Here, actually execute the container build by jenkins from the dashboard build nowClick

The jenkins-agent container is started from the jenkins container, and it takes some time because the container is started from there based on the dockerfile of Git. Occasionally, the dashboard shows the error pending—Jenkins does n’t have label docker-agent.

View Console Output After confirming the start of the container from jenkins, run the container on the console

$ docker run -d -p 80:80 katacoda/jenkins-demo:latest

~~~

$ curl host01
<h1>This request was processed by host: </h1> 

図3.png

docke imageCheck the container image with

図2.png

benhall/dind-jenkins-agentIs docker-agent image

katacoda/jenkins-demoShows the creation of a container with jenkins Also, focusing on the `TAG``` part, 1,2, latest``` are prepared. This means that in step 5 of "Create Build Project", `` $ {BUILD_NUMBER}` `` was managed as a tag, and the container build process by jenkins was performed twice.

Recommended Posts

Experience CI / CD with katacoda (for beginners) --Part10 (Building Docker Images using Jenkins)
CI / CD practice for beginners --Part2 --CI / CD tool construction
CI / CD practice for beginners --Part1 --Environment construction
CI / CD practice for beginners --Part3 --Preparation for development project
Environment construction with Docker for beginners
[Commentary with images] Github beginners summarized gitignore
Learn Java with "So What" [For beginners]
Experience CI / CD with katacoda (for beginners) --Part10 (Building Docker Images using Jenkins)
[For beginners] Test devise user registration with RSpec
[Rails] Beginners can resize images with image_tag (groping)
Environment construction with Docker for beginners
Try building Express + PostgreSQL + Sequelize with Docker [Part 2]
Try building Express + PostgreSQL + Sequelize with Docker [Part 1]
Binary tree memorandum for beginners (Part 3 Realization using class)
Tutorial to create a blog with Rails for beginners Part 2
Tutorial to create a blog with Rails for beginners Part 0
Measures for permissions when building MySQL with Docker on WSL2