Dockerfile构建基础镜像为alpine的jdk1.8镜像

一、拉取alpine镜像

新建一个my-dockerfile目录,存放dockerfile文件

mkdir /home/my_dockerfile/

拉取alpine镜像

[root@localhost my_dockerfile]# docker pull alpine

查看

[root@localhost my_dockerfile]# docker images|grep alpine
postgres                                                                                                                                9.5-alpine          2bd1a8c91904        24 hours ago        35.9MB
alpine                                                                                                                                  latest              389fef711851        2 weeks ago         5.58MB

大小为5.58M

二、编写dockerfile-alpine-jdk1.8文件

[root@localhost my_dockerfile]# vim dockerfile-alpine-jdk18
FROM alpine:latest
MAINTAINER skycheng

#更换aline源
RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/community" > /etc/apk/repositories
RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/main" >> /etc/apk/repositories
#update apk
RUN apk update && apk upgrade
RUN apk --no-cache add ca-certificates
# bash vim wget curl net-tools
RUN apk add bash bash-doc bash-completion
RUN apk add vim wget curl net-tools
RUN rm -rf /var/cache/apk/*
RUN /bin/bash
#setup glibc
RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-2.32-r0.apk
RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-bin-2.32-r0.apk
RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-i18n-2.32-r0.apk
RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-dev-2.32-r0.apk
RUN wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub RUN apk add glibc-2.32-r0.apk
RUN apk add glibc-bin-2.32-r0.apk
RUN apk add glibc-dev-2.32-r0.apk
RUN apk add glibc-i18n-2.32-r0.apk
RUN rm -rf *.apk
#setup date
RUN apk add tzdata
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#setup language 解决中文乱码
RUN /usr/glibc-compat/bin/localedef -i en_US -f UTF-8 en_US.UTF-8
ENV LANG=en_US.UTF-8
#copy jdk-8u251-linux-x64.tar.gz ADD jdk-8u251-linux-x64.tar.gz /usr/local #setup java env ENV JAVA_HOME=/usr/local/jdk1.8.0_251 ENV PATH=$PATH:.:$JAVA_HOME/bin ENV CALSSPATH=$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

三、构建镜像

[root@localhost my_dockerfile]# docker build -f dockerfile-alpine-jdk18 -t skychengp/alpine-jdk:1.0.0 .
Sending build context to Docker daemon  195.1MB
Step 1/17 : FROM alpine:latest
 ---> 389fef711851
Step 2/17 : MAINTAINER skycheng
 ---> Using cache
 ---> 33c3bb8277ce
Step 3/17 : RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/community" > /etc/apk/repositories
 ---> Using cache
 ---> 5f6fb94f4115
Step 4/17 : RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/main" >> /etc/apk/repositories
 ---> Using cache
 ---> e747aed7e84c
Step 5/17 : RUN apk update && apk upgrade
 ---> Using cache
 ---> 01682f654770
Step 6/17 : RUN apk add --no-cache ca-certificates
 ---> Using cache
 ---> 013b3af31e6b
Step 7/17 : RUN apk add bash bash-doc bash-completion
 ---> Running in c8b34c5aa282
(1/7) Installing ncurses-terminfo-base (6.2_p20200523-r0)
(2/7) Installing ncurses-libs (6.2_p20200523-r0)
(3/7) Installing readline (8.0.4-r0)
(4/7) Installing bash (5.0.17-r0)
Executing bash-5.0.17-r0.post-install
(5/7) Installing pkgconf (1.7.2-r0)
(6/7) Installing bash-completion (2.10-r0)
(7/7) Installing bash-doc (5.0.17-r0)
Executing busybox-1.31.1-r19.trigger
OK: 13 MiB in 22 packages
Removing intermediate container c8b34c5aa282
 ---> b831c9eef890
Step 8/17 : RUN apk add vim wget curl net-tools
 ---> Running in 956d62823559
(1/11) Installing nghttp2-libs (1.41.0-r0)
(2/11) Installing libcurl (7.69.1-r3)
(3/11) Installing curl (7.69.1-r3)
(4/11) Installing mii-tool (1.60_git20140218-r2)
(5/11) Installing net-tools (1.60_git20140218-r2)
(6/11) Installing xxd (8.2.0735-r0)
(7/11) Installing lua5.3-libs (5.3.5-r6)
(8/11) Installing vim (8.2.0735-r0)
(9/11) Installing libunistring (0.9.10-r0)
(10/11) Installing libidn2 (2.3.0-r0)
(11/11) Installing wget (1.20.3-r1)
Executing busybox-1.31.1-r19.trigger
OK: 45 MiB in 33 packages
Removing intermediate container 956d62823559
 ---> 7c81d10ece00
Step 9/17 : RUN rm -rf /var/cache/apk/*
 ---> Running in ddd227a9a68c
Removing intermediate container ddd227a9a68c
 ---> c6387f003afb
Step 10/17 : RUN /bin/bash
 ---> Running in 5089e7ec4e11
Removing intermediate container 5089e7ec4e11
 ---> ad94121b440d
Step 11/17 : RUN wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub
 ---> Running in 9c927718a359
Removing intermediate container 9c927718a359
 ---> 8fc32c014bd5
Step 12/17 : RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-2.32-r0.apk
 ---> Running in f44896188614
--2020-12-31 02:50:58--  https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-2.32-r0.apk
Resolving github.com (github.com)... 13.229.188.59
Connecting to github.com (github.com)|13.229.188.59|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/33333969/d4cfa880-d89f-11ea-9b3d-e2a2cba4ad38?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201231%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201231T025059Z&X-Amz-Expires=300&X-Amz-Signature=9cebe5e17d9dfdc37d5d6f7013599681bd7fd0099083da5af65d4ff3aa0468e7&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=33333969&response-content-disposition=attachment%3B%20filename%3Dglibc-2.32-r0.apk&response-content-type=application%2Fvnd.android.package-archive [following]
--2020-12-31 02:51:00--  https://github-production-release-asset-2e65be.s3.amazonaws.com/33333969/d4cfa880-d89f-11ea-9b3d-e2a2cba4ad38?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201231%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201231T025059Z&X-Amz-Expires=300&X-Amz-Signature=9cebe5e17d9dfdc37d5d6f7013599681bd7fd0099083da5af65d4ff3aa0468e7&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=33333969&response-content-disposition=attachment%3B%20filename%3Dglibc-2.32-r0.apk&response-content-type=application%2Fvnd.android.package-archive
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.217.102.244
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.217.102.244|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4435333 (4.2M) [application/vnd.android.package-archive]
Saving to: 'glibc-2.32-r0.apk'

     0K .......... .......... .......... .......... ..........  1% 78.4K 55s
    50K .......... .......... .......... .......... ..........  2% 78.5K 54s
   100K .......... .......... .......... .......... ..........  3% 78.0K 53s
   150K .......... .......... .......... .......... ..........  4% 78.5K 53s
   200K .......... .......... .......... .......... ..........  5% 78.5K 52s
   250K .......... .......... .......... .......... ..........  6%  107K 49s
   300K .......... .......... .......... .......... ..........  8% 83.7K 48s
   350K .......... .......... .......... .......... ..........  9%  117K 46s
   400K .......... .......... .......... .......... .......... 10%  118K 44s
   450K .......... .......... .......... .......... .......... 11%  118K 42s
   500K .......... .......... .......... .......... .......... 12%  234K 39s
   550K .......... .......... .......... .......... .......... 13%  235K 37s
   600K .......... .......... .......... .......... .......... 15%  197K 35s
   650K .......... .......... .......... .......... .......... 16%  283K 33s
   700K .......... .......... .......... .......... .......... 17%  118K 33s
   750K .......... .......... .......... .......... .......... 18%  234K 31s
   800K .......... .......... .......... .......... .......... 19%  235K 30s
   850K .......... .......... .......... .......... .......... 20%  236K 28s
   900K .......... .......... .......... .......... .......... 21%  234K 27s
   950K .......... .......... .......... .......... .......... 23% 11.3M 26s
  1000K .......... .......... .......... .......... .......... 24%  235K 25s
  1050K .......... .......... .......... .......... .......... 25%  118K 24s
  1100K .......... .......... .......... .......... .......... 26%  231K 24s
  1150K .......... .......... .......... .......... .......... 27%  233K 23s
  1200K .......... .......... .......... .......... .......... 28%  228K 22s
  1250K .......... .......... .......... .......... .......... 30%  118K 22s
  1300K .......... .......... .......... .......... .......... 31%  117K 22s
  1350K .......... .......... .......... .......... .......... 32%  118K 21s
  1400K .......... .......... .......... .......... .......... 33%  118K 21s
  1450K .......... .......... .......... .......... .......... 34%  118K 21s
  1500K .......... .......... .......... .......... .......... 35%  118K 21s
  1550K .......... .......... .......... .......... .......... 36%  117K 20s
  1600K .......... .......... .......... .......... .......... 38%  117K 20s
  1650K .......... .......... .......... .......... .......... 39%  198K 20s
  1700K .......... .......... .......... .......... .......... 40%  130K 19s
  1750K .......... .......... .......... .......... .......... 41%  233K 19s
  1800K .......... .......... .......... .......... .......... 42%  117K 18s
  1850K .......... .......... .......... .......... .......... 43%  199K 18s
  1900K .......... .......... .......... .......... .......... 45% 83.2K 18s
  1950K .......... .......... .......... .......... .......... 46% 78.2K 18s
  2000K .......... .......... .......... .......... .......... 47% 67.7K 18s
  2050K .......... .......... .......... .......... .......... 48% 69.4K 18s
  2100K .......... .......... .......... .......... .......... 49% 74.2K 18s
  2150K .......... .......... .......... .......... .......... 50% 91.9K 17s
  2200K .......... .......... .......... .......... .......... 51% 96.5K 17s
  2250K .......... .......... .......... .......... .......... 53%  117K 17s
  2300K .......... .......... .......... .......... .......... 54%  118K 16s
  2350K .......... .......... .......... .......... .......... 55%  118K 16s
  2400K .......... .......... .......... .......... .......... 56%  234K 15s
  2450K .......... .......... .......... .......... .......... 57%  235K 15s
  2500K .......... .......... .......... .......... .......... 58%  234K 14s
  2550K .......... .......... .......... .......... .......... 60%  236K 14s
  2600K .......... .......... .......... .......... .......... 61%  250K 13s
  2650K .......... .......... .......... .......... .......... 62%  221K 13s
  2700K .......... .......... .......... .......... .......... 63% 1.17M 12s
  2750K .......... .......... .......... .......... .......... 64%  230K 12s
  2800K .......... .......... .......... .......... .......... 65%  132K 11s
  2850K .......... .......... .......... .......... .......... 66%  236K 11s
  2900K .......... .......... .......... .......... .......... 68%  236K 10s
  2950K .......... .......... .......... .......... .......... 69%  236K 10s
  3000K .......... .......... .......... .......... .......... 70%  234K 9s
  3050K .......... .......... .......... .......... .......... 71%  236K 9s
  3100K .......... .......... .......... .......... .......... 72%  236K 9s
  3150K .......... .......... .......... .......... .......... 73% 1.14M 8s
  3200K .......... .......... .......... .......... .......... 75%  235K 8s
  3250K .......... .......... .......... .......... .......... 76%  286K 7s
  3300K .......... .......... .......... .......... .......... 77%  236K 7s
  3350K .......... .......... .......... .......... .......... 78%  236K 6s
  3400K .......... .......... .......... .......... .......... 79%  237K 6s
  3450K .......... .......... .......... .......... .......... 80%  236K 6s
  3500K .......... .......... .......... .......... .......... 81%  236K 5s
  3550K .......... .......... .......... .......... .......... 83%  235K 5s
  3600K .......... .......... .......... .......... .......... 84%  232K 5s
  3650K .......... .......... .......... .......... .......... 85%  236K 4s
  3700K .......... .......... .......... .......... .......... 86%  236K 4s
  3750K .......... .......... .......... .......... .......... 87%  236K 4s
  3800K .......... .......... .......... .......... .......... 88%  236K 3s
  3850K .......... .......... .......... .......... .......... 90%  236K 3s
  3900K .......... .......... .......... .......... .......... 91%  236K 3s
  3950K .......... .......... .......... .......... .......... 92%  236K 2s
  4000K .......... .......... .......... .......... .......... 93%  234K 2s
  4050K .......... .......... .......... .......... .......... 94%  236K 2s
  4100K .......... .......... .......... .......... .......... 95%  402K 1s
  4150K .......... .......... .......... .......... .......... 96%  542K 1s
  4200K .......... .......... .......... .......... .......... 98%  236K 1s
  4250K .......... .......... .......... .......... .......... 99%  238K 0s
  4300K .......... .......... .......... .                    100% 12.3M=27s

2020-12-31 02:51:28 (159 KB/s) - 'glibc-2.32-r0.apk' saved [4435333/4435333]

Removing intermediate container f44896188614
 ---> ab3d8e6250ed
Step 13/17 : RUN apk add glibc-2.32-r0.apk
 ---> Running in f798f543f90d
fetch http://mirrors.aliyun.com/alpine/latest-stable/community/x86_64/APKINDEX.tar.gz
fetch http://mirrors.aliyun.com/alpine/latest-stable/main/x86_64/APKINDEX.tar.gz
(1/1) Installing glibc (2.32-r0)
OK: 54 MiB in 34 packages
Removing intermediate container f798f543f90d
 ---> 8afe0d6a0ecd
Step 14/17 : ADD jdk-8u251-linux-x64.tar.gz /usr/local
 ---> 6c6841282db2
Step 15/17 : ENV JAVA_HOME=/usr/local/jdk1.8.0_251
 ---> Running in a0cc638a3cf4
Removing intermediate container a0cc638a3cf4
 ---> 8a83d8bc9ef4
Step 16/17 : ENV PATH=$PATH:.:$JAVA_HOME/bin
 ---> Running in d554dba9e4d3
Removing intermediate container d554dba9e4d3
 ---> 7b394a95322d
Step 17/17 : ENV CALSSPATH=$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in 3956f4e0c630
Removing intermediate container 3956f4e0c630
 ---> 7c53314d155b
Successfully built 7c53314d155b
Successfully tagged skychengp/alpine-jdk:1.0.0

查看镜像

[root@localhost my_dockerfile]# docker images|grep alpine
skychengp/alpine-jdk                                                                                                                    1.0.0               7c53314d155b        3 minutes ago       465MB

四、运行容器,验证

[root@localhost blockchain-explorer]# docker run -it --name testjdk skychengp/alpine-jdk:1.0.0  /bin/bash
bash-5.0# pwd
/
bash-5.0# java -version
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
bash-5.0# 

五、将镜像推送到镜像仓库

[root@localhost blockchain-explorer]# docker push skychengp/alpine-jdk:1.0.0
The push refers to repository [docker.io/skychengp/alpine-jdk]
8094c5351a97: Pushed 
d3a5055b77c1: Pushed 
a6a99ae24993: Pushed 
e976ef7b42a1: Pushed 
3bc71adf2056: Pushed 
50ed3fc63216: Pushed 
f3e4869d2583: Pushed 
48ec23f5667d: Pushed 
89f19e95bd82: Pushed 
a2cb35a6f401: Pushed 
20f6cb8aed73: Pushed 
777b2c648970: Mounted from library/alpine 
1.0.0: digest: sha256:f6605af021cfc7fa8fd625e6d2943b98449413a38653ff9c49081dd1ad83bbc6 size: 2835
[root@localhost blockchain-explorer]# 

六、查看

 七、删除本地镜像,并拉取远端镜像

[root@localhost blockchain-explorer]# docker rmi -f skychengp/alpine-jdk:1.0.0
[root@localhost blockchain-explorer]# docker pull skychengp/alpine-jdk:1.0.0
1.0.0: Pulling from skychengp/alpine-jdk
801bfaa63ef2: Already exists 
bae3c8b049a5: Pull complete 
74bbf6ad6a3e: Pull complete 
93afac560ff5: Pull complete 
4d8c671ff03c: Pull complete 
adec1a2368bf: Pull complete 
90fbea72c149: Pull complete 
66a9f1f1f39b: Pull complete 
fb928bf66f8a: Pull complete 
8befd4a4a61b: Pull complete 
a9d8b0e8a8ce: Pull complete 
9bdd38312325: Pull complete 
Digest: sha256:f6605af021cfc7fa8fd625e6d2943b98449413a38653ff9c49081dd1ad83bbc6
Status: Downloaded newer image for skychengp/alpine-jdk:1.0.0
docker.io/skychengp/alpine-jdk:1.0.0

八、运行容器

[root@localhost blockchain-explorer]# docker run -it --name testjdk skychengp/alpine-jdk:1.0.0 /bin/bash
bash-5.0# java -version
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
bash-5.0# 
原文地址:https://www.cnblogs.com/sky-cheng/p/14215332.html