본문 바로가기

리눅스

[리눅스] Jenkins 이용하여 Docker Image 만들기

반응형

Jenkins 이용하여 Docker Image 만들기

 - HowToDo.cloud를 보고 재구성하였습니다.

 

애플리케이션 개발

main.js

// load the http module
var http = require('http');

// configure our HTTP server
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

// listen on localhost:8000
server.listen(8000);
console.log("Server listening at http://127.0.0.1:8000/");

package.json

{
  "name": "hellonode",
  "version": "1.0.0",
  "description": "A Hello World HTTP server",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node main.js"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/anti1346/project1/"
  },
  "keywords": [
    "node",
    "docker",
    "dockerfile"
  ],
  "license": "ISC"
}

 

컨테이너 이미지 생성

Dockerfile

# use a node base image
FROM node:7-onbuild

# set a health check
HEALTHCHECK --interval=5s \
            --timeout=5s \
            CMD curl -f http://127.0.0.1:8000 || exit 1

# tell docker what port to expose
EXPOSE 8000

 

Jenkinsfile 생성

Jenkinsfile

 node {
     def app

     stage('Clone repository') {
         /* Let's make sure we have the repository cloned to our workspace */

         checkout scm
     }

     stage('Build image') {
         /* This builds the actual image; synonymous to
         * docker build on the command line */

         app = docker.build("anti1346/myapp-jenkins")
     }

     stage('Test image') {
         app.inside {
             sh 'echo "Tests passed"'
         }
     }

     stage('Push image') {
         /* Finally, we'll push the image with two tags:
         * First, the incremental build number from Jenkins
         * Second, the 'latest' tag.
         * Pushing multiple tags is cheap, as all the layers are reused. */
         docker.withRegistry('https://registry.hub.docker.com', 'jenkinsfordockerhub') {
             app.push("${env.BUILD_NUMBER}")
             app.push("latest")
         }
     }
 }
728x90

GitHub Personal Access Token 생성

계정 > Settings

 

계정 > Settings > Developer settings > Personal access tokens > Generate new token

 

Docker Hub Access Token 생성

계정 > Account Settings

 

계정 > Account Settings > Security > New Access Token

 

Jenkins 설정

Credentials 설정

Jenkins > Jenkins 관리 > Manage Credentials

 

GitHub 계정 등록

 

DockerHub 계정 등록

 

새로운 Item 생성

새로운 Item > Pipeline > Pipeline

 

Stage View

 

출처 : Jenkins 이용하여 Docker Image 만들기

https://www.howtodo.cloud/devops/docker/2019/05/16/devops-application.html

 

728x90
반응형