镜像的结构

image.png

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

image.png
镜像名称的结构
${registry_ name}/${repository. name}/${image. name}:${tag. name}
例如:
docker.io/library/alpine:3.10.1

登陆到dokcer.io

  1. [root@alice ~]# 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: mmdghh
  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@alice ~]#

查看已经登陆的信息

[root@alice ~]# cat /root/.docker/config.json 
{
    "auths": {
        "https://index.docker.io/v1/": {
            "auth": "5bCP5LiR56uf5Zyo5oiR6Lqr6L65"
        }
    },
    "HttpHeaders": {
        "User-Agent": "Docker-Client/18.09.5 (linux)"
    }
}[root@alice ~]# 
注: 此处的密码可以用echo "5bCP5LiR56uf5Zyo5oiR6Lqr6L65" |base64 -d 进行解码

搜索镜像

[root@alice ~]# docker search alpine
NAME                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
alpine                                 A minimal Docker image based on Alpine Linux…   7047                [OK]                
mhart/alpine-node                      Minimal Node.js built on Alpine Linux           478                                     
anapsix/alpine-java                    Oracle Java 8 (and 7) with GLIBC 2.28 over A…   466                                     [OK]
frolvlad/alpine-glibc                  Alpine Docker image with glibc (~12MB)          251                                     [OK]
gliderlabs/alpine                      Image based on Alpine Linux will help you wi…   183                                     
alpine/git                             A  simple git container running in alpine li…   161                                     [OK]
mvertes/alpine-mongo                   light MongoDB container                         117                                     [OK]
yobasystems/alpine-mariadb             MariaDB running on Alpine Linux [docker] [am…   79                                      [OK]
alpine/socat                           Run socat command in alpine container           62                                      [OK]
kiasaki/alpine-postgres                PostgreSQL docker image based on Alpine Linux   45                                      [OK]
davidcaste/alpine-tomcat               Apache Tomcat 7/8 using Oracle Java 7/8 with…   43                                      [OK]
jfloff/alpine-python                   A small, more complete, Python Docker image …   38                                      [OK]
byrnedo/alpine-curl                    Alpine linux with curl installed and set as …   34                                      [OK]
hermsi/alpine-sshd                     Dockerize your OpenSSH-server with rsync and…   32                                      [OK]
zenika/alpine-chrome                   Chrome running in headless mode in a tiny Al…   27                                      [OK]
hermsi/alpine-fpm-php                  FPM-PHP 7.0 to 8.0, shipped along with tons …   25                                      [OK]
etopian/alpine-php-wordpress           Alpine WordPress Nginx PHP-FPM WP-CLI           24                                      [OK]
bashell/alpine-bash                    Alpine Linux with /bin/bash as a default she…   18                                      [OK]
davidcaste/alpine-java-unlimited-jce   Oracle Java 8 (and 7) with GLIBC 2.21 over A…   13                                      [OK]
roribio16/alpine-sqs                   Dockerized ElasticMQ server + web UI over Al…   11                                      [OK]
spotify/alpine                         Alpine image with `bash` and `curl`.            11                                      [OK]
cfmanteiga/alpine-bash-curl-jq         Docker Alpine image with Bash, curl and jq p…   6                                       [OK]
ellerbrock/alpine-mysql-client         MySQL Client based on Alpine Linux              1                                       [OK]
bushrangers/alpine-caddy               Alpine Linux Docker Container running Caddys…   1                                       [OK]
dwdraju/alpine-curl-jq                 Alpine Docker Image with curl, jq, bash         0                                       [OK]
[root@alice ~]#

拉取镜像

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

[root@alice ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
801bfaa63ef2: Pull complete 
Digest: sha256:3c7497bf0c7af93428242d6176e8f7905f2201d8fc5861f45be7a346b5f23436
Status: Downloaded newer image for alpine:latest
[root@alice ~]#

也可以指定版本下载

[root@alice ~]# 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
[root@alice ~]# docker pull docker.io/library/alpine:3.10.3
3.10.3: Pulling from library/alpine
Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
Status: Image is up to date for alpine:3.10.3
[root@alice ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              389fef711851        3 weeks ago         5.58MB
alpine              3.10.3              965ea09ff2eb        14 months ago       5.55MB
[root@alice ~]# 
如果使用官方的docker.io 可以不写前面的docker.io/library/ 因为默认就是公开的  如果是自己的或者其他仓库 需要写全

给镜像打tag(标签)

[root@alice ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              389fef711851        3 weeks ago         5.58MB
alpine              3.10.3              965ea09ff2eb        14 months ago       5.55MB
[root@alice ~]# docker tag 965ea09ff2eb docker.io/mmdghh/alpine:v3.10.3
[root@alice ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              389fef711851        3 weeks ago         5.58MB
alpine              3.10.3              965ea09ff2eb        14 months ago       5.55MB
mmdghh/alpine       v3.10.3             965ea09ff2eb        14 months ago       5.55MB
[root@alice ~]# 
IMAGE ID一样的话说明镜像是一样的 前面的tag只是一个指针 就像软链接

推送到远程仓库

[root@alice ~]# docker push docker.io/mmdghh/alpine:v3.10.3
The push refers to repository [docker.io/mmdghh/alpine]
77cae8ab23bf: Mounted from library/alpine 
received unexpected HTTP status: 504 Gateway Time-out
[root@alice ~]# vim /etc/docker/daemon.json 
[root@alice ~]# docker push docker.io/mmdghh/alpine:v3.10.3
The push refers to repository [docker.io/mmdghh/alpine]
77cae8ab23bf: Mounted from library/alpine 
Head https://registry-1.docker.io/v2/mmdghh/alpine/blobs/sha256:965ea09ff2ebd2b9eeec88cd822ce156f6674c7e99be082c7efac3c62f3ff652: net/http: TLS handshake timeout
[root@alice ~]# docker push docker.io/mmdghh/alpine:v3.10.3
The push refers to repository [docker.io/mmdghh/alpine]
77cae8ab23bf: Mounted from library/alpine 
Post https://registry-1.docker.io/v2/mmdghh/alpine/blobs/uploads/: net/http: TLS handshake timeout
[root@alice ~]# docker push docker.io/mmdghh/alpine:v3.10.3
The push refers to repository [docker.io/mmdghh/alpine] # 能推送的原因是之前登陆过了
77cae8ab23bf: Layer already exists 
v3.10.3: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528
[root@alice ~]# 
前面几次都失败了 因为网络不稳定

推送后可以在网页看到对应的镜像
image.png

image.png
推送一个latest版本

[root@alice ~]# docker tag 965ea09ff2eb docker.io/mmdghh/alpine:latest
[root@alice ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              389fef711851        3 weeks ago         5.58MB
alpine              3.10.3              965ea09ff2eb        14 months ago       5.55MB
mmdghh/alpine       latest              965ea09ff2eb        14 months ago       5.55MB
mmdghh/alpine       v3.10.3             965ea09ff2eb        14 months ago       5.55MB
[root@alice ~]# docker push docker.io/mmdghh/alpine:latest
The push refers to repository [docker.io/mmdghh/alpine]
77cae8ab23bf: Layer already exists # 这层已经存在 不会重复推送(增量)
latest: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528
[root@alice ~]#

image.png

删除镜像

[root@alice ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              389fef711851        3 weeks ago         5.58MB
alpine              3.10.3              965ea09ff2eb        14 months ago       5.55MB
mmdghh/alpine       latest              965ea09ff2eb        14 months ago       5.55MB
mmdghh/alpine       v3.10.3             965ea09ff2eb        14 months ago       5.55MB
[root@alice ~]# docker rmi 965ea09ff2eb
Error response from daemon: conflict: unable to delete 965ea09ff2eb (must be forced) - image is referenced in multiple repositories #这个ID有多个tag 所以需要用-f 来删除
[root@alice ~]# docker rmi -f 965ea09ff2eb 
Untagged: alpine:3.10.3
Untagged: alpine@sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a 先去掉tag再删除
Untagged: mmdghh/alpine:latest
Untagged: mmdghh/alpine:v3.10.3
Untagged: mmdghh/alpine@sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a
Deleted: sha256:965ea09ff2ebd2b9eeec88cd822ce156f6674c7e99be082c7efac3c62f3ff652
Deleted: sha256:77cae8ab23bf486355d1b3191259705374f4a11d483b24964d2f729dd8c076a0
[root@alice ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              389fef711851        3 weeks ago         5.58MB
[root@alice ~]#

上面的删除操作只是删除了本地的镜像,不会对远程仓库的镜像产生影响
image.png

[root@alice ~]# docker pull docker.io/mmdghh/alpine
Using default tag: latest
latest: Pulling from mmdghh/alpine
89d9c30c1d48: Pull complete 
Digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a
Status: Downloaded newer image for mmdghh/alpine:latest
[root@alice ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              389fef711851        3 weeks ago         5.58MB
mmdghh/alpine       latest              965ea09ff2eb        14 months ago       5.55MB
[root@alice ~]#