Operations 11 min read

From Zero to CI/CD: Deploy Jenkins in Docker with Blue Ocean Pipelines

Learn how to install Jenkins inside Docker using the jenkinsci/blueocean image, configure ports and volumes, unlock the instance, set up credentials for Git and SSH, create a multi‑branch pipeline with Blue Ocean, and write a Jenkinsfile that automates build, test, and deployment steps.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
From Zero to CI/CD: Deploy Jenkins in Docker with Blue Ocean Pipelines

Origin

Because the company's Jenkins configuration lacked deployment notifications, after learning Jenkins I decided to fix it. Installing the dingtalk plugin caused previous job data loss, giving me a chance to start from scratch.

Install and Run Jenkins in Docker

Assume Docker is already installed on the server.

Use the jenkinsci/blueocean image, which includes Blue Ocean and other plugins.

Pull the image: docker pull jenkinsci/blueocean Run Jenkins:

docker run -idt --name kmywjenkins -p 9090:8080 -p 60000:50000 -v jenkins-data:/var/jenkins_home -v /data/web-data/docker.sock:/var/run/docker.sock jenkinsci/blueocean

Parameter explanation:

-idt runs the container interactively with a pseudo‑TTY.

--name sets the container alias.

-p maps host ports to container ports.

-v jenkins-data:/var/jenkins_home mounts a persistent Jenkins home directory.

-v /data/web-data/docker.sock:/var/run/docker.sock allows the container to access the host Docker daemon.

Note: Docker runs Jenkins as the jenkins user by default; add -u root if root privileges are needed.

Access the Jenkins Docker container

You can enter the container with: docker exec -it [containerid] bash Restart Jenkins with:

docker restart [containerid]

Unlock Jenkins

Retrieve the initial admin password:

docker exec kmywjenkins cat /var/jenkins_home/secrets/initialAdminPassword

Enter the token in the browser to unlock Jenkins.

Jenkins unlock screen
Jenkins unlock screen

Create Credentials

To connect to Git repositories or SSH servers, create credentials in Jenkins. Example for a Git repository (using a tool called gitte): choose “Username with password”, provide the account credentials, and assign an ID for later reference.

Git credentials
Git credentials

For SSH connections, generate a public/private key pair on the server, paste the private key into the “SSH Username with private key” credential, and set the username.

SSH credential
SSH credential

Create a Multibranch Pipeline

Previous jobs were Freestyle, which lack flexibility. Using a Declarative Pipeline allows independent builds for each branch.

Blue Ocean provides a visual UI for pipelines. After installing the Blue Ocean plugin (or using the blueocean image), open Blue Ocean to see existing pipelines.

Blue Ocean overview
Blue Ocean overview

Click “Create Pipeline”, select Git, provide the repository URL, and choose the previously created credentials.

Create pipeline
Create pipeline

Jenkins will scan the repository and detect branches containing a Jenkinsfile.

Detected pipelines
Detected pipelines

Jenkinsfile Basics

The following Jenkinsfile defines environment variables, parameters, triggers, and stages for building and deploying a front‑end project.

pipeline {
  agent any
  environment {
    HOST_TEST = '[email protected]'
    HOST_ONLINE = '[email protected]'
    SOURCE_DIR = 'dist/*'
    TARGET_DIR = '/data/www/kuaimen-yunying-front'
  }
  parameters {
    choice(description: 'Select deployment environment', name: 'env', choices: ['测试环境', '线上环境'])
    string(name: 'update', defaultValue: '', description: 'Update description')
  }
  triggers {
    GenericTrigger(
      genericVariables: [[key: 'ref', value: '$.ref']],
      causeString: 'Triggered on $ref',
      token: 'runcenter-front-q1w2e3r4t5',
      tokenCredentialId: '',
      printContributedVariables: true,
      printPostContent: true,
      silentResponse: false,
      regexpFilterText: '$ref',
      regexpFilterExpression: 'refs/heads/' + BRANCH_NAME
    )
  }
  stages {
    stage('获取git commit message') {
      steps {
        script {
          env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true).trim()
        }
      }
    }
    stage('打包') {
      steps {
        nodejs('nodejs-12.16') {
          echo '开始安装依赖'
          sh 'yarn'
          echo '开始打包'
          sh 'yarn run build'
        }
      }
    }
    stage('部署') {
      when { expression { params.env == '测试环境' } }
      steps {
        sshagent(credentials: ['km-test2']) {
          sh "ssh -o StrictHostKeyChecking=no ${HOST_TEST} uname -a"
          sh "scp -r ${SOURCE_DIR} ${HOST_TEST}:${TARGET_DIR}"
          sh 'echo "部署成功~"'
        }
      }
    }
    stage('发布') {
      when { expression { params.env == '线上环境' } }
      steps {
        sshagent(credentials: ['km-online']) {
          sh "ssh -o StrictHostKeyChecking=no ${HOST_ONLINE} uname -a"
          sh "scp -r ${SOURCE_DIR} ${HOST_ONLINE}:${TARGET_DIR}"
          sh 'echo "发布成功~"'
        }
      }
    }
  }
  post {
    success {
      dingtalk(
        robot: '77d4c82d-3794-4583-bc7f-556902fee6b0',
        type: 'MARKDOWN',
        atAll: true,
        title: '你有新的消息,请注意查收',
        text: [
          '# 运营管理系统发布通知',
          '---',
          '#### **所属:前端**',
          "#### **构建任务:${env.BUILD_DISPLAY_NAME}**",
          "#### **Git commit:${env.GIT_COMMIT_MSG}**",
          "#### **本次更新内容:${params.update}**",
          "#### **部署环境:${params.env}**",
          '#### **构建结果:成功**'
        ]
      )
    }
  }
}

Key points of the Jenkinsfile:

The pipeline must be defined at the top level. agent selects the execution environment. stages contain ordered stage blocks. steps run the actual commands. post runs after all stages, e.g., sending a DingTalk notification. when controls conditional execution. environment defines variables accessible throughout the pipeline. parameters allow user input.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Dockerci/cdDevOpsPipelineJenkinsBlue Ocean
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.