docker教程04 (镜像的制作和分享)

我们要制作一个docke镜像并进行分享,这个镜像的功能很简单,用python打印hello world就行

1制作镜像

在上一篇博客已经写了如何创建一个容器,并且已经创建好了,我们用命令查看一下创建好的那个centos容器

[root@dk ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                  PORTS               NAMES
755195af59c2        centos:centos8      "/bin/bash"         2 days ago          Up 2 days                                   nostalgic_rosalind
74b1c2b7647f        hello-world         "/hello"            3 days ago          Exited (0) 3 days ago                       modest_ganguly

我们看到创建好的容器的id是755195af59c2,然后我们进入容器

[root@dk ~]# docker attach 755195af59c2 

然后我们在容器安装一下python3

yum -y install python36

接下来写一个python输出你好啊世界

[root@755195af59c2 /]# vi aa.py
print("hello world")

退出到宿主机,制作镜像

[root@dk ~]# docker commit -p 755195af59c2
sha256:d47be7ed448556c41b232ccb191dfab851ab121b0401148af2fdfc5a9a8d41af
[root@dk~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              d47be7ed4485        29 seconds ago      254MB
centos              centos8             831691599b88        5 weeks ago         215MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
[root@dk~]# docker tag d47be7ed4485 centos/hello:v0.1 
[root@dk~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos/hello        v0.1                d47be7ed4485        2 minutes ago       254MB
centos              centos8             831691599b88        5 weeks ago         215MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB

这个centos/hello就是我们制作的镜像

2通过打包分享镜像

我们刚刚通过命令已经看到镜像的id是d47be7ed4485,然后我们用命令打个压缩包

[root@dk ~]# docker save -o centosHello.gz d47be7ed4485
[root@dk ~]# ls
centosHello.gz

接下来我们把这个压缩文件发送到另外一台机器

[root@dk002 ~]# docker load -i centosHello.gz 
Loaded image ID: sha256:d47be7ed448556c41b232ccb191dfab851ab121b0401148af2fdfc5a9a8d41af

这样就在另外一台机器上也加载了centosHello的镜像了

3通过上传到仓库分享镜像

国外的太慢了,我们使用阿里云的镜像仓库镜像分享,首先去阿里云
https://cr.console.aliyun.com/cn-shanghai/instances/repositories
docker教程04 (镜像的制作和分享)_第1张图片
然后docker宿主机上登录

[root@dk ~]# docker login --username=你的账号 registry.cn-shenzhen.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

修改一下标签

[root@dk ~]# docker tag d47be7ed4485 registry.cn-shenzhen.aliyuncs.com/lief/rep/centos:v0.1 

然后推送

[root@dk ~]# docker push registry.cn-shenzhen.aliyuncs.com/lief/rep/centos:v0.1
The push refers to repository [registry.cn-shenzhen.aliyuncs.com/lief/rep/centos]
07b7e8b0efa2: Pushed 
eb29745b8228: Pushed 
v0.1: digest: sha256:381e3003813c3ce3a428ae23896a3e263a7fa2f546ae891be7440fb16b6c47fa size: 741

然后我们在另外一台机器上即可下载改镜像了

C:\Users\Administrator>docker pull registry.cn-shenzhen.aliyuncs.com/lief/rep/centos:v0.1
v0.1: Pulling from lief/rep/centos
6910e5a164f7: Pull complete
0acc0431e429: Pull complete
Digest: sha256:381e3003813c3ce3a428ae23896a3e263a7fa2f546ae891be7440fb16b6c47fa
Status: Downloaded newer image for registry.cn-shenzhen.aliyuncs.com/lief/rep/centos:v0.1
registry.cn-shenzhen.aliyuncs.com/lief/rep/centos:v0.1

C:\Users\Administrator>docker images
REPOSITORY                                          TAG                 IMAGE ID            CREATED             SIZE
registry.cn-shenzhen.aliyuncs.com/lief/rep/centos   v0.1                d47be7ed4485        About an hour ago   254MB

最后我们用这个镜像创建一个容器,并打印刚刚我们写好的python代码

C:\Users\Administrator>docker run -it registry.cn-shenzhen.aliyuncs.com/lief/rep/centos:v0.1
[root@cea2108b83e9 /]# python3 aa.py
hello world

你可能感兴趣的:(docker,docker)