Mahyar
Mahyar

Reputation: 1121

Deploy/run the app from Jenkins pipeline with agent docker

I'm trying to have a sample pipeline (using declarative) which builds/tests/ and deploys a sample (node) app.

I'm using agent docker which runs the job in a container ... config and test phases are working fine :

pipeline {
  agent {
    docker { image 'node:latest' }
  }
  stages {
    stage('config') {
      steps {
        sh 'npm install'
      }
    }
    stage('run test') {
      steps {
        sh 'npm test'
      }
    }

  }

the problem is how to add the deployment stage essentially builds a docker image and runs it like :

docker build -t myapp .
docker run -d myapp

(let's assume this is the way I wanna deploy/run the app ... and have a Dockerfile)

the problem is how to deploy the sample app (use the docker commands) ... adding deploy stage here won't work since I'm using the docker agent (I guess it's running docker inside docker which sounds wrong!)

Should I kick off a new job - after pipeline succeeds - which uses agent any which runs the deployment commands? or there's a better way for running the app?

Upvotes: 1

Views: 599

Answers (1)

Jroger
Jroger

Reputation: 265

Docker inside docker is not wrong if made right! You can mount parent socket on container node:

/var/run/docker.sock:/var/run/docker.sock

In Jenkins, Go to Manage -> Configure System -> scroll to Cloud -> click on Docker Agent templates -> Container settings....

you will see something like the picture below:

enter image description here

Use an image like this for agent:

FROM jenkins/jnlp-slave
USER root
RUN apt-get update
RUN apt-get -y  install \
                   apt-transport-https \
                   ca-certificates \
                   curl \
                   gnupg2 \
                   software-properties-common
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian stretch stable"
RUN apt-get update
RUN apt-get -y  install \
               docker-ce \
               docker-ce-cli \
               containerd.io

And to execute the docker within the job use:

withDockerContainer(image: IMAGE_NAME)
{
    ...
}

useful links:

https://adamcod.es/2017/08/19/docker-patterns-socket-mount.html https://docs.docker.com/v17.09/engine/reference/commandline/dockerd/#examples

Upvotes: 1

Related Questions