1. 镜像的结构

03_镜像结构.png

  • docker镜像是一个典型的分层结构
  • 只有最上面一层是可写的 其他都是只读的固化到镜像的
  • 每次推送都是增量的

03_容器镜像关系.png

镜像名称的结构

  1. ${registry_name}/${repository_name}/${image_name}:${tag_name}
  2. # 远端仓库地址/仓库名字/镜像名:镜像标签(即版本号)

例如:

  1. docker.io/library/alpine:3.10.3

2. 注册DockerHub

dockerhub:全球最大的docker官方镜像仓库,开源免费。

地址:https://hub.docker.com/
04_dockerhub注册.png
邮箱验证 —-> 登录
05_dockerhub登录成功.png

2.1 docker登录dockerhub:

登录到docker.io

  1. [root@localhost ~]# docker login docker.io
  2. 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.
  3. Username: zhanghb123
  4. Password:
  5. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  6. Configure a credential helper to remove this warning. See
  7. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  8. Login Succeeded
  9. [root@localhost ~]#

2.2 查看已登录信息

  1. [root@localhost ~]# cat /root/.docker/config.json
  2. {
  3. "auths": {
  4. "https://index.docker.io/v1/": {
  5. "auth": "emhhbmdoYjEyMzpaaGIxMjMzMjFA"
  6. }
  7. }
  8. }[root@localhost ~]#
  9. 注: 此处的密码可以用echo "5bCP5LiR56uf5Zyo5oiR6Lqr6L65" |base64 -d 进行解码

3. 搜索镜像

dockerhub 官方镜像,也可以在浏览器登录dockerhub网页搜索

Alpine 操作系统是一个面向安全的轻型 Linux 发行版。

  1. [root@localhost ~]# docker search alpine
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. alpine A minimal Docker image based on Alpine Linux 7337 [OK]
  4. mhart/alpine-node Minimal Node.js built on Alpine Linux 483
  5. anapsix/alpine-java Oracle Java 8 (and 7) with GLIBC 2.28 over A 468 [OK]
  6. frolvlad/alpine-glibc Alpine Docker image with glibc (~12MB) 259 [OK]
  7. gliderlabs/alpine Image based on Alpine Linux will help you wi 183
  8. alpine/git A simple git container running in alpine li 175 [OK]
  9. yobasystems/alpine-mariadb MariaDB running on Alpine Linux [docker] [am 86 [OK]
  10. alpine/socat Run socat command in alpine container 68 [OK]
  11. kiasaki/alpine-postgres PostgreSQL docker image based on Alpine Linux 44 [OK]
  12. davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 with 44 [OK]
  13. jfloff/alpine-python A small, more complete, Python Docker image 40 [OK]
  14. byrnedo/alpine-curl Alpine linux with curl installed and set as 34 [OK]
  15. hermsi/alpine-sshd Dockerize your OpenSSH-server with rsync and 33 [OK]
  16. zenika/alpine-chrome Chrome running in headless mode in a tiny Al 31 [OK]
  17. hermsi/alpine-fpm-php FPM-PHP 7.0 to 8.0, shipped along with tons 25 [OK]
  18. etopian/alpine-php-wordpress Alpine WordPress Nginx PHP-FPM WP-CLI 24 [OK]
  19. bashell/alpine-bash Alpine Linux with /bin/bash as a default she 18 [OK]
  20. davidcaste/alpine-java-unlimited-jce Oracle Java 8 (and 7) with GLIBC 2.21 over A 13 [OK]
  21. roribio16/alpine-sqs Dockerized ElasticMQ server + web UI over Al 12 [OK]
  22. spotify/alpine Alpine image with `bash` and `curl`. 11 [OK]
  23. cfmanteiga/alpine-bash-curl-jq Docker Alpine image with Bash, curl and jq p 6 [OK]
  24. bushrangers/alpine-caddy Alpine Linux Docker Container running Caddys 1 [OK]
  25. ellerbrock/alpine-mysql-client MySQL Client based on Alpine Linux 1 [OK]
  26. dwdraju/alpine-curl-jq Alpine Docker Image with curl, jq, bash 1 [OK]
  27. goodguykoi/alpine-curl-internal simple alpine image with curl installed no C 1 [OK]
  28. [root@localhost ~]#

4. 拉取镜像

如果不指定tag 默认下载最新版本 latest

  1. [root@localhost ~]# docker pull alpine
  2. Using default tag: latest
  3. latest: Pulling from library/alpine
  4. 540db60ca938: Pull complete
  5. Digest: sha256:69e70a79f2d41ab5d637de98c1e0b055206ba40a8145e7bddb55ccc04e13cf8f
  6. Status: Downloaded newer image for alpine:latest
  7. docker.io/library/alpine:latest # 仓库地址/仓库/镜像名:标签(版本)
  8. [root@localhost ~]#

指定下载版本:

  1. [root@localhost ~]# docker pull alpine:3.10.3
  2. 3.10.3: Pulling from library/alpine
  3. 89d9c30c1d48: Pull complete
  4. Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
  5. Status: Downloaded newer image for alpine:3.10.3
  6. docker.io/library/alpine:3.10.3
  7. [root@localhost ~]#
  8. # 如果使用官方的docker.io 可以不写前面的docker.io/library/ 因为默认就是公开的 如果是自己的或者其他仓库 需要写全

5. 镜像打标签

  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. alpine latest 6dbb9cc54074 2 days ago 5.61MB
  4. alpine 3.10.3 965ea09ff2eb 18 months ago 5.55MB
  5. [root@localhost ~]# docker tag 965ea09ff2eb docker.io/zhanghb123/alpine:v3.10.3
  6. [root@localhost ~]# docker images
  7. REPOSITORY TAG IMAGE ID CREATED SIZE
  8. alpine latest 6dbb9cc54074 2 days ago 5.61MB
  9. alpine 3.10.3 965ea09ff2eb 18 months ago 5.55MB
  10. zhanghb123/alpine v3.10.3 965ea09ff2eb 18 months ago 5.55MB
  11. [root@localhost ~]#
  12. # IMAGE ID相同的话说明镜像是一样的 前面的tag只是一个指针 就像软链接

6. 推送至远程仓库

  1. [root@localhost ~]# docker push docker.io/zhanghb123/alpine:v3.10.3
  2. The push refers to repository [docker.io/zhanghb123/alpine]
  3. 77cae8ab23bf: Mounted from library/alpine
  4. v3.10.3: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528
  5. [root@localhost ~]#

推送后可以在网页看到对应的镜像
07_dockerhub推送截图.png07_dockerhub推送截图1.png

7. 删除镜像

  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. alpine latest 6dbb9cc54074 2 days ago 5.61MB
  4. zhanghb123/alpine v3.10.3 965ea09ff2eb 18 months ago 5.55MB
  5. alpine 3.10.3 965ea09ff2eb 18 months ago 5.55MB
  6. [root@localhost ~]# docker rmi 965ea09ff2eb
  7. Error response from daemon: conflict: unable to delete 965ea09ff2eb (must be forced) - image is referenced in multiple repositories # #这个ID有多个tag 所以需要用-f 来删除
  8. [root@localhost ~]# docker rmi -f 965ea09ff2eb
  9. Untagged: alpine:3.10.3
  10. Untagged: alpine@sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a # 先去掉tag再删除
  11. Untagged: zhanghb123/alpine:v3.10.3
  12. Untagged: zhanghb123/alpine@sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a
  13. Deleted: sha256:965ea09ff2ebd2b9eeec88cd822ce156f6674c7e99be082c7efac3c62f3ff652
  14. Deleted: sha256:77cae8ab23bf486355d1b3191259705374f4a11d483b24964d2f729dd8c076a0
  15. [root@localhost ~]# docker images
  16. REPOSITORY TAG IMAGE ID CREATED SIZE
  17. alpine latest 6dbb9cc54074 2 days ago 5.61MB
  18. [root@localhost ~]#
  19. # 上面的删除操作只是删除了本地的镜像,不会对远程仓库的镜像产生影响