Docker images - types. Slim vs slim-stretch vs stretch vs alpine

前端 未结 2 1190
你的背包
你的背包 2020-12-22 20:15

I am looking to pick up a docker image to build a java app and looking at the variants of the OpenJDK images available. I am looking here https://github.com/docker-library/o

2条回答
  •  心在旅途
    2020-12-22 21:07

    Choose a base docker image that fits your needs and please keep in mind that Image size is an important aspect also.

    Image can be considered as a set of instructions on how to create the container. In Docker, one image could be inherited from (or based on) another image, adding additional instructions on top of base ones. Each image consists of multiple layers, which are effectively immutable.

    Plase read Crafting the perfect Java Docker build flow article.

    Docker image size is actually very important. The size has an impact on:

    • network latency: need to transfer Docker image over the web
    • storage: need to store all these bits somewhere
    • service availability and elasticity: when using a Docker scheduler, like Kubernetes, Swarm, Nomad, DC/OS or other (the scheduler can move containers between hosts)
    • security: do you really, I mean really need the libpng package with all its CVE vulnerabilities for your Java application?
    • development agility: small Docker images == faster build time and faster deployment


    To run a java application you need JRE at least. For example, for a spring project your image can be based on slim Alpine Linux with OpenJDK JRE:

    #simple dockerFile for java app:
    
    #here we are using Base Alpine Linux based image with OpenJDK JRE only
    #For Java 8, try this
    FROM openjdk:8-jre-alpine
    
    #For Java 11, try this
    #FROM adoptopenjdk/openjdk11:alpine-jre
    
    #copy application WAR/JAR (with libraries inside)
    COPY target/spring-boot-*.war/jar yourName.war/jar
    # specify default command
    CMD ["/usr/bin/java", "-jar", "/yourName.war/jar"]
    

    Also you can use docker history yourImageName to see all layers (and their size) that makes your image.

提交回复
热议问题