Docker(2):镜像管理
Docker 三个非常重要的理念:
三要素之间的关系:(本地和远端是逻辑上的概念 )
镜像的结构:
${registry_name}/${repository_name}/${image_name}:${tag_name} # 如: docker.io/library/alpine:3.10.1
登录 docker hub
在虚拟机上登录自己的 docker hub 账户:
~]$ docker login docker.io 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: neomaxxxxxxx Password: WARNING! Your password will be stored unencrypted in /home/ec2-user/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded # 账户和密码保存在了 /home/ec2-user/.docker/config.json 文件中。而且是未加密的,只是进行了 base64 的 encode 。所以通过 base64 decode 可解码, 命令如下: # echo "auth中base64加密的内容"| base64 -d
搜索镜像
如要搜索 alpine, 命令如下:(alpine 是 linux 的一种非常小的发行版)
~]$ docker search alpine
下载镜像:
~]$ docker pull alpine Using default tag: latest latest: Pulling from library/alpine 29291e31a76a: Pull complete Digest: sha256:eb3e4e175ba6d212ba1d6e04fc0782916c08e1c9d7b45892e9796141b1d379ae Status: Downloaded newer image for alpine:latest docker.io/library/alpine:latest
下载指定tag的镜像:
~]$ docker pull alpine:3.10.3 3.10.3: Pulling from library/alpine 89d9c30c1d48: Pull complete Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a Status: Downloaded newer image for alpine:3.10.3 docker.io/library/alpine:3.10.3
查看本地镜像:
~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 021b3423115f 2 weeks ago 5.6MB hello-world latest d1165f221234 5 months ago 13.3kB alpine 3.10.3 965ea09ff2eb 22 months ago 5.55MB ~]$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 021b3423115f 2 weeks ago 5.6MB hello-world latest d1165f221234 5 months ago 13.3kB alpine 3.10.3 965ea09ff2eb 22 months ago 5.55MB
给镜像打标签:
~]$ docker tag 965ea09ff2eb docker.io/neomaple/alpine:v3.10.3 // docker tage image-id registry_name/repository_name/image_name:tag_name // repository_name 就是你在 docker hub 的注册名 ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 021b3423115f 2 weeks ago 5.6MB hello-world latest d1165f221234 5 months ago 13.3kB alpine 3.10.3 965ea09ff2eb 22 months ago 5.55MB neomaple/alpine v3.10.3 965ea09ff2eb 22 months ago 5.55MB
推送镜像(在 login 的状态下)
~]$ docker push docker.io/neomaple/alpine:v3.10.3 The push refers to repository [docker.io/neomaple/alpine] 77cae8ab23bf: Mounted from library/alpine v3.10.3: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528
也可以给本地镜像打一个新的标签 tag,命令和给镜像打标签的命令是一样的
删除镜像
// 原先的镜像信息 ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 021b3423115f 2 weeks ago 5.6MB hello-world latest d1165f221234 5 months ago 13.3kB alpine 3.10.3 965ea09ff2eb 22 months ago 5.55MB neomaple/alpine v3.10.3 965ea09ff2eb 22 months ago 5.55MB ~]$ docker rmi docker.io/neomaple/alpine:v3.10.3 Untagged: neomaple/alpine:v3.10.3 Untagged: neomaple/alpine@sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a // docker rmi 镜像tag 只是把 tag 删掉了, 其镜像并没有删除,且 docker hub 上的该镜像也没有删除 ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 021b3423115f 2 weeks ago 5.6MB hello-world latest d1165f221234 5 months ago 13.3kB alpine 3.10.3 965ea09ff2eb 22 months ago 5.55MB // 想要真正删除镜像,需要用如下命令: docker rmi 镜像ID。如果该镜像在多个 repository 中被引用(或者多个标签同时指向该镜像),则删除时需要加上 -f 参数 ~]$ docker rmi 965ea09ff2eb Untagged: alpine:3.10.3 Untagged: alpine@sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a Deleted: sha256:965ea09ff2ebd2b9eeec88cd822ce156f6674c7e99be082c7efac3c62f3ff652 Deleted: sha256:77cae8ab23bf486355d1b3191259705374f4a11d483b24964d2f729dd8c076a0 ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 021b3423115f 2 weeks ago 5.6MB hello-world latest d1165f221234 5 months ago 13.3kB
Docker 结构示意图:
Docker 镜像特性:
1、 Docker 镜像位于 bootfs 之上
2、每一层镜像的下面一层称为其父镜像(父子关系)
3、第一层镜像称为 Base Image
4、容器在最顶层
5、其下所有层都为 readonly
6、Docker 将readonly 的FS层称作 "image"
Code your future.