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

新建dockerfile目录

mkdir /opt/mydockerfile

拉取alpine镜像

docker pull alpine

编写dockerfile-alpine-jdk1.8文件

cd /opt/mydockerfile

vim dockerfile-alpine-jdk18
FROM alpine:latest
MAINTAINER xiaosige "leeze0216@163.com"

#更换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.34-r0.apk
#RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-bin-2.34-r0.apk
#RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-i18n-2.34-r0.apk
#RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-dev-2.34-r0.apk
# glibc安装包如果从网络下载速度实在是太慢了,先提前下载复制到镜像中
COPY glibc-2.34-r0.apk .
COPY glibc-bin-2.34-r0.apk .
COPY glibc-dev-2.34-r0.apk .
COPY glibc-i18n-2.34-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.34-r0.apk
RUN apk add glibc-bin-2.34-r0.apk
RUN apk add glibc-dev-2.34-r0.apk
RUN apk add glibc-i18n-2.34-r0.apk
RUN rm -rf *.apk

#setup date
RUN apk add tzdata
RUN 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

# 添加alias ll='ls -al'
ADD ./ll /bin/ll
RUN chmod 777 /bin/ll

#copy jdk-8u311-linux-x64.tar.gz
ADD jdk-8u311-linux-x64.tar.gz /usr/local

#setup java env
ENV JAVA_HOME=/usr/local/jdk1.8.0_311
ENV PATH=$PATH:.:$JAVA_HOME/bin
ENV CALSSPATH=$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

构建镜像

docker build -f dockerfile-alpine-jdk18 -t xiaosige/alpine-jdk:1.0.0 .
[root@xiaosige mydockerfile]# docker build -f dockerfile-alpine-jdk18 -t xiaosige/alpine-jdk:1.0.0 .
Sending build context to Docker daemon  173.6MB
Step 1/30 : FROM alpine:latest
 ---> 14119a10abf4
Step 2/30 : MAINTAINER xiaosige "leeze0216@163.com"
 ---> Running in 1ca7e3fe8cd4
Removing intermediate container 1ca7e3fe8cd4
 ---> a12b929884c6
Step 3/30 : RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/community" > /etc/apk/repositories
 ---> Running in d30141b40989
Removing intermediate container d30141b40989
 ---> bdd0194242df
Step 4/30 : RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/main" >> /etc/apk/repositories
 ---> Running in fdef39d291da
Removing intermediate container fdef39d291da
 ---> 9b05d8656e9f
Step 5/30 : RUN apk update && apk upgrade
 ---> Running in f0fc168abe7a
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
v3.14.2-117-gc76a8e57ec [http://mirrors.aliyun.com/alpine/latest-stable/community]
v3.14.2-116-gea9b052f30 [http://mirrors.aliyun.com/alpine/latest-stable/main]
OK: 14943 distinct packages available
(1/1) Upgrading alpine-keys (2.3-r1 -> 2.4-r0)
OK: 6 MiB in 14 packages
Removing intermediate container f0fc168abe7a
 ---> e4ebb6b07c41
Step 6/30 : RUN apk --no-cache add ca-certificates
 ---> Running in 9d150bc9e523
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 ca-certificates (20191127-r5)
Executing busybox-1.33.1-r3.trigger
Executing ca-certificates-20191127-r5.trigger
OK: 6 MiB in 15 packages
Removing intermediate container 9d150bc9e523
 ---> 07bbec5ac046
Step 7/30 : RUN apk add bash bash-doc bash-completion
 ---> Running in 349dbe49e18d
(1/7) Installing ncurses-terminfo-base (6.2_p20210612-r0)
(2/7) Installing ncurses-libs (6.2_p20210612-r0)
(3/7) Installing readline (8.1.0-r0)
(4/7) Installing bash (5.1.4-r0)
Executing bash-5.1.4-r0.post-install
(5/7) Installing pkgconf (1.7.4-r0)
(6/7) Installing bash-completion (2.11-r4)
(7/7) Installing bash-doc (5.1.4-r0)
Executing busybox-1.33.1-r3.trigger
OK: 13 MiB in 22 packages
Removing intermediate container 349dbe49e18d
 ---> c7b45eb58ec3
Step 8/30 : RUN apk add vim wget curl net-tools
 ---> Running in 0aa7f4033d62
(1/12) Installing brotli-libs (1.0.9-r5)
(2/12) Installing nghttp2-libs (1.43.0-r0)
(3/12) Installing libcurl (7.79.1-r0)
(4/12) Installing curl (7.79.1-r0)
(5/12) Installing mii-tool (1.60_git20140218-r2)
(6/12) Installing net-tools (1.60_git20140218-r2)
(7/12) Installing xxd (8.2.3437-r0)
(8/12) Installing lua5.3-libs (5.3.6-r0)
(9/12) Installing vim (8.2.3437-r0)
(10/12) Installing libunistring (0.9.10-r1)
(11/12) Installing libidn2 (2.3.1-r0)
(12/12) Installing wget (1.21.1-r1)
Executing busybox-1.33.1-r3.trigger
OK: 37 MiB in 34 packages
Removing intermediate container 0aa7f4033d62
 ---> a8148d692752
Step 9/30 : RUN rm -rf /var/cache/apk/*
 ---> Running in 246b97afc896
Removing intermediate container 246b97afc896
 ---> 797ad812172c
Step 10/30 : RUN /bin/bash
 ---> Running in 1451436e6e2f
Removing intermediate container 1451436e6e2f
 ---> 174286182698
Step 11/30 : COPY glibc-2.34-r0.apk .
 ---> 652c53def449
Step 12/30 : COPY glibc-bin-2.34-r0.apk .
 ---> 160e773ea556
Step 13/30 : COPY glibc-dev-2.34-r0.apk .
 ---> 0ddaa22afa7b
Step 14/30 : COPY glibc-i18n-2.34-r0.apk .
 ---> e1251741ca54
Step 15/30 : RUN wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub
 ---> Running in a33efe825376
Removing intermediate container a33efe825376
 ---> d1cbf5b00ba7
Step 16/30 : RUN apk add glibc-2.34-r0.apk
 ---> Running in 5b9e9837c65a
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.34-r0)
OK: 41 MiB in 35 packages
Removing intermediate container 5b9e9837c65a
 ---> 0ff4b785adb0
Step 17/30 : RUN apk add glibc-bin-2.34-r0.apk
 ---> Running in b65af13f569b
(1/2) Installing libgcc (10.3.1_git20210424-r2)
(2/2) Installing glibc-bin (2.34-r0)
Executing glibc-bin-2.34-r0.trigger
OK: 44 MiB in 37 packages
Removing intermediate container b65af13f569b
 ---> 2a553cf15df0
Step 18/30 : RUN apk add glibc-dev-2.34-r0.apk
 ---> Running in d40efc59cc3e
(1/1) Installing glibc-dev (2.34-r0)
Executing glibc-bin-2.34-r0.trigger
OK: 102 MiB in 38 packages
Removing intermediate container d40efc59cc3e
 ---> 853e57ef2aeb
Step 19/30 : RUN apk add glibc-i18n-2.34-r0.apk
 ---> Running in 1007b18bf054
(1/1) Installing glibc-i18n (2.34-r0)
OK: 128 MiB in 39 packages
Removing intermediate container 1007b18bf054
 ---> 2f11ef6e2a3e
Step 20/30 : RUN rm -rf *.apk
 ---> Running in a55b3deb9cfa
Removing intermediate container a55b3deb9cfa
 ---> f4d875a9bde7
Step 21/30 : RUN apk add tzdata
 ---> Running in 93f6978051d6
(1/1) Installing tzdata (2021e-r0)
Executing busybox-1.33.1-r3.trigger
OK: 131 MiB in 40 packages
Removing intermediate container 93f6978051d6
 ---> 6ce2d9ce1b20
Step 22/30 : RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
 ---> Running in bc3c55254938
Removing intermediate container bc3c55254938
 ---> 4d6ed2cd9790
Step 23/30 : RUN /usr/glibc-compat/bin/localedef -i en_US -f UTF-8 en_US.UTF-8
 ---> Running in 2be0c47a1bae
Removing intermediate container 2be0c47a1bae
 ---> 9cd2cd39dbbd
Step 24/30 : ENV LANG=en_US.UTF-8
 ---> Running in fba634ea0e25
Removing intermediate container fba634ea0e25
 ---> 8dbd23bae443
Step 25/30 : ADD ./ll /bin/ll
 ---> d523605d3c0d
Step 26/30 : RUN chmod 777 /bin/ll
 ---> Running in 1509523a3b4c
Removing intermediate container 1509523a3b4c
 ---> 687db62a12a2
Step 27/30 : ADD jdk-8u311-linux-x64.tar.gz /usr/local
 ---> 10f3a0525359
Step 28/30 : ENV JAVA_HOME=/usr/local/jdk1.8.0_311
 ---> Running in 3f12a00f6343
Removing intermediate container 3f12a00f6343
 ---> c20380dbec7f
Step 29/30 : ENV PATH=$PATH:.:$JAVA_HOME/bin
 ---> Running in 21fb10f3a937
Removing intermediate container 21fb10f3a937
 ---> a80795139991
Step 30/30 : ENV CALSSPATH=$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in 3aee4dfbe86c
Removing intermediate container 3aee4dfbe86c
 ---> 358c94e522b9
Successfully built 358c94e522b9
Successfully tagged xiaosige/alpine-jdk:1.0.0

查看镜像

[root@xiaosige mydockerfile]# docker images | grep xiaosige
xiaosige/alpine-jdk         1.0.0     358c94e522b9   20 minutes ago   526MB

运行容器,验证

docker run --name jdk18 -it  xiaosige/alpine-jdk:1.0.0 /bin/bash

docker run -it --rm xiaosige/maven:3.8-jdk-8
ash-5.1# java -version
java version "1.8.0_311"
Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)

将镜像推送到镜像仓库


docker tag xiaosige/alpine-jdk:1.0.0 10.211.55.81:8082/xiaosige/alpine-jdk:1.0.0

docker push 10.211.55.81:8082/xiaosige/alpine-jdk:1.0.0

查看

[root@xiaosige mydockerfile]# docker search 10.211.55.81:8082/xiaosige/alpine-jdk:1.0.0
NAME                                          DESCRIPTION   STARS     OFFICIAL   AUTOMATED
10.211.55.81:8082/xiaosige/alpine-jdk:1.0.0  

PS

实现alias ll='ls -al'

  1. 编写ll命令文件: ll
    #!/bin/sh
    set -e
    alias ll="ls -al"
    ll "$@" 
  2. Dockerifle文件关键内容
    ...省略...
    ADD ./ll /bin/ll

/opt/mydockerfile文件结构

-rw-r--r--. 1 root root      1898 Oct 31 12:35 dockerfile-alpine-jdk18
-rwxr-xr-x. 1 root root   1940882 Oct 31 11:53 glibc-2.34-r0.apk
-rwxr-xr-x. 1 root root   1120265 Oct 31 11:54 glibc-bin-2.34-r0.apk
-rwxr-xr-x. 1 root root  15940495 Oct 31 11:54 glibc-dev-2.34-r0.apk
-rwxr-xr-x. 1 root root   7766693 Oct 31 11:54 glibc-i18n-2.34-r0.apk
-rw-r--r--. 1 root root 146799982 Oct 31 11:34 jdk-8u311-linux-x64.tar.gz
-rw-r--r--. 1 root root        43 Oct 31 12:27 ll