Docker cache gradle dependencies

后端 未结 4 1196
攒了一身酷
攒了一身酷 2020-12-12 22:41

I\'m trying to deploy our java web application to aws elastic beanstalk using docker, the idea is to be able to run the container locally for development and testing and eve

4条回答
  •  伪装坚强ぢ
    2020-12-12 23:00

    You might want to consider splitting your application image to two images: one for building the myapp.war and the other for running your application. That way, you can use docker volumes during the actual build and bind the host's ~/.gradle folder into the container performing the build. Instead of only one step to run your application, you would have more steps, though. Example:

    builder image

    FROM 
    
    # Add project Source
    # -> you can use a project specific gradle.properties in your project root
    # in order to override global/user gradle.properties
    ADD . /var/app/myapp
    
    RUN mkdir -p /root/.gradle
    ENV HOME /root
    # declare shared volume path
    VOLUME /root/.gradle
    WORKDIR /var/app/myapp/ 
    
    # Compile only
    CMD ["./gradlew", "war"]
    

    application image

    FROM 
    
    ADD ./ROOT.war /opt/tomcat/webapps/ROOT.war
    
    # Start Tomcat
    CMD ["/opt/tomcat/bin/catalina.sh", "run"]
    

    How to use in your project root, assuming the builder Dockerfile is located there and the application Dockerfile is located at the webapp subfolder (or any other path you prefer):

    $ docker build -t builder .
    $ docker run --name=build-result -v ~/.gradle/:/root/.gradle/ builder
    $ docker cp build-result:/var/app/myapp/myapp.war webapp/ROOT.war
    $ cd webapp
    $ docker build -t application .
    $ docker run -d -P application
    

    I haven't tested the shown code, but I hope you get the idea. The example might even be improved by using data volumes for the .gradle/ cache, see the Docker user guide for details.

提交回复
热议问题