1

How can I run a MongoDB docker image, run the tests and then kill it using Jenkins Pipeline? Here is the Pipeline now:

pipeline {
    agent {
        docker {
            image 'node' 
            args '-p 8080:8080' 
        }
    }
    stages {
        stage('Build') { 
            steps {
                sh 'npm install'
                sh 'npm build'
            }
        }
        stage('test') { 
            steps {
                sh 'npm test'
            }
        }
    }
}

I suppose Jenkins has to run the node image, and also the mongo image and link them together (to be able to access mongodb). Any standard solution?

user1079877
  • 9,008
  • 4
  • 43
  • 54
  • 2
    docker-compose? – Alex Blex Sep 21 '18 at 12:40
  • Does it even get docker-compose? How I'm gonna run it? using sh? – user1079877 Sep 21 '18 at 14:59
  • jenkins with docker-compose: https://stackoverflow.com/questions/37214628/does-jenkins-pipeline-plug-in-support-docker-compose. Native multi-container pipeline: https://jenkins.io/doc/book/pipeline/docker/#using-multiple-containers, although I find it less flexible - when mongo container starts it requires some time after that to spin-up the database and start accepting requests on 27017. – Alex Blex Sep 21 '18 at 15:14
  • Thanks for the answer, but this solution is when you want to run 2 containers after each other, not at same time. – user1079877 Sep 23 '18 at 13:24
  • 1
    It's not an answer. It's a comment. Or rather a question - is anything stops you using docker-compose? If I understand you need both container running at the same time - the db starts first, waits for init, then start the app, run tests, and stop both. Docker-compose should be sufficient with it's native `depends_on` and `healthcheck` or custom `wait_for`. – Alex Blex Sep 23 '18 at 21:31
  • I think it should be enough. But I'm wondering why there's nothing like that already in Pipeline. It's one of the most important requirements for testing. Many applications need to have some containers up for other Mock services or database. – user1079877 Sep 24 '18 at 13:37

0 Answers0