#dodocker镜像管理基础
文章目录
##docker 镜像的制作
多数情况下,我们做镜像是基于别人已存在的某个基础镜像来实现的,我们把它称为 base image。比如一个纯净版的最小化的 centos、ubuntu 或 debian。
那么这个最小化的 centos 镜像从何而来呢?其实这个基础镜像一般是由 Docker Hub的相关维护人员,也就是 Docker 官方手动制作的。这个基础镜像的制作对于 Docker 官方的专业人员来说是非常容易的,但对于终端用户来说就不是那么容易制作的了
###Docker Hub
图像存储库
从社区和官方库中查找和拉取镜像,以及管理、推送和拉取您有权访问的私有镜像库。
自动化构建
更改源代码存储库时自动创建新图像。
网络挂钩
Webhook 是自动构建的一项功能,可让您在成功推送到存储库后触发操作。
组织
创建工作组以管理对映像存储库的访问。
GitHub 和 Bitbucket 集成
将 Hub 和 Docker 映像添加到您当前的工作流程中
###docker 镜像的获取
# docker pull <registry>[:<port>]/[<namespace>/]<name>:<tag>
[root@localhost ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
###镜像的推送
打标签
[root@localhost ~]# docker tag busybox:latest gbj1123/busybox:b1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest beae173ccac6 7 months ago 1.24MB
renweiwei/busybox b1 beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
推送
登录docker
[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: renweiwei
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
把本地的docker推送到docker hub
[root@localhost ~]# docker push gbj1123/busybox:b1
The push refers to repository [docker.io/renweiwei/busybox]
01fd6df81c8e: Mounted from library/busybox
b1: digest: sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee size: 527
###镜像的删除
####docker rmi
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
clearlinux/httpd 2 5b744a21cf88 3 days ago 122MB
busybox latest beae173ccac6 7 months ago 1.24MB
starskysmile/busybox v1 beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
[root@localhost ~]# docker rmi gbj1123/busybox:v1
Untagged: starskysmile/busybox:v1
Untagged: starskysmile/busybox@sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
clearlinux/httpd 2 5b744a21cf88 3 days ago 122MB
busybox latest beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
####docker image rm
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
clearlinux/httpd 2 5b744a21cf88 3 days ago 122MB
busybox latest beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
[root@localhost ~]# docker image rm clearlinux/httpd:2
Untagged: clearlinux/httpd:2
Untagged: clearlinux/httpd@sha256:d9c99c30dd549109d27400c4a57c200cf0f8ce45dc4020931072161172f01772
Deleted: sha256:5b744a21cf88ec54c09d85ef07fd5a5a50d77afb161727fda7d3900e46b2cd22
Deleted: sha256:b4085a4debca190215316e10125f9091528ad26cf21fb6e7501699af3c361f2c
Deleted: sha256:25fe972503d8782fc149537765224ae9dc74ed3e44700d380065aaf00c515405
Deleted: sha256:44aa16d19cedaa5662fc41b4d78e190008bb8dfe67a30481ec33da061473085a
Deleted: sha256:8bb09523c600d656fef42b87ab68aadb742be06b5049c79f0af1228872999498
Deleted: sha256:66372b7123d78eb57500a1b592411fecbfde9e1e84c75df451ea549d90532040
Deleted: sha256:8cc29680d8a057042bff9a1ab5f8287f1e1df42c31ce5f5951e2e725fa758333
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest beae173ccac6 7 months ago 1.24MB
httpd latest dabbfbe0c57b 7 months ago 144MB
###镜像的生成
镜像的生成途径:
Dockerfile
基于容器制作
Docker Hub automated builds
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Zn93d1t1-1659878781816)(./1.jpg)]
###基础容器制作镜像
根据容器的更改创建新映像
用法:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
|Options |Default |Description|
|-|
|—author, -a| |作者 (e.g., “John Hannibal Smith hannibal@a-team.com”)|
|-c, --change list| |Apply Dockerfile instruction to the created image|
|-m, --message string| |Commit message|
|-p, --pause| |true| 在提交期间暂停容器|
1.提取官方镜像
[root@localhost ~]# docker pull centos #提取centos官方镜像
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images #查看当前的镜像列表,有centos
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 10 months ago 231MB
2.使用镜像创建容器
[root@localhost ~]# docker run -d -it --name a1 centos #使用centos镜像创建一个仓库并运行,使用-d参数放在后台运行 -it设定容器使用shell
88cd83ee6c68506e52581965e2ec7d0523bcc2a89213c11fe9adad42b9026414
[root@localhost ~]# docker ps #查看当前正在运行中的镜像
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
88cd83ee6c68 centos "/bin/bash" 12 seconds ago Up 10 seconds a1
3.在创建镜像时,我们不能关闭容器,必须使其处于运行状态,所以我们必须要另起一个终端
4.在容器内可以搭建自己想要的服务
5.在主机上基于容器生成新的镜像
[root@localhost ~]# docker commit -a 'zhangsan <zhangsan@1234.com>' -c 'CMD ["/root/httpd.sh"]' -p a1 centos-httpd:v3
#-a指定作者极其邮箱,可以不添加该参数, -c指定默认启动程序 -p 为暂停容器 a1为容器名 centos-httpd:v3为自定义的镜像名和脚本
6.上传镜像,此时要注意的是,如果我们的仓库名叫centos-httpd,那么我们要在Docker Hub上创建一个名为centos-httpd的仓库,然后再将我们做好的镜像push上去
[root@localhost ~]# docker login #登入自己的docker hub账号
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xiang0311
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@localhost ~]# docker tag centos-httpd:v3 xiang0311/centos-httpd:v3 #先打上标签
[root@localhost ~]# docker push xiang0311/centos-httpd:v3 #再进行上传
The push refers to repository [docker.io/xiang0311/centos-httpd]
dce50ca3a723: Pushed
74ddd0ec08fa: Pushed
v3: digest: sha256:0baebe67ef6f6c8d68e1a28a246181d5baee455d0a213e39b485c3b69116dae5 size: 742
制作一个默认运行apache服务的镜像
提取镜像并搭建仓库
[root@localhost ~]# docker pull centos #提取centos官方镜像
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images #查看当前的镜像列表,有centos
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 10 months ago 231MB
[root@localhost ~]# docker run -d -it --name a1 centos #使用centos镜像创建一个仓库并运行,使用-d参数放在后台运行
88cd83ee6792506e52581965e2ec7d0523bcc2a89213c11fe9adad42b9026414
[root@localhost ~]# docker ps 查看当前正在运行中的镜像
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
88cd83ee6792 centos "/bin/bash" 12 seconds ago Up 10 seconds a1
[root@localhost ~]# docker exec -it a1 /bin/bash #进入容器内,-it参数用于指定其使用的hell
[root@88cd83ee6792 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
```
##镜像的导入与导出
假如有2台主机,我们在主机1上做了一个镜像,主机2想用这个镜像怎么办呢?
我们可以在主机1上push镜像到镜像仓库中,然后在主机2上pull把镜像拉下来使用,这种方式就显得比较麻烦,假如我只是测试用的,在一台主机上做好镜像后在另一台主机上跑一下就行了,没必要推到仓库上然后又把它拉到本地来。
此时我们可以在已有镜像的基础上把镜像打包成一个压缩文件,然后拷贝到另一台主机上将其导入,这就是镜像的导入和导出功能。
docker中我们使用docker save进行导出,使用docker load进行导入。
在已生成镜像的主机上执行docker save导出镜像
```
[root@localhost ~]# docker save -o centos-httpd.gz centos-httpd:v3
[root@localhost ~]# ls
centos-httpd.g
```
在另一台没有镜像的主机上执行docker load导入镜像
```
[root@localhost ~]# scp 'root'@'192.168.17.130':/root/centos-httpd.gz . #将远程主机上了文件cp至当前目录
The authenticity of host '192.168.17.130 (192.168.244.120)' can't be established.
ECDSA key fingerprint is SHA256:4OAMZHkSwnoRqgD6qIbh7i9pR5kxhfqySvERl985cnk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added '192.168.17.130' (ECDSA) to the list of known hosts.
root@192.168.17.130s password:
centos-httpd.gz 100% 701MB 83.3MB/s 00:08
[root@localhost ~]# ls
anaconda-ks.cfg centos-httpd.gz
[root@localhost ~]# docker load -i centos-httpd.gz #导入镜像
74ddd0ec08fa: Loading layer [====================================>] 238.6MB/238.6MB
dce50ca3a723: Loading layer [==================================>] 496.6MB/496.6MB
Loaded image: centos-httpd:v3
[root@localhost ~]# docker images #查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-httpd v3 f34e55fcfb14 About an hour ago 714MB
```