文档章节

alpine Java docker images

MtrS
 MtrS
发布于 2016/09/24 21:00
字数 1099
阅读 66
收藏 0

Smaller Java images with Alpine Linux

August 10th 2015 Nicola Paolucci in Java, Docker, Linux

Sometimes I need to be hit in the head with an axe to find a solution to a problem that has been bugging me forever. A minimal Java container has been on my wish list since I found out about Docker and I've been writing about running Java in Docker for sometime already.

alot

Official Java images have historically been mastodontic - cue picture above - I just tried "docker pull java" and I got an image of 816.4MB. A colleague of mine few days ago mentioned Alpine Linux, a minimalistic Linux distribution based on musl libc and BusyBox that comes with a nice package manager. And the base image is ... 5Mb ?! Where have I been hiding? Why didn't I know about this?! Anyway here's my chance to make things right. The objective: to have a minimal Java container for my (and your) applications. Let's dream together. If OpenJDK 7 is good enough

If you are still using JDK7 and you don't have a strong requirement to have the Oracle version, the easiest and leaner image I found is very simple to setup with this Dockerfile:

FROM alpine:3.2
RUN apk --update add openjdk7-jre
CMD ["/usr/bin/java", "-version"]
``

    Build it with:

docker build -t yourname/minimal-java .

    Or if you are lazy just:

docker pull durdn/minimal-java

    Now you can test that java is installed with:

docker run -t durdn/minimal-java

Which outputs:

java version "1.7.0_79"

OpenJDK Runtime Environment (IcedTea 2.5.5) (Alpine 7.79.2.5.5-r0)

OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

The result is a Java 7 runtime environment, ready for your Java 7 applications in only 123MB instead than 800+MB. NICE!
What about Oracle JRE/JDK 8?

For many applications teams prefer or require the Oracle JDK. In this case we can't use Alpine package manager (yet), we have to wrangle the installation ourselves from the official Oracle packages. Do you want to see how that's done? This is the list of steps:

    Install curl, tar, and ca-certificates on the base alpine image.
    Install glibc-2.21 which is a hard dependency of Java 8.
    Download the Oracle JRE/JDK using tricks in this SO article.
    Remove spurious folders not needed (like jdk/jre/lib/desktop and others...).
    Set the proper environment variables.

The whole process is well laid out amongst others in a clean Dockerfile by anapsix which I list here for completeness:

# AlpineLinux with a glibc-2.21 and Oracle Java 8

​

FROM alpine:3.2

MAINTAINER Anastas Dancha [...]

Install cURL

RUN apk --update add curl ca-certificates tar && \

curl -Ls https://circle-artifacts.com/gh/andyshinn/alpine-pkg-glibc/6/artifacts/0/home/ubuntu/alpine-pkg-glibc/packages/x86_64/glibc-2.21-r2.apk > /tmp/glibc-2.21-r2.apk && \

apk add --allow-untrusted /tmp/glibc-2.21-r2.apk

Java Version

ENV JAVA_VERSION_MAJOR 8

ENV JAVA_VERSION_MINOR 45

ENV JAVA_VERSION_BUILD 14

ENV JAVA_PACKAGE jdk

Download and unarchive Java

RUN mkdir /opt && curl -jksSLH "Cookie: oraclelicense=accept-securebackup-cookie"\

http://download.oracle.com/otn-pub/java/jdk/${JAVA_VERSION_MAJOR}u${JAVA_VERSION_MINOR}-b${JAVA_VERSION_BUILD}/${JAVA_PACKAGE}-${JAVA_VERSION_MAJOR}u${JAVA_VERSION_MINOR}-linux-x64.tar.gz \

| tar -xzf - -C /opt &&\

ln -s /opt/jdk1.${JAVA_VERSION_MAJOR}.0_${JAVA_VERSION_MINOR} /opt/jdk &&\

rm -rf /opt/jdk/*src.zip \

       /opt/jdk/lib/missioncontrol \

       /opt/jdk/lib/visualvm \

       /opt/jdk/lib/*javafx* \

       /opt/jdk/jre/lib/plugin.jar \

       /opt/jdk/jre/lib/ext/jfxrt.jar \

       /opt/jdk/jre/bin/javaws \

       /opt/jdk/jre/lib/javaws.jar \

       /opt/jdk/jre/lib/desktop \

       /opt/jdk/jre/plugin \

       /opt/jdk/jre/lib/deploy* \

       /opt/jdk/jre/lib/*javafx* \

       /opt/jdk/jre/lib/*jfx* \

       /opt/jdk/jre/lib/amd64/libdecora_sse.so \

       /opt/jdk/jre/lib/amd64/libprism_*.so \

       /opt/jdk/jre/lib/amd64/libfxplugins.so \

       /opt/jdk/jre/lib/amd64/libglass.so \

       /opt/jdk/jre/lib/amd64/libgstreamer-lite.so \

       /opt/jdk/jre/lib/amd64/libjavafx*.so \

       /opt/jdk/jre/lib/amd64/libjfx*.so

Set environment

ENV JAVA_HOME /opt/jdk

ENV PATH ${PATH}:${JAVA_HOME}/bin



The result of building this image or pulling from anapsix/alpine-java is a fully functional Oracle Java 8 image weighing only 173Mb. Impressive!
Running Atlassian Stash on it

The whole point of the exercise above was for me to run a leaner container with Stash - our enterprise Git server - trying to shave space off from our official image. The task was a success - if not a smashing one. The final Stash image I produced weighs 368MB which adds up to a ~30% reduction over the official image. Here how I had to tweak the Dockerfile:

FROM durdn/minimal-java8:stripped

​

MAINTAINER Atlassian Stash Team

​

ENV DOWNLOAD_URL        https://downloads.atlassian.com/software/stash/downloads/atlassian-stash-

​

ENV STASH_HOME          /var/atlassian/application-data/stash

​

RUN apk --update add git tar bash

​

# Install Atlassian Stash to the following location

ENV STASH_INST_DIR   /opt/atlassian/stash

​

ENV STASH_VERSION 3.11.1

​

RUN mkdir -p             ${STASH_INST_DIR}                                                     \

  && curl -LO --silent   ${DOWNLOAD_URL}${STASH_VERSION}.tar.gz                                \

  && tar -xf atlassian-stash-${STASH_VERSION}.tar.gz -C ${STASH_INST_DIR} --strip-components 1 \

  && rm                  atlassian-stash-${STASH_VERSION}.tar.gz                               \

  && mkdir -p            ${STASH_INST_DIR}/conf/Catalina                                       \

  && chmod -R 700        ${STASH_INST_DIR}/conf/Catalina                                       \

  && chmod -R 700        ${STASH_INST_DIR}/logs                                                \

  && chmod -R 700        ${STASH_INST_DIR}/temp                                                \

  && chmod -R 700        ${STASH_INST_DIR}/work                                  

​

VOLUME ["${STASH_INST_DIR}"]

​

# HTTP Port

EXPOSE 7990

​

# SSH Port

EXPOSE 7999

​

WORKDIR $STASH_INST_DIR

​

# Run in foreground

CMD ["./bin/start-stash.sh", "-fg"]

Bonus trick: How to strip an image

I love the layering ability of Docker images but for base images upon which I'll build my stacks often I'd like them to consist of a single layer. It's a mental thing more than anything so excuse my weirdness if you can.

Many times the extra layers in your base images will not be re-used. For those situations it can be helpful to strip an image of all its layers and flatten it. The technique to accomplish that is the following:

    First run it so that you have a container to refer to:

docker run -t durdn/minimal-java /bin/true

    Then export it and re-import it:

docker export `docker ps -q -n=1` | docker import - durdn/minimal-java:stripped

    Verify that the new image only has one layer with:

docker history durdn/minimal-java:stripped

​

IMAGE               CREATED             CREATED BY          SIZE                COMMENT

8eb82b59dee6        31 seconds ago                          172.9 MB            Imported from -

​

docker run -ti durdn/minimal-java8:stripped /opt/jdk/bin/java -version

​

java version "1.8.0_45"

Java(TM) SE Runtime Environment (build 1.8.0_45-b14)

Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

Conclusions

That's it for my discoveries today. Pings, likes, comments, love or hate gladly received here in the comments, at @durdn or at my awesome team @atlassiandev.

(Credit for the epic alot picture goes to Hyperbole and a half).

本文转载自:https://developer.atlassian.com/blog/2015/08/minimal-java-docker-containers/

共有 人打赏支持
下一篇: openshift
MtrS
粉丝 32
博文 586
码字总数 388629
作品 0
榆林
私信 提问
最小化 Java 镜像的常用技巧

背景 随着容器技术的普及,越来越多的应用被容器化。人们使用容器的频率越来越高,但常常忽略一个基本但又非常重要的问题 - 容器镜像的体积。本文将介绍精简容器镜像的必要性并以基于 spring...

吴波bruce_wu
01/11
0
0
CentOS学习笔记 - 9. docker maven编译基于gofabric8的java应用镜像

续上一篇 1. 修改java工程的pom.xml , 加入docker编译插件 2. 编写Dockerfile, 放到docker目录下面 docker目录在 3. 编译镜像 4. 查看镜像 5. 运行镜像...

七月星空
2017/10/24
0
0
Docker | 第五章:构建自定义镜像

前言 上一章节,主要是介绍了下的一些常用命令的说明。我们知道,利用可以构建一个新的镜像,比如运行环境,就需要一个环境的镜像,但直接使用公共的镜像时,一般上大小都比较大。所以本章节...

oKong
2018/08/04
0
0
Alpine Docker 安装 bash

Alpine Linux是一个轻型Linux发行版,它不同于通常的Linux发行版,Alpine采用了musl libc 和 BusyBox以减少系统的体积和运行时的资源消耗。Alpine Linux提供了自己的包管理工具:apk,我们可...

时间之弧
2018/04/12
0
0
docker入门与实践之【06-其他镜像制作方式】

1.从压缩包导入 格式: 可以使用history命令查询构建记录: 2. 和 使用 docker save 命令可以将镜像保存为归档文件。 比如我们希望保存这个 alpine 镜像。 保存镜像的命令为: 然后我们将 al...

Funcy1122
2018/06/08
0
0

没有更多内容

加载失败,请刷新页面

加载更多

泛型就这么简单

前言 从今天开始进入Java基础的复习,可能一个星期会有一篇的<十道简单算法>,我写博文的未必都是正确的~如果有写错的地方请大家多多包涵并指正~ 今天要复习的是泛型,泛型在Java中也是个很...

群星纪元
18分钟前
0
0
大数据提醒你:中国这些古建筑,可能是下一个巴黎圣母院!

大家晚上好,我是今天的提笔人嗅嗅。 巴黎圣母院失火事件让我的心情很沉重,一句无关痛痒的安慰“巴黎不哭”,已经不能表达我对这场文化之殇的惋惜之痛,人类伟大的建筑在一瞬间被毁灭。 世界...

forespider
29分钟前
0
0
mysql函数substring_index的用法

substring_index 按索引字符位进行截取字符串 substring_index(“待截取的字符串”,“截取数据依据的字符”,截取字符的位置N) 第三个参数可正,可负。正数表示索引字符前面的字符串,负数...

echojson
29分钟前
0
0
好程序员web前端分享用CSS和JS打造一个简单的图片编辑器

好程序员web前端分享用CSS和JS打造一个简单的图片编辑器,本文主要是利用CSS的 filter和简单的Jquery代码来实现一个简单的图片编辑器,包括对图片的透明度,黑白,图片亮度等调节。 CSS filt...

好程序员IT
39分钟前
2
0
浅析spring mvc的细节

spring mvc 整体结构 系统监听到请求 -> 通知tomcat -> 根据web.xml 通知相应的拦截器(spring mvc 通常指DispatcherServlet) --> 检查url是否有相匹配的请求实现 --> 拿到请求实现bean的适配...

最爱肉肉
40分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部