alpine of Docker customizes alicloud image

Keywords: Docker JDK glibc Linux

Article directory

Combined with the previous article Dockerfile making image blog For example, jdk8
The v1.0 version is 610 MB, which is very large and takes up too much space for deployment projects. After learning apline, reduce the weight of jdk (about 200MB) without affecting the function

Introduction to alpine Linux

  1. Alpine Linux is a lightweight Linux distribution, which is different from the usual Linux distribution (centos, Ubuntu). Alpine uses musl libc
    And BusyBox to reduce the size of the system and resource consumption at runtime.
  2. Alpine Linux provides its own package management tool: APK (Note: apt get in ubuntu). We can use https://pkgs.alpinelinux.org/packages
    The Alpine Docker image inherits these advantages of the Alpine Linux distribution, compared with other Linux
    Docker image, which is very small
  3. Compare common, uncompressed base images (see current: latest tab): alpine - 4.8mb CentOS - 124.8mb
    Debian - 125.1MB Centos - 196MB
  4. It is recommended to use Alpine Linux version 3.10.0, which is also the first version of the v3.10 stable series, alpine:3.10

Making JDK8 image based on alpine

1. Making jdk image by alpine

  1. Download mirroring

docker pull alpine:3.10

2. Create and edit dockerfile

Delete the original Dockerfile and re create the write content

cd /javawxm_docker/jdk/
rm -rf Dockerfile

touch Dockerfile 
vi Dockerfile

Note: the javawxm · docker in the Dockerfile must be the same as the folder created before, otherwise an error will be reported

#1. Specify the basic image, and it must be the first instruction
#FROM alpine:latest
FROM alpine:3.10

#2. Indicate the author of the image and its email
MAINTAINER wxm"wxm@qq.com"

#3. When building an image, specify the working directory of the image. Subsequent commands are based on the working directory. If it does not exist, the directory will be created
WORKDIR /javawxm_docker/jdk

#4. Copy some installation packages to the image. Syntax: Add / copy < SRC >... < dest >
## Difference between ADD and COPY: ADD is copied and decompressed, and COPY is copied only
ADD jdk-8u221-linux-x64.tar.gz /javawxm_docker/jdk/
## If the glibc installation package is too slow to download from the network, download and copy it to the image in advance
COPY glibc-2.29-r0.apk /javawxm_docker/jdk/
COPY glibc-bin-2.29-r0.apk /javawxm_docker/jdk/
COPY glibc-i18n-2.29-r0.apk /javawxm_docker/jdk/

#5. Alicloud is the software source for updating Alpine, because it is too slow to pull from the default official source
RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
    echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
RUN apk update && apk upgrade

#6. Run the specified command
## In order to simplify itself, Alpine linux does not install too many common software. apk is similar to ubuntu's apt get,
## It is used to install some commonly used software V parts. The syntax is as follows: apk add bash wget curl git make vim docker
## wget is an ftp/http transport tool under linux. If it is not installed, an error will be reported "/ bin/sh: wget: not found". There are few online examples to install wget
## CA certificates certificate service is a pre dependency for glibc installation
RUN apk --no-cache add ca-certificates wget \
    && wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
    && apk add glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk \
    && rm -rf /var/cache/apk/* glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk

#7. Configure environment variables
ENV JAVA_HOME=/javawxm_docker/jdk/jdk1.8.0_221
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$JAVA_HOME/bin:$PATH

#Commands to execute when container starts
#CMD ["java","-version"]


3. Execute dockerfile to create image

docker build -t jdk8:v2.0 .

Report errors: Refer to error response from day: dockerfile parse error line 1: unknown instruction


In the first step, if the glic installation package is too slow to download from the network or reports an error, it should be downloaded and copied to the image in advance
Select three files to upload

rz



After successful i

Obviously v2.0 is much slimmer than v1.0

4. Create and start the container

docker create -it jdk8:v2.0
docker start container ID

5. Enter the container

docker exec -it container ID /bin/sh

Note 1: last sh not bash
It can also be directly started and entered into the container
docker run -it --name myjdk container ID

6. test jdk

java -version

Note 1: docker exec -it container ID /bin/sh uses the current account (root) to log in by default. You can view the current user name through whoami command,
You can also switch to other account docker exec - it -- user root < container ID > / bin / sh by the following command
Note 2: openjdk: the image of 8-jdk-alpine is the same as the image we created by ourselves, except that JDK is too thin and smaller
You can come down and create a container for yourself. Try note 3: the image size after the final production is about 400M

2. Alpine makes jre image (slimming)

You can also reduce common sense twice: when Java applications are running, they rely on class files and do not need java files jdk: to compile Java
. java source file + class file of running Java jre: class file of running Java
Linux system is mainly used to run class files and deploy projects; therefore, it is unnecessary to compile Java source files

Minimum JRE basic image of Docker container

1. Download jre first

The download address is https://www.java.com/en/download/manual.jsp, about 77M
The final download is: jre-8u221-linux-x64.tar.gz
2.rz upload to centos, delete useless files and recompress

Decompression and weight reduction

tar -zxvf jre-8u221-linux-x64.tar.gz

After decompression, delete the original compression package (it depends on whether you want to delete it or not. It's useless anyway)

rm -rf jre-8u221-linux-x64.tar.gz

View jre size (230m before slimming)

du -sh jre1.8.0_221

Enter the jre directory and execute the slimming command

#Delete text file
rm -rf COPYRIGHT LICENSE README release THIRDPARTYLICENSEREADME-JAVAFX.txtTHIRDPARTYLICENSEREADME.txt Welcome.html \
#Delete other useless files
rm -rf lib/plugin.jar \
lib/ext/jfxrt.jar \
bin/javaws \
lib/javaws.jar \
lib/desktop \
plugin \
lib/deploy* \
lib/*javafx* \
lib/*jfx* \
lib/amd64/libdecora_sse.so \
lib/amd64/libprism_*.so \
lib/amd64/libfxplugins.so \
lib/amd64/libglass.so \
lib/amd64/libgstreamer-lite.so \
lib/amd64/libjavafx*.so \
lib/amd64/libjfx*.so


111M after slimming
Return to the parent directory and repack jre

cd .../
tar -cvf jre1.8.0_221.tar.gz jre1.8.0_221

Compression is xvf, decompression is cvf or zcvf

Delete the original 111M jre and Doerfile

rm -rf jre1.8.0_221
rm -rf Dockerfile

3. Create and edit dockerfile

touch Dockerfile
vi Dockerfile

#1.Specifies the underlying image and must be the first instruction
#FROM alpine:latest
FROM alpine:3.10

#2.Indicates the author of the image and its email
MAINTAINER wxm"wxm@qq.com"

#3.When building an image, specify the working directory of the image. The subsequent commands are based on the working directory. If it does not exist, the directory will be created
WORKDIR /javawxm_docker/jdk

#4.Copy some installation packages to the image. Syntax: ADD/COPY <src>... <dest>
## Difference between ADD and COPY: ADD is copied and decompressed, and COPY is copied only
## Be careful~~~Uploaded after slimming jre
ADD jre1.8.0_221.tar.gz /javawxm_docker/jdk/
## If the glibc installation package is too slow to download from the network, download and copy it to the image in advance
COPY glibc-2.29-r0.apk /javawxm_docker/jdk/
COPY glibc-bin-2.29-r0.apk /javawxm_docker/jdk/
COPY glibc-i18n-2.29-r0.apk /javawxm_docker/jdk/

#5.To update Alpine Alibaba cloud is the software source of. Because it's too slow to pull from the default official source
RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
    echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
RUN apk update && apk upgrade

#6.Run the specified command
## Alpine linux does not install too many common software in order to simplify itself,apk Be similar to ubuntu Of apt-get,
## It is used to install some commonly used software V parts. The syntax is as follows: apk add bash wget curl git make vim docker
## wget is ftp under linux/http Error will be reported if the transmission tool is not installed“/bin/sh:   wget: not found",Less installation of online examples wget
## ca-certificates Certificate services, installation glibc Predependence
RUN apk --no-cache add ca-certificates wget \
    && wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
    && apk add glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk \
    && rm -rf /var/cache/apk/* glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk

#7. Configure environment variables
## Notice ~ ~ ~ no jdk, point to jre directly
ENV JAVA_HOME=/javawxm_docker/jdk/jre1.8.0_221
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$JAVA_HOME/bin:$PATH

#Commands to execute when container starts
#CMD ["java","-version"]

4. Execute dockerfile to create image

docker build -t jdk8:v3.0 .

Upload file rz command
Re construct

docker build -t jdk8:v3.0 .
Note: all three files need to be uploaded

Three files are uploaded and then built again
docker build -t jdk8:v3.0 .
Displayed successfully!

5. Create and start the container

docker create -it jdk8:v3.0
docker start container ID

The results of the three versions of jdk images are as follows:

V1.0: centos7+jdk1.8
V2.0: alpine3.10+jdk1.8
V3.0: alpine3.10+jre1.8 + slimming command

3. Docker image uploaded to alicloud

Upload Docker image to alicloud (or download image from alicloud)

preparation in advance

1. Register for alicloud account

Alicloud official website: https://dev.aliyun.com
Search "container image service" to enter the console

2. Login account

3. Configure Docker accelerator

4. create the namespace of the image warehouse

For example: javawxm

5. Create image warehouse (bind a code hosting website when creating image warehouse, for example: github)

For example: alpine ˊ JRE
Bind account
Select the private warehouse created in github in advance

Push image

There are operation steps after clicking in

Push image to Registry
1. Log in to the doker warehouse of Alibaba cloud, and - username is the user name of Alibaba cloud. In addition, the password is the password set when opening the image service. If you forget the password, you can click the menu: "container image service" - > "default instance" - > "access credentials" to modify it

docker login --username=zmt1309439917 registry.cn-hangzhou.aliyuncs.com

2. Add tag for local image

docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/javawxm/alpine_jre:[Image version number]


3. Push image (jdk8 Alpine: 1.0)

Docker push registry.cn-hangzhou.aliyuncs.com/javawxm/alpine [JRE: [image version No.]
Detect image version

Delete the local jdk again

Pull image

1. Log in to Alibaba cloud's doker warehouse

docker login --username=zmt1309439917 registry.cn-hangzhou.aliyuncs.com

Pull image from Registry

Docker pull registry.cn-hangzhou.aliyuncs.com/javawxm/alpine [JRE: [image version No.]

Published 107 original articles, won praise 11, visited 6362
Private letter follow

Posted by angrytuna on Fri, 21 Feb 2020 03:34:39 -0800