How to cache maven dependencies in Docker

前端 未结 3 1577
情深已故
情深已故 2021-02-04 16:35

I\'m working on a project with ~200MB dependencies and i\'d like to avoid useless uploads due to my limited bandwidth.

When I push my Dockerfile (i\'ll attach it in a m

3条回答
  •  攒了一身酷
    2021-02-04 17:21

    In general Dockerfile container build, works in layers and each time you build these layers are available in catch and is used if there are no changes. Ideally it should have worked same way.

    Maven generally looks for dependencies by default in .m2 folder located in Home dir of User in Ubuntu /home/username/

    If dependent jars are not available then it downloads those jars to .m2 and uses it.

    Now you can zip and copy this .m2 folder after 1 successful build and move it inside Docker Container User's Home directory.

    Do this before you run build command

    Note: You might need to replace existing .m2 folder in docker

    So your Docker file would be something like this

    FROM maven:3.6.0-jdk-8-slim
    
    WORKDIR /app
    
    COPY .m2.zip /home/testuser/
    
    ADD pom.xml /app
    
    RUN mvn verify clean --fail-never
    
    COPY ./src /app/src
    
    RUN mvn package
    ...
    

提交回复
热议问题