目录
一、仓库(Repository)
用来保存镜像的地方。
1. 镜像站
使用镜像站需要在网站注册账号,国内镜像站很多以下列举华为镜像站。
Docker官方网站(配置镜像加速器):Docker Hub
国内网站:华为开源镜像站
2. 登录
输入用户名和密码登陆镜像站,可拉取自己账号下的镜像在本地使用。
语法结构:
docker login
案例:
1.登陆Docker官方网站:
[root@localhost ~]# docker login #默认登陆到Docker Hub网站
Log in with your Docker ID or email address 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.
You can log in with your password or a Personal Access Token (PAT). Using a limited-scope PAT grants better security and is required for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/
Username: test.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
2.登陆华为镜像站:
[root@localhost ~]# docker login mirrors.huaweicloud.com #登陆华为镜像站
Username: test.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
3. 登出
退出登陆的镜像站。
语法结构:
docker logout
[root@localhost ~]# docker logout
Removing login credentials for https://index.docker.io/v1/
二、镜像(Images)
镜像用于运行容器,没有镜像不能运行容器。镜像不在本地存在,获取镜像会从Docker官方下载。
1. 查找镜像
查看官方(Docker Hub)镜像。
语法结构:
docker search 镜像名
[root@localhost ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos DEPRECATED; The official build of CentOS. 7668 [OK]
kasmweb/centos-7-desktop CentOS 7 desktop for Kasm Workspaces 41
bitnami/centos-base-buildpack Centos base compilation image 0 [OK]
couchbase/centos7-systemd centos7-systemd images with additional debug… 8 [OK]
...
[root@localhost ~]# docker search centos --filter=stars=100 #只显示收藏大于100的镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos DEPRECATED; The official build of CentOS. 7668 [OK]
输出结果说明:
- NAME:镜像名称
- DESCRIPTION:镜像描述
- STARS:收藏数,即星级数
- OFFICTAL:是否是Docker官方发布镜像
- AUTOMATED:是否自动构建
2. 查看镜像
查看本地镜像信息。
语法结构:
docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
输出结果说明:
- REPOSITORY:镜像的仓库源
- TAG:镜像的标签,标记镜像不同的版本
- IMAGE ID:镜像的ID
- CREATED:创建镜像的时间
- SIZE:镜像的大小
注意:
同一个仓库可以有多个镜像,但tag版本需要不同。如果ubuntu仓库中存在15.10、20.04等多个不同版本,使用repository:tag定义同一镜像的不同版本,即ubuntu:15.10和ubuntu:20.04。
3. 拉取镜像即获取新的镜像
将镜像站中的镜像下载到本地,获取镜像不指定tag会自动下载最新版镜像。
语法结构:
docker pull 镜像名
案例:
1.登陆到Docker官方网站,下载centos镜像到本地:
[root@localhost ~]# docker login #登陆到Docker Hub
Log in with your Docker ID or email address 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.
You can log in with your password or a Personal Access Token (PAT). Using a limited-scope PAT grants better security and is required for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/
Username: test.com
Password:
...
Login Succeeded
[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镜像,latest为最新版
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
2.不登陆直接获取ubuntu镜像到本地(ubuntu镜像本地不存在会自动从Docker Hub下载):
[root@localhost ~]# docker logout #登出Docker Hub网站
Removing login credentials for https://index.docker.io/v1/
[root@localhost ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
4. 镜像标签
为镜像添加新的标签。
语法结构:
docker tag 原镜像名/镜像ID 用户名/原镜像名:新标签
[root@localhost ~]# docker tag ubuntu name/ubuntu:v1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 years ago 72.8MB
name/ubuntu v1 ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
5. 推送镜像
用户登陆到镜像站后,可以将自己的镜像上传至镜像站。
语法结构:
docker push 镜像名
案例:
1.使用拉取的Ubuntu镜像重新生成name/ubuntu:v1,并将镜像推送到Docker官方镜像站(注意name替换成Docker Hub中自己的账号名称):
[root@localhost ~]# docker login #登陆到Docker Hub
Log in with your Docker ID or email address 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.
You can log in with your password or a Personal Access Token (PAT). Using a limited-scope PAT grants better security and is required for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/
Username: max_top1002@163.com
Password:
...
Login Succeeded
[root@localhost ~]# docker images #查看docker镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@localhost ~]# docker tag ubuntu name/ubuntu:v1 #使用现有ubuntu镜像重新生成新镜像存储在本地,将name改为自己Docker Hub账号的用户名
[root@localhost ~]# docker images #查看本地镜像,name/ubuntu存在
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 years ago 72.8MB
name/ubuntu v1 ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@localhost ~]# docker push name/ubuntu:v1 #将生成的新镜像推送到Docker Hub镜像站
The push refers to repository [docker.io/name/ubuntu]
9f54eef41275: Mounted from library/ubuntu
v1: digest: sha256:7cc0576c7c0ec2384de5cbf245f41567e922aab1b075f3e8ad565f508032df17 size: 529
[root@localhost ~]# docker search name/ubuntu #查看推送成功
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
name/ubuntu 0
6. 删除镜像
删除本地镜像。
语法结构:
docker rmi 镜像名/镜像ID [--force] #--force为可选,加入该参数可以删除1或多个
案例:
1.删除centos镜像:
[root@localhost ~]# docker rmi centos
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
2.同时删除ubuntu和name/ubuntu:v1镜像:
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
name/ubuntu v1 ba6acccedd29 2 years ago 72.8MB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
[root@localhost ~]# docker rmi --force ubuntu ba6acccedd29
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Untagged: name/ubuntu:v1
Deleted: sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
3.快速删除所有镜像:
[root@localhost ~]# docker images #拉取ubuntu和centos镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@localhost ~]# docker rmi $(docker images -q) #先获取所有镜像的id,再删除获取到的id值
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Deleted: sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1
Deleted: sha256:9f54eef412758095c8079ac465d494a2872e02e90bf1fb5f12a1641c0d1bb78b
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
扩展:删除推送到Docker官网的镜像
1.进入自己的Docker Hub仓库,单击镜像名:
2.进入镜像点击Settings,下拉找到Delete repository:
3.输入关键字后,单击Delete删除镜像: