docker镜像管理

镜像是什么
简单来说docker镜像是一个不包含linux内核而又精简的linux操作系统。
镜像从哪里来
docker hub是由docker公司负责维护的公共注册中心,包含大量的容器镜像,docker工具默认从这个镜像库下载镜像。https://hub.docker.com/explore
默认国外源,下载较慢,建议配置国内镜像仓库:
vim /etc/docker/daemon.json
{
"registry-mirrors":["https://registry.docker-cn.com"]
}
重启docker生效
[root@docker docker]# systemctl restart docker
[root@docker docker]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
53071b97a884: Pull complete
Digest: sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3d
Status: Downloaded newer image for busybox:latest
[root@docker docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 62c261073ecf 2 days ago 109MB
busybox latest 64f5d945efcc 4 weeks ago 1.2MB
镜像与容器联系
镜像不是一个单一的文件,而是有多层构成。我们可以通过docker history<ID/NAME>查看镜像中各层内容及大小,每层对应着dockerfile中的一条指令。docker镜像默认存储在/var/lib/docker/中。
容器其实是在镜像的最上面加了一层读写层,在运行容器里做的任何文件改动,都会写到这个读写层。如果容器删除了,最上面的读写层也就删除了,改动也就丢失了。
docker使用存储驱动管理镜像每层内容及可读写层的容器层。

[root@docker docker]# docker history nginx
IMAGE CREATED CREATED BY SIZE COMMENT
62c261073ecf 2 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
2 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B
2 days ago /bin/sh -c #(nop) EXPOSE 80 0B
2 days ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx… 22B
2 days ago /bin/sh -c set -x && addgroup --system -… 54MB
2 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~stretch 0B
2 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.3.2 0B
2 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.17.0 0B
4 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
4 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
4 weeks ago /bin/sh -c #(nop) ADD file:fcb9328ea4c115670… 55.3MB
[root@docker docker]# ls
builder buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
镜像管理命令
[root@docker docker]# docker help

Usage: docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
--config string Location of client config files (default "/root/.docker")
-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:
builder Manage builds
config Manage Docker configs
container Manage containers
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
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.
[root@docker docker]# docker image help

Usage: docker image COMMAND

Manage images

Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.
[root@docker docker]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 62c261073ecf 2 days ago 109MB
busybox latest 64f5d945efcc 4 weeks ago 1.2MB
[root@docker docker]# docker image history busybox
IMAGE CREATED CREATED BY SIZE COMMENT
64f5d945efcc 4 weeks ago /bin/sh -c #(nop) CMD ["sh"] 0B
4 weeks ago /bin/sh -c #(nop) ADD file:e36dd1822f36a8169… 1.2MB
查看镜像信息
[root@docker docker]# docker image inspace

Usage: docker image COMMAND

Manage images

Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.
[root@docker docker]# docker image inspect nginx
[
{
"Id": "sha256:62c261073ecffe22a28f2ba67760a9320bc4bfe8136a83ba9b579983346564be",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:12db363acf5b2d2f9f5fed240e228a04692bdac68402430fbd2f720c3a967d01"
],
"Parent": "",
"Comment": "",
"Created": "2019-06-04T22:29:15.22271163Z",
"Container": "6e81d902bf0b5631099c0789d2573a348a26649a73391e67fa4b14545f6de200",
"ContainerConfig": {
"Hostname": "6e81d902bf0b",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.17.0",
"NJS_VERSION=0.3.2",
"PKG_RELEASE=1~stretch"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD ["nginx" "-g" "daemon off;"]"
],
"ArgsEscaped": true,
"Image": "sha256:b4c546a22099dc546c525e66dd7bbf40bc183195a0559d418b04057cbe363792",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers docker-maint@nginx.com"
},
"StopSignal": "SIGTERM"
},
"DockerVersion": "18.06.1-ce",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.17.0",
"NJS_VERSION=0.3.2",
"PKG_RELEASE=1~stretch"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"ArgsEscaped": true,
"Image": "sha256:b4c546a22099dc546c525e66dd7bbf40bc183195a0559d418b04057cbe363792",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers docker-maint@nginx.com"
},
"StopSignal": "SIGTERM"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 109258871,
"VirtualSize": 109258871,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/cc87902e3a53ec2d035789fd293dd20d3f2c5483c36ded801dde84567abf8b9e/diff:/var/lib/docker/overlay2/a5fe97da169a47defdb199e3b1ef5ef50702864f57998b2b4958e38ca4150ccc/diff",
"MergedDir": "/var/lib/docker/overlay2/c5e5d61d04d7659136d27a78a17b2bdc66f6a01d48d33da7872d0ad056387961/merged",
"UpperDir": "/var/lib/docker/overlay2/c5e5d61d04d7659136d27a78a17b2bdc66f6a01d48d33da7872d0ad056387961/diff",
"WorkDir": "/var/lib/docker/overlay2/c5e5d61d04d7659136d27a78a17b2bdc66f6a01d48d33da7872d0ad056387961/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:6270adb5794c6987109e54af00ab456977c5d5cc6f1bc52c1ce58d32ec0f15f4",
"sha256:22c458a3ff08c2249c2362281035476427dd84ae6c4d12a14e1f79e7ae325eba",
"sha256:ea06a73e56fce0a3293829c54bedc2f77d88c4d5951ff1acd59ee1507ad2ed7f"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
编辑标签
[root@docker ~]# docker tag nginx:latest nginx:v1
查看镜像列表
[root@docker ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 62c261073ecf 2 days ago 109MB
nginx v1 62c261073ecf 2 days ago 109MB
busybox latest 64f5d945efcc 4 weeks ago 1.2MB
删除镜像
[root@docker ~]# docker image rm nginx:v1
Untagged: nginx:v1
[root@docker ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 62c261073ecf 2 days ago 109MB
busybox latest 64f5d945efcc 4 weeks ago 1.2MB
保存镜像
[root@docker ~]# docker image save nginx:latest >nginx_latest.tar
[root@docker ~]# du -sh nginx_latest.tar
108M nginx_latest.tar
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e8c8d5983eef busybox "sh" 51 seconds ago Up 50 seconds naughty_poitras
导出镜像
[root@docker ~]# docker export e8c8d5983eef > busybox.tar
[root@docker ~]# du -sh busybox.tar
1.4M busybox.tar
导入镜像
[root@docker ~]# docker image import busybox.tar busybox:busybox_testsha256:c31d10bdde73e9355e9b499cf94022f522ac8d0613139ab4d70608a6c76c52a7
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox busybox_test c31d10bdde73 14 seconds ago 1.2MB
nginx latest 62c261073ecf 2 days ago 109MB
busybox latest 64f5d945efcc 4 weeks ago 1.2MB
nginx 1.12 4037a5562b03 13 months ago 108MB
export:导出容器文件系统到tar归档文件。
save:保存一个或多个镜像到一个tar归档文件。

posted on 2019-06-07 23:20  yinshoucheng  阅读(537)  评论(0编辑  收藏  举报

导航