docker推送本地镜像到hub.docker.com

avatar 2020年6月22日18:08:18 评论 2,006 次浏览

首先,我们要理解hub.docker.com是做什么的,我们可以用来做什么?hub.docker.com可以理解为github.com一样,都是存储使用,只是hub.docker.com存储的是镜像,github存储的是代码。我们可以把自己定义的镜像存储到hub.docker.com上,在使用的时候方便直接拉取。下面看我的操作实例:

wolf:images wulaoer$ cat dockerfile 
FROM centos:7.2.1511
RUN echo "Asia/Shanghai" > /etc/timezone;
RUN mkdir /usr/local/java
ADD jdk-8u201-linux-x64.tar.gz /usr/local/java/
RUN ln -s /usr/local/java/jdk1.8.0_201 /usr/local/java/jdk
ENV JAVA_HOME=/usr/local/java/jdk
ENV JRE_HOME=${JAVA_HOME}/jre
ENV CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH=${JAVA_HOME}/bin:$PATH

这是我写了一个jdk的dockerfile,制作一个jdk的镜像,然后把这个jdk的镜像推送到我的hub.docker.com上。

wolf:images wulaoer$ docker build -t centos7:jdk1.8 .

制作了一个镜像,这里会拉取了一个centos:7.2.1511镜像,然后在镜像中把本地的jdk文件copy到镜像中,并设置环境变量,生成一个新的镜像centos7:jdk1.8,这里可以利用镜像创建一个容器看一下是否有jdk。

wolf:images wulaoer$ docker images
REPOSITORY                      TAG                                              IMAGE ID            CREATED             SIZE
centos7                         jdk1.8                                           f3f55dc6f730        7 seconds ago       591MB
docker/desktop-kubernetes       kubernetes-v1.16.5-cni-v0.7.5-critools-v1.15.0   a86647f0b376        5 months ago        279MB
docker/kube-compose-installer   v0.4.25-alpha1                                   2a71ac5a1359        7 months ago        42.3MB
centos                          7.2.1511                                         9aec5c5fe4ba        15 months ago       195MB
wolf:images wulaoer$ docker run -it --rm centos7:jdk1.8 /bin/bash
[root@14e41b1fbe37 /]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

新的镜像中已经有jdk,下面我们尝试把新的镜像推送的hub.docker.com上去。先是登陆到hub.docker.com上,然后在做推送处理。

wolf:images wulaoer$ docker login
Authenticating with existing credentials...
Login Succeeded
wolf:images wulaoer$ docker tag centos7:jdk1.8 wolf27w/centos7:jdk1.8
wolf:images wulaoer$ docker push wolf27w/centos7:jdk1.8
The push refers to repository [docker.io/wolf27w/centos7]
c6fdd6b6a297: Pushed 
36c13063ed86: Pushed 
f27da8ed9aa0: Pushed 
54aa21255121: Pushed 
a11c91bfd866: Mounted from library/centos 
jdk1.8: digest: sha256:6ee7cf19b6635135952652335692df028274b94c6a65f71406685ac28cc7934d size: 1363

推送成功,我们可以在hub.docker.com上查看一下,是否推送上去了,然后把本地的镜像删除,拉取一下hub.docker.com上的镜像,在创建一个容器,看看容器中jdk的版本。

wolf:images wulaoer$ docker pull wolf27w/centos7:jdk1.8
jdk1.8: Pulling from wolf27w/centos7
a8c7037c15e9: Pull complete 
85cf5d4c9b03: Pull complete 
cf64ac71ba65: Pull complete 
64f1355736c7: Pull complete 
c6cd42b411c6: Pull complete 
Digest: sha256:6ee7cf19b6635135952652335692df028274b94c6a65f71406685ac28cc7934d
Status: Downloaded newer image for wolf27w/centos7:jdk1.8
docker.io/wolf27w/centos7:jdk1.8
wolf:images wulaoer$ docker images
REPOSITORY                      TAG                                              IMAGE ID            CREATED             SIZE
wolf27w/centos7                 jdk1.8                                           2bb9bcef2bd4        36 minutes ago      591MB
docker/desktop-kubernetes       kubernetes-v1.16.5-cni-v0.7.5-critools-v1.15.0   a86647f0b376        5 months ago        279MB
docker/kube-compose-installer   v0.4.25-alpha1                                   2a71ac5a1359        7 months ago        42.3MB
wolf:images wulaoer$ docker run -it --rm wolf27w/centos7:jdk1.8 /bin/bash
[root@5bd48193fd17 /]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

测试完成,拉取的镜像可以在本地创建一个容器,容器中已经有jdk,是我们制作镜像中的版本。后期我们可以把自己相关的镜像推送到hub.docker.com上了,不需要在重新制作,记得推送的镜像不要有公司内部资料,防止资料泄密。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: