Search code examples
dockerjenkinsjenkins-pipelinecicd

How to build and upload docker-compose file inside jenkins pipeline


I need to build a docker image using docker-compose.yaml file and then I need to push it on dockerhub. I used following command to build Dockerfile and push it to dockerhub with jenkins docker plugin.

stage('Building image and Push') {
      steps{
        script {
          customImage = docker.build("my-image:${env.BUILD_ID}")
          customImage.push()
        }
      }
    }

Is there a similar method to write this kind of command to build and push docker-compose file?

PS: I heard about about a plugin called Docker compose build step. Can I do same as above with this plugin?


Solution

  • This does not seem supported in a scripted pipeline.

    Using purely a declarative pipeline on an agent based on the docker-compose image (so with docker-compose preinstalled), I suppose you can shell script those commands directly:

    stage('Build Docker Image') {  
        steps{                     
        sh 'docker-compose build'     
        echo 'Docker-compose-build Build Image Completed'                
        }           
    }
    

    And then login to DockerHub and push, as described in "Simple Jenkins Declarative Pipeline to Push Docker Image To Docker Hub"

    stage('Login to Docker Hub') {          
        steps{                          
        sh 'echo $DOCKERHUB_CREDENTIALS_PSW | sudo docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'                     
        echo 'Login Completed'      
        }           
    }
    
    stage('Push Image to Docker Hub') {         
        steps{                            
     sh 'sudo docker push <dockerhubusername>/<dockerhubreponame>:$BUILD_NUMBER'           
    echo 'Push Image Completed'       
        }            
    }
    

    A manual approach, since an "agent docker-compose" is not directly available (you have docker agent docker/dockerfile/kubernetes, but no docker-compose option)