How to cache data for containers

0 votes

I am using a maven container,I want to know if there is any method using which i can cache the data whenever the jenkins pipeline runs so that i don't have to re-download various stuff like dependencies every time the pipeline runs?

Jul 9, 2019 in Docker by Sam
• 6,260 points
1,744 views

1 answer to this question.

0 votes

Many build tools like maven etc, will download external dependencies and cache them locally for future re-use. 

Since containers are initially created with "clean" file systems, this can result in slower Pipelines, as they may not take advantage of on-disk caches between subsequent Pipeline runs.

Pipeline supports adding custom arguments which are passed to Docker, allowing users to specify custom docker volumes to mount, which can be used for caching data on the agent between Pipeline runs. 

The following example will cache ~/.m2 between Pipeline runs utilizing the maven container, thereby avoiding the need to re-download dependencies for subsequent runs of the Pipeline.

Jenkinsfile (Declarative Pipeline)

pipeline {
    agent {
        docker {
            image 'maven:3-alpine'
            args '-v $HOME/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -B'
            }
        }
    }
}

In case you are using a scripted pipeline here's how you can do it:

Jenkinsfile (Scripted Pipeline)

node {
    /* Requires the Docker Pipeline plugin to be installed */
    docker.image('maven:3-alpine').inside('-v $HOME/.m2:/root/.m2') {
        stage('Build') {
            sh 'mvn -B'
        }
    }
}

answered Jul 9, 2019 by Sirajul
• 59,170 points

Related Questions In Docker

0 votes
2 answers
+1 vote
2 answers

How to run docker containers on different machines

You can use labels and selectors for ...READ MORE

answered Oct 23, 2018 in Docker by Laila
2,096 views
0 votes
1 answer

How to store data in external drive with Docker Postgres:9.3 image?

Apparently, the problem would be in your ...READ MORE

answered Jul 12, 2018 in Docker by Kalgi
• 2,680 points
1,590 views
0 votes
1 answer

How to obtain the Docker container's IP address from the host?

This can be done by executing the ...READ MORE

answered Jul 17, 2018 in Docker by Sophie may
• 10,590 points
7,779 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,060 points
1,554 views
0 votes
2 answers
0 votes
2 answers

How to open docker containers file system?

Adding on to @DareDev's answer there's one ...READ MORE

answered Aug 5, 2019 in Docker by Sirajul
• 59,170 points
17,935 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP