1、Docker 镜像结构
- Docker镜像是一个典型的分层结构;
- 只有最上面一层是可写的,其他都是只读的,固化到镜像的;
- 每次推送都是增量的。
镜像名称的结构:
${registry_ name}/${repository. name}/${image. name}:${tag. name}
${远端仓库地址}/${分类仓库名称}$/{镜像名称}$/{标签名称}
例如:
docker.io/zhenxu1995/nginx:V3.10.1
2、登录Docker hub账号
[root@wzx-docker ~]# 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: zhenxu1995
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/.docker/config.json文件中,所以我们可以在这个文件中查看账号和密码信息。
[root@wzx-docker ~]# cat /root/.docker/config.json
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "emhlbnh1...........IzNDVXeng="
}
}
}
[root@wzx-docker ~]#echo "emhlbnh1...........IzNDVXeng=" |base64 -d
zhenxu1995:............[root@wzx-docker ~]
3、Docker 客户端
Docker 客户端非常简单 ,我们可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项。
[root@wzx-docker ~]# docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST
env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Build with BuildKit (Docker Inc., v0.5.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.8.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
可以通过命令 docker command --help 更深入的了解指定的 Docker 命令使用方法。
例如我们要查看 docker rmi 指令的具体使用方法:
[root@wzx-docker ~]# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
4、搜索镜像
我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个 alpine 的镜像;我们可以通过 docker search 命令搜索 寻找适合我们的镜像。
[root@wzx-docker ~]# docker search alpine
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
alpine A minimal Docker image based on Alpine Linux… 7622 [OK]
mhart/alpine-node Minimal Node.js built on Alpine Linux 483
anapsix/alpine-java Oracle Java 8 (and 7) with GLIBC 2.28 over A… 470 [OK]
frolvlad/alpine-glibc Alpine Docker image with glibc (~12MB) 261 [OK]
alpine/git A simple git container running in alpine li… 185 [OK]
mvertes/alpine-mongo light MongoDB container 116 [OK]
yobasystems/alpine-mariadb MariaDB running on Alpine Linux [docker] [am… 91 [OK]
alpine/socat Run socat command in alpine container 69 [OK]
kiasaki/alpine-postgres PostgreSQL docker image based on Alpine Linux 44 [OK
各项说明:
NAME:镜像仓库源的名称
DESCRIPTION:镜像的描述
OFFICIAL:是否 docker 官方发布
stars:类似 Github 里面的 star,表示点赞、喜欢的意思。
AUTOMATED:自动构建。
5、拉取镜像
如果不指定版本,默认下载最新版本 latest
[root@wzx-docker ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
5843afab3874: Pull complete
Digest: sha256:234cb88d3020898631af0ccbbcca9a66ae7306ecd30c9720690858c1b007d2a0
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
6、拉取指定版本镜像
[root@wzx-docker ~]# docker pull docker.io/library/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
7、查看已下载镜像
[root@wzx-docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest d4ff818577bc 2 weeks ago 5.6MB
hello-world latest d1165f221234 4 months ago 13.3kB
alpine 3.10.3 965ea09ff2eb 20 months ago 5.55MB
各项说明:
- REPOSITORY:表示镜像的仓库源
- TAG:镜像的标签
- IMAGE ID:镜像ID
- CREATED:镜像创建时间
- SIZE:镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如 ubuntu 仓库源里,有 15.10、14.04 等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。
8、给镜像打标签
我们可以使用 docker tag 命令,为镜像添加一个新的标签。
格式:docker tag 镜像ID 用户名称 镜像源名(repository name) 新的标签名(tag)
[root@wzx-docker ~]# docker tag d4ff818577bc docker.io/zhenxu1995/alpine:v3.10.1
[root@wzx-docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest d4ff818577bc 3 weeks ago 5.6MB
zhenxu1995/alpine v3.10.1 d4ff818577bc 3 weeks ago 5.6MB
hello-world latest d1165f221234 4 months ago 13.3kB
alpine 3.10.3 965ea09ff2eb 20 months ago 5.55MB
9、推送镜像到Docker hub上
[root@wzx-docker ~]# docker push zhenxu1995/alpine:v3.10.1
The push refers to repository [docker.io/zhenxu1995/alpine]
72e830a4dff5: Layer already exists
v3.10.1: digest: sha256:1775bebec23e1f3ce486989bfc9ff3c4e951690df84aa9f926497d82f2ffca9d size: 528
再次打标签,然后推送
[root@wzx-docker ~]# docker tag 965ea09ff2eb zhenxu1995/alpine:latest
[root@wzx-docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest d4ff818577bc 3 weeks ago 5.6MB
zhenxu1995/alpine v3.10.1 d4ff818577bc 3 weeks ago 5.6MB
hello-world latest d1165f221234 4 months ago 13.3kB
alpine 3.10.3 965ea09ff2eb 20 months ago 5.55MB
zhenxu1995/alpine latest 965ea09ff2eb 20 months ago 5.55MB
[root@wzx-docker ~]# docker push zhenxu1995/alpine:latest
The push refers to repository [docker.io/zhenxu1995/alpine]
77cae8ab23bf: Mounted from library/alpine
latest: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528
登录网页版Docker hub进行验证,可以看到我们分两次推送上去的镜像
10、删除镜像标签
[root@wzx-docker ~]# docker rmi zhenxu1995/alpine:latest
Untagged: zhenxu1995/alpine:latest
Untagged: zhenxu1995/alpine@sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a
[root@wzx-docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest d4ff818577bc 3 weeks ago 5.6MB
zhenxu1995/alpine v3.10.1 d4ff818577bc 3 weeks ago 5.6MB
hello-world latest d1165f221234 4 months ago 13.3kB
alpine 3.10.3 965ea09ff2eb 20 months ago 5.55MB
可以看到,之前zhenxu1995/alpine:latest的标签已经被删除了
11、删除镜像
使用docker rmi -f 将镜像ID为965ea09ff2eb的镜像进行删除
[root@wzx-docker ~]# docker rmi -f 965ea09ff2eb
Untagged: alpine:3.10.3
Untagged: alpine@sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
Deleted: sha256:965ea09ff2ebd2b9eeec88cd822ce156f6674c7e99be082c7efac3c62f3ff652
Deleted: sha256:77cae8ab23bf486355d1b3191259705374f4a11d483b24964d2f729dd8c076a0
[root@wzx-docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest d4ff818577bc 3 weeks ago 5.6MB
zhenxu1995/alpine v3.10.1 d4ff818577bc 3 weeks ago 5.6MB
hello-world latest d1165f221234 4 months ago 13.3kB
12、从Docker hub上拉取镜像
从Docker hub 上重新将zhenxu1995/alpine:latest镜像拉取下来
[root@wzx-docker ~]# docker pull zhenxu1995/alpine:latest
latest: Pulling from zhenxu1995/alpine
89d9c30c1d48: Pull complete
Digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a
Status: Downloaded newer image for zhenxu1995/alpine:latest
docker.io/zhenxu1995/alpine:latest
[root@wzx-docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest d4ff818577bc 3 weeks ago 5.6MB
zhenxu1995/alpine v3.10.1 d4ff818577bc 3 weeks ago 5.6MB
hello-world latest d1165f221234 4 months ago 13.3kB
zhenxu1995/alpine latest 965ea09ff2eb 20 months ago 5.55MB