Learn SpringCloud from Me | Article 18: The Basic Environment for Docker ization of Micro Services

1. Containerization

With the advent of Docker, container technology has made a qualitative leap. Docker standardizes the infrastructure of services, unifies the packaging and distribution of applications, deployment and operating system-related class libraries, and solves the problem of environmental differences in test production deployment.For operations, service deployment and rollback operations are easier because of the immutability of the mirror.With a variety of third-party container management platforms, it is easy to achieve one-click deployment, dynamic scaling and other operations.

2. Basic Mirror Selection

For operating system selection, you can choose traditional CentOS, Ubuntu, or a lighter Alphaine.For example, CentOS or Ubuntu have mirrors over 100MB, and tens of MB after compression, whereas the lightweight version of Alphaine 3.10 has a mirror size of about 5.58MB, and it has only about 2MB after compression.

The Alpine operating system is a lightweight security-oriented Linux distribution.It differs from Linux distributions in that Alpine uses musl libc and busybox to reduce system size and run-time resource consumption, but is much more functional than busybox, so it is increasingly favored by the open source community.While keeping slim, Alpine also provides its own package management tool, apk.

One option for basic mirroring is to consider the size of the mirror, and the other is to provide only minimal dependency packages.As for the second point, the dependent packages required for different services are different, so we will not discuss them here. If you only consider the first point, Alpine is definitely the preferred one. The smaller the mirror, the faster the remote push and pull, the smaller the resources consumed, and the more convenient it is. We use Alpine as the base image here.

3. Dockerfile Writing

One of the more troublesome aspects of selecting Alpine is that Alpine uses the C standard library of musl libc, while Oracle or OpenJDK provide versions that are predominantly glibc-based.So let's consider adding glibc to Alpine and then the JDK compiled version of glibc as the base image.

3.1 Alpine + glibc

The version selected here is the latest version of Alpine 3.10, which uses the Sgerrand open source glibc installation package (https://github.com/sgerrand/alpine-pkg-glibc/), version 2.30-r0.The code is as follows:

List of codes: chapter17/dockerfiles/alpine-glibc/Dockerfile
***

FROM alpine:3.10
MAINTAINER inwsy@hotmail.com
RUN apk add --no-cache ca-certificates curl openssl binutils xz tzdata \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone \
    && GLIBC_VER="2.30-r0" \
    && ALPINE_GLIBC_REPO="https://github.com/sgerrand/alpine-pkg-glibc/releases/download" \
    && curl -Ls ${ALPINE_GLIBC_REPO}/${GLIBC_VER}/glibc-${GLIBC_VER}.apk > /opt/${GLIBC_VER}.apk \
    && apk add --allow-untrusted /opt/${GLIBC_VER}.apk \
    && curl -Ls https://www.archlinux.org/packages/core/x86_64/gcc-libs/download > /opt/gcc-libs.tar.xz \
    && mkdir /opt/gcc \
    && tar -xf /opt/gcc-libs.tar.xz -C /opt/gcc \
    && mv /opt/gcc/usr/lib/libgcc* /opt/gcc/usr/lib/libstdc++* /usr/glibc-compat/lib \
    && strip /usr/glibc-compat/lib/libgcc_s.so.* /usr/glibc-compat/lib/libstdc++.so* \
    && curl -Ls https://www.archlinux.org/packages/core/x86_64/zlib/download > /opt/libz.tar.xz \
    && mkdir /opt/libz \
    && tar -xf /opt/libz.tar.xz -C /opt/libz \
    && mv /opt/libz/usr/lib/libz.so* /usr/glibc-compat/lib \
    && apk del binutils \
    && rm -rf /opt/${GLIBC_VER}.apk /opt/gcc /opt/gcc-libs.tar.xz /opt/libz /opt/libz.tar.xz /var/cache/apk/*

Here are a few things to note:

  • Since Docker is a layered design, in Dockerfile, each instruction has its own context, and when executed to the next instruction, the next layer of builds is superimposed on the previous layer, so it is best to write commands in the same RUN instruction when installing class libraries, reducing layers and lowering the last mirroredSize
  • Class libraries or packages are installed in the RUN command, and the cache of the apk needs to be removed in the same command in order to effectively remove the apk and reduce the mirror size.
  • Do not use latest for the label of the underlying mirror. The latest label is used by default when the mirror does not specify a label.When the image is updated, the latest tag points to a different image, and building the image may fail.If you do need to use the latest version of the underlying image, you can use the latest tag; otherwise, you may want to specify a specific image tag.
  • Here, the author has created a version that can be uploaded to the mirror warehouse of Aliyun. Readers in need can pull this mirror directly.
docker pull registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:glibc-2.30-r0

3.2 Alpine + glibc + JDK8

For JDK version selection, there are Oracle's Hotspot JDK and OpenJDK.Here we used Oracle's server-jre-8u221 version when building JDK8.For JDK9, JDK10, and JDK11, we use OpenJDK to build them.

Oracle's JDK8 mirror builds the following Dockerfile:

Code List: chapter17/dockerfiles/java8/Dockerfile
***

FROM registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:glibc-2.30-r0
MAINTAINER inwsy@hotmail.com
ADD server-jre-8u221-linux-x64.tar.gz /opt/
RUN chmod +x /opt/jdk1.8.0_221
ENV JAVA_HOME=/opt/jdk1.8.0_221
ENV PATH="$JAVA_HOME/bin:${PATH}"

Similarly, this mirror author has uploaded the Ali Cloud Mirror Warehouse and can be retrieved directly using the following commands:

docker pull registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:8u221-jre

It can be validated with the following commands:

docker run --rm -it registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:8u221-jre java -version

The results are as follows:

java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

3.3 Alpine + glibc + OpenJDK9

The OpenJDK9 mirror builds the following Dockerfile:

Code List: chapter17/dockerfiles/java9/Dockerfile
***

FROM registry.cn-shanghai.aliyuncs.com/weishiyao/alpine-3.10:glibc-2.30-r0
MAINTAINER inwsy@hotmail.com

RUN echo -e "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.10/main\n\
https://mirror.tuna.tsinghua.edu.cn/alpine/v3.10/community" > /etc/apk/repositories

RUN apk --update add curl bash openjdk9-jre && \
      rm -rf /var/cache/apk/*

ENV JAVA_HOME /usr/lib/jvm/default-jvm
ENV PATH ${PATH}:${JAVA_HOME}/bin

OpenJDK's jre Here the author uses the mirror of the mirror station of Tsinghua University to install it.

Similarly, this mirror author has uploaded the Ali Cloud Mirror Warehouse and can be retrieved directly using the following commands:

docker pull registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:openjdk9-jre-9.0.4

The validation commands are as follows:

docker run --rm -it registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:openjdk9-jre-9.0.4 java -version

The results are as follows:

openjdk version "9.0.4"
OpenJDK Runtime Environment (build 9.0.4+12-alpine-r1)
OpenJDK 64-Bit Server VM (build 9.0.4+12-alpine-r1, mixed mode)

3.4 Alpine + glibc + OpenJDK10

The mirror image of OpenJDK10 constructs the following Dockerfile:

Code List: chapter17/dockerfiles/java10/Dockerfile
***

FROM registry.cn-shanghai.aliyuncs.com/weishiyao/alpine-3.10:glibc-2.30-r0

RUN echo -e "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.10/main\n\
https://mirror.tuna.tsinghua.edu.cn/alpine/v3.10/community" > /etc/apk/repositories

RUN apk --update add curl bash openjdk10-jre && \
      rm -rf /var/cache/apk/*

ENV JAVA_HOME /usr/lib/jvm/default-jvm
ENV PATH ${PATH}:${JAVA_HOME}/bin

Similarly, this mirror author has uploaded the Ali Cloud Mirror Warehouse and can be retrieved directly using the following commands:

docker pull registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:openjdk10-jre-10.0.2

The validation commands are as follows:

docker run --rm -it registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:openjdk10-jre-10.0.2 java -version

The results are as follows:

openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-alpine-r0)
OpenJDK 64-Bit Server VM (build 10.0.2+13-alpine-r0, mixed mode)

3.5 Alpine + glibc + OpenJDK11

The mirror of OpenJDK11 builds the following Dockerfile:

List of codes: chapter17/dockerfiles/java11/Dockerfile
***

FROM registry.cn-shanghai.aliyuncs.com/weishiyao/alpine-3.10:glibc-2.30-r0
MAINTAINER inwsy@hotmail.com
RUN echo -e "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.10/main\n\
https://mirror.tuna.tsinghua.edu.cn/alpine/v3.10/community" > /etc/apk/repositories

RUN apk --update add curl bash openjdk11-jre && \
      rm -rf /var/cache/apk/*

ENV JAVA_HOME /usr/lib/jvm/default-jvm
ENV PATH ${PATH}:${JAVA_HOME}/bin

Similarly, this mirror author has uploaded the Ali Cloud Mirror Warehouse and can be retrieved directly using the following commands:

docker pull registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:openjdk11-jre-11.0.2

The validation commands are as follows:

docker run --rm -it registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:openjdk11-jre-11.0.2 java -version

The results are as follows:

openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment (build 11.0.4+4-alpine-r1)
OpenJDK 64-Bit Server VM (build 11.0.4+4-alpine-r1, mixed mode)

4. Instance Code

Sample Code-Github

Sample Code-Gitee

Keywords: Java glibc Docker curl

Added by Iceman512 on Mon, 23 Sep 2019 04:48:42 +0300