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 in Docker by Sam
• 1,560 points
37 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 by Sirajul
• 25,700 points

Related Questions In Docker

0 votes
1 answer
+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
76 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,620 points
117 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 16, 2018 in Docker by Sophie may
• 9,530 points
407 views
0 votes
1 answer
0 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
• 56,520 points
156 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 in Docker by Sirajul
• 25,700 points
69 views