Follow me SpringCloud | CHAPTER 18: Docker basis of the micro-environment services

1. containerized

Docker turned out, the container technology to bring a qualitative leap, Docker standardized infrastructure services, packaged unified distribution, deployment, and operating system-related class libraries to solve the environmental differences when the test production deployment problem. For operation and maintenance is concerned, due to the inflexibility of the mirror, easier service deployment and rollback operations. Containers using various third-party management platform, a key deployment, dynamic expansion and contraction of the operation becomes easy.

2. Select the base image

In the operating system of choice, choose traditional CentOS, Ubuntu or more lightweight Alpine. Such as CentOS or Ubuntu mirrored in 100MB or more, after compression also has several dozen MB, while the lightweight Alpine 3.10 version of the image size is about 5.58MB, and after it is compressed only about 2MB in size.

Alpine is a secure operating system for lightweight Linux distribution. It is different from the usual Linux distributions, Alpine uses musl libc busybox to reduce resource consumption and system size and run-time, but they function more perfect than busybox, so more and more favored by the open source community. While maintaining the thin, Alpine also provides its own package management tool apk.

On the base image selection, consider the size of a mirror, the other to provide only minimal dependencies. On the second point, the different services required dependencies are different, no longer discuss here, if only from the first point to consider, then, Alpine is definitely the first choice, the smaller the image, the remote sliding faster consumption of resources the smaller, more convenient, we used here as a basis for Alpine mirror.

Written 3. Dockerfile

Select Alpine has a troublesome place is musl libc Alpine uses the standard C library, or Oracle OpenJDK version is mainly provided by glibc has been based. Therefore, we consider Alpine plus glibc, then add the JDK version to compile glibc as a base image.

3.1 Alpine + glibc

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

Listing: chapter17 / dockerfiles / Alpine-glibc / Dockerfile
***

FROM alpine:3.10
MAINTAINER [email protected]
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 points to note:

  • Since Docker is layered design, in Dockerfile, each context has its own instruction, the next instruction to be executed when will the next layer is added to construct the laminate layer, and therefore in the installation library the best time to commands written in the same RUN command, reducing stratification, reduce the size of the final image
  • RUN command is installed in the library or package, you need to delete the cache apk in the same command, so as to effectively delete apk, reduce the image size.
  • Do not use the base image tag latest, when the mirror is no label is specified, will use the latest default label. When the image updates, latest labels will point to a different mirror, then build mirroring may fail. If you really need the latest version of the base image, you can use the latest label, otherwise, it is best to specify certain image tag.
  • Here I have created a version, uploaded to the cloud mirrored Ali warehouse, there is a need readers can directly pull this image use.
docker pull registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:glibc-2.30-r0

3.2 Alpine + glibc + JDK8

For JDK version selected, there is Oracle's Hotspot JDK, there OpenJDK. Here we use Oracle's server-jre-8u221 version when building JDK8 of. For JDK9, JDK10 and JDK11 we use OpenJDK to be built.

Oracle's JDK8 Dockerfile mirror constructed as follows:

Listing: chapter17 / dockerfiles / java8 / Dockerfile
***

FROM registry.cn-shanghai.aliyuncs.com/springcloud-book/alpine3.10:glibc-2.30-r0
MAINTAINER [email protected]
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}"

Again, this author has uploaded a mirror image of Ali cloud storage, you can use the following command to pull directly:

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

Can be verified, the command is as follows:

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

Execution 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

OpenJDK9 Dockerfile mirror constructed as follows:

Listing: chapter17 / dockerfiles / java9 / Dockerfile
***

FROM registry.cn-shanghai.aliyuncs.com/weishiyao/alpine-3.10:glibc-2.30-r0
MAINTAINER [email protected]

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

The author used herein jre OpenJDK mirror image Tsinghua station installation.

Again, this author has uploaded a mirror image of Ali cloud storage, you can use the following command to pull directly:

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

Verify the following command:

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

Execution 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

OpenJDK10 Dockerfile mirror constructed as follows:

Listing: 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

Again, this author has uploaded a mirror image of Ali cloud storage, you can use the following command to pull directly:

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

Verify the following command:

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

Execution 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

OpenJDK11 Dockerfile mirror constructed as follows:

Listing: chapter17 / dockerfiles / java11 / Dockerfile
***

FROM registry.cn-shanghai.aliyuncs.com/weishiyao/alpine-3.10:glibc-2.30-r0
MAINTAINER [email protected]
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

Again, this author has uploaded a mirror image of Ali cloud storage, you can use the following command to pull directly:

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

Verify the following command:

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

Execution 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. The example code

Sample Code -Github

Sample Code -Gitee

Guess you like

Origin www.cnblogs.com/babycomeon/p/11570393.html