Docker: docker image常用命令实战
#docker列出镜像
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 881bd08c0b08 6 days ago 109MB
#docker:查看镜像名为nginx的镜像历史
[root@192 ~]# docker history nginx
IMAGE CREATED CREATED BY SIZE COMMENT
881bd08c0b08 6 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 6 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B
<missing> 6 days ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 6 days ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx… 22B
<missing> 6 days ago /bin/sh -c set -x && apt-get update && apt… 54MB
<missing> 6 days ago /bin/sh -c #(nop) ENV NJS_VERSION=1.15.9.0.… 0B
<missing> 6 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.15.9-… 0B
<missing> 6 days ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
<missing> 6 days ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 6 days ago /bin/sh -c #(nop) ADD file:5ea7dfe8c8bc87ebe… 55.3MB
#docker:查看镜像名为nginx的镜像历史,查看完整内容,不被截取,CREATED By 一行对应dockfile文件的一行命令
[root@192 ~]# docker history --no-trunc nginx
IMAGE CREATED CREATED BY SIZE COMMENT
sha256:881bd08c0b08234bd19136957f15e4301097f4646c1e700f7fea26e41fc40069 6 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon off;"] 0B
<missing> 6 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B
<missing> 6 days ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 6 days ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log 22B
<missing> 6 days ago /bin/sh -c set -x && apt-get update && apt-get install --no-install-recommends --no-install-suggests -y gnupg1 apt-transport-https ca-certificates && NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; found=''; for server in ha.pool.sks-keyservers.net hkp://keyserver.ubuntu.com:80 hkp://p80.pool.sks-keyservers.net:80 pgp.mit.edu ; do echo "Fetching GPG key $NGINX_GPGKEY from $server"; apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; done; test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/* && dpkgArch="$(dpkg --print-architecture)" && nginxPackages=" nginx=${NGINX_VERSION} nginx-module-xslt=${NGINX_VERSION} nginx-module-geoip=${NGINX_VERSION} nginx-module-image-filter=${NGINX_VERSION} nginx-module-njs=${NJS_VERSION} " && case "$dpkgArch" in amd64|i386) echo "deb https://nginx.org/packages/mainline/debian/ stretch nginx" >> /etc/apt/sources.list.d/nginx.list && apt-get update ;; *) echo "deb-src https://nginx.org/packages/mainline/debian/ stretch nginx" >> /etc/apt/sources.list.d/nginx.list && tempDir="$(mktemp -d)" && chmod 777 "$tempDir" && savedAptMark="$(apt-mark showmanual)" && apt-get update && apt-get build-dep -y $nginxPackages && ( cd "$tempDir" && DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)" apt-get source --compile $nginxPackages ) && apt-mark showmanual | xargs apt-mark auto > /dev/null && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; } && ls -lAFh "$tempDir" && ( cd "$tempDir" && dpkg-scanpackages . > Packages ) && grep '^Package: ' "$tempDir/Packages" && echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list && apt-get -o Acquire::GzipIndexes=false update ;; esac && apt-get install --no-install-recommends --no-install-suggests -y $nginxPackages gettext-base && apt-get remove --purge --auto-remove -y apt-transport-https ca-certificates && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list && if [ -n "$tempDir" ]; then apt-get purge -y --auto-remove && rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; fi 54MB
<missing> 6 days ago /bin/sh -c #(nop) ENV NJS_VERSION=1.15.9.0.2.8-1~stretch 0B
<missing> 6 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.15.9-1~stretch 0B
<missing> 6 days ago /bin/sh -c #(nop) LABEL maintainer=NGINX Docker Maintainers <docker-maint@nginx.com> 0B
<missing> 6 days ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 6 days ago /bin/sh -c #(nop) ADD file:5ea7dfe8c8bc87ebe0d06d275bce41e015310bdfc04546246302e9ce07ee416c in / 55.3MB
#docker工作目录
[root@192 ~]# ls /var/lib/docker
builder buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
#overlay2是镜像驱动
[root@192 ~]# ls /var/lib/docker/overlay2/
3ad0bc68ee6c3e92492a5d56b107da1834e0f22c8849016604b5110f8c67c8c8
772eafb718462ae983f10fd78795b4fe73b46640e7e956ffb9571eee3ef85ea0
backingFsBlockDev
c04bd3d232d46217bc57de00dc47b3bad9d9aa478398b41cfeae3a187092610d
e15b4ab77eaa30d5a08cdff1cb80a2ea5ab2815cc254a466fdf9a050ecb0f198
e15b4ab77eaa30d5a08cdff1cb80a2ea5ab2815cc254a466fdf9a050ecb0f198-init
#查看运行中的容器
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56dd5fb4e962 nginx "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:8800->80/tcp jolly_chatterjee
#进入容器:CONTAINER ID=56dd5fb4e962 并打开一个终端
[root@192 ~]# docker container exec -it 56dd5fb4e962 bash
#在容器中,查看文件系统
root@56dd5fb4e962:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
#查看操作系统
root@56dd5fb4e962:/# cat /etc/issue
Debian GNU/Linux 9 \n \l
#debian系统用的是apt软件管理包
root@56dd5fb4e962:/# apt
apt 1.4.9 (amd64)
Usage: apt [options] command
apt is a commandline package manager and provides commands for
searching and managing as well as querying information about packages.
It provides the same functionality as the specialized APT tools,
like apt-get and apt-cache, but enables options more suitable for
interactive use by default.
Most used commands:
list - list packages based on package names
search - search in package descriptions
show - show package details
install - install packages
remove - remove packages
autoremove - Remove automatically all unused packages
update - update list of available packages
upgrade - upgrade the system by installing/upgrading packages
full-upgrade - upgrade the system by removing/installing/upgrading packages
edit-sources - edit the source information file
See apt(8) for more information about the available commands.
Configuration options and syntax is detailed in apt.conf(5).
Information about how to configure sources can be found in sources.list(5).
Package and version choices can be expressed via apt_preferences(5).
Security details are available in apt-secure(8).
This APT has Super Cow Powers.
#退出容器
root@56dd5fb4e962:/# exit
exit
#查看docker帮助
[root@192 ~]# 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@192 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 881bd08c0b08 6 days ago 109MB
#列出容器
[root@192 ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56dd5fb4e962 nginx "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:8800->80/tcp jolly_chatterjee
#显示nginx镜像详细信息
[root@192 ~]# docker image inspect nginx
[
{
"Id": "sha256:881bd08c0b08234bd19136957f15e4301097f4646c1e700f7fea26e41fc40069",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:b878833bf4fc397c8c681c0d59b47a339935c2d99692148077b27f406a1c78be"
],
"Parent": "",
"Comment": "",
"Created": "2019-03-05T04:34:40.803983958Z",
"Container": "82067b49ee8c4bc91d386798148f1f2cfdb0ff39076873c8d40e1e1baf521eb7",
"ContainerConfig": {
"Hostname": "82067b49ee8c",
"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.15.9-1~stretch",
"NJS_VERSION=1.15.9.0.2.8-1~stretch"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"nginx\" \"-g\" \"daemon off;\"]"
],
"ArgsEscaped": true,
"Image": "sha256:4a9060e0ac21710c70f99ec2fb604b1c2847ced2acf10e4d4e86905d554dc685",
"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.15.9-1~stretch",
"NJS_VERSION=1.15.9.0.2.8-1~stretch"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"ArgsEscaped": true,
"Image": "sha256:4a9060e0ac21710c70f99ec2fb604b1c2847ced2acf10e4d4e86905d554dc685",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGTERM"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 109252443,
"VirtualSize": 109252443,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/3ad0bc68ee6c3e92492a5d56b107da1834e0f22c8849016604b5110f8c67c8c8/diff:/var/lib/docker/overlay2/772eafb718462ae983f10fd78795b4fe73b46640e7e956ffb9571eee3ef85ea0/diff",
"MergedDir": "/var/lib/docker/overlay2/c04bd3d232d46217bc57de00dc47b3bad9d9aa478398b41cfeae3a187092610d/merged",
"UpperDir": "/var/lib/docker/overlay2/c04bd3d232d46217bc57de00dc47b3bad9d9aa478398b41cfeae3a187092610d/diff",
"WorkDir": "/var/lib/docker/overlay2/c04bd3d232d46217bc57de00dc47b3bad9d9aa478398b41cfeae3a187092610d/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:6744ca1b11903f4db4d5e26145f6dd20f9a6d321a7f725f1a0a7a45a4174c579",
"sha256:c59b3ca455e3e3fc50d0102af9e4cb585aa6ce806aa9ed2db536c9525df3ca89",
"sha256:3e9eb35b1c23abd3b0852f16454425f78ff65dee17d4956e1dbd08202d36d9b2"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
#从镜像仓库拉取 镜像名=busybox的镜像(如果本地存在,就做更新操作)
[root@192 ~]# docker image pull busybox
Using default tag: latest
latest: Pulling from library/busybox
Digest: sha256:506f440802e1dc578a9953dd0957a48caeb6a4008df9af04a75d9e184aa01006
Status: Downloaded newer image for busybox:latest
#查看镜像,多了一个busybox说明拉取成功
[root@192 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 881bd08c0b08 6 days ago 109MB
busybox latest d8233ab899d4 3 weeks ago 1.2MB
#显示busybox镜像详细信息
[root@192 ~]# docker image inspect busybox
[
{
"Id": "sha256:d8233ab899d419c58cf3634c0df54ff5d8acc28f8173f09c21df4a07229e1205",
"RepoTags": [
"busybox:latest"
],
"RepoDigests": [
"busybox@sha256:506f440802e1dc578a9953dd0957a48caeb6a4008df9af04a75d9e184aa01006"
],
"Parent": "",
"Comment": "",
"Created": "2019-02-15T00:19:37.830935034Z",
"Container": "197cb47b0e98a00daefcb62c5fa84634792dd22f11aa29bc95fdd4e10d654d30",
"ContainerConfig": {
"Hostname": "197cb47b0e98",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"sh\"]"
],
"ArgsEscaped": true,
"Image": "sha256:896f6e65107acffcae30fe593503aebf407fc30ad4566a8db04921dbfb6c0721",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "18.06.1-ce",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"sh"
],
"ArgsEscaped": true,
"Image": "sha256:896f6e65107acffcae30fe593503aebf407fc30ad4566a8db04921dbfb6c0721",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"Architecture": "amd64",
"Os": "linux",
"Size": 1199417,
"VirtualSize": 1199417,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/1ce297b1d409c6acbb819285fe10dd3b92b4f3059c0c3f5ef145d103b8538512/merged",
"UpperDir": "/var/lib/docker/overlay2/1ce297b1d409c6acbb819285fe10dd3b92b4f3059c0c3f5ef145d103b8538512/diff",
"WorkDir": "/var/lib/docker/overlay2/1ce297b1d409c6acbb819285fe10dd3b92b4f3059c0c3f5ef145d103b8538512/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:adab5d09ba79ecf30d3a5af58394b23a447eda7ffffe16c500ddc5ccb4c0222f"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
#移除未使用的镜像,没有被标记或者没有被任何容器引用
[root@192 ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
[root@192 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 881bd08c0b08 6 days ago 109MB
busybox latest d8233ab899d4 3 weeks ago 1.2MB
[root@192 ~]# ls
anaconda-ks.cfg
#保存nginx镜像到nginx.tar文件(适用场景:直接以这种方式传播镜像文件,在另一个docker环境直接导入就可使用)
[root@192 ~]# docker image save nginx >nginx.tar
#查看nginx.tar文件大小
[root@192 ~]# du -sh nginx.tar
108M nginx.tar
#查看容器, 当前的容器 STATUS=UP ,说明是运行中,可以通过浏览器 http://<docker主机ip>:8800 访问tomcat服务
[root@192 ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56dd5fb4e962 nginx "nginx -g 'daemon of…" 4 hours ago Up 4 hours 0.0.0.0:8800->80/tcp jolly_chatterjee
[root@192 ~]# docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
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
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
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
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
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
#停止CONTAINER ID=56dd5fb4e962的容器
[root@192 ~]# docker container stop 56dd5fb4e962
56dd5fb4e962
#删除CONTAINER ID=56dd5fb4e962的容器
[root@192 ~]# docker container rm 56dd5fb4e962
56dd5fb4e962
#列出容器
[root@192 ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#列出镜像
[root@192 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 881bd08c0b08 6 days ago 109MB
busybox latest d8233ab899d4 3 weeks ago 1.2MB
#删除nginx镜像
[root@192 ~]# docker image rm nginx
Untagged: nginx:latest
Untagged: nginx@sha256:b878833bf4fc397c8c681c0d59b47a339935c2d99692148077b27f406a1c78be
Deleted: sha256:881bd08c0b08234bd19136957f15e4301097f4646c1e700f7fea26e41fc40069
Deleted: sha256:39d647657f07356aed647e68914109c3098d4bb8ace1bc1d3a09bb40c8766971
Deleted: sha256:798ab02dcf5760bbb0f5885cbaf22bac887e7c1cf3c64fc8864e98630e426aea
Deleted: sha256:6744ca1b11903f4db4d5e26145f6dd20f9a6d321a7f725f1a0a7a45a4174c579
#列出镜像,确认nginx镜像被删除
[root@192 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest d8233ab899d4 3 weeks ago 1.2MB
# 从nginx.tar文件,加载一个镜像到本地
[root@192 ~]# docker image load <nginx.tar
Loaded image: nginx:latest
# 列出镜像 确认nginx镜像已经被导入
[root@192 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 881bd08c0b08 6 days ago 109MB
busybox latest d8233ab899d4 3 weeks ago 1.2MB
作者: 梅梅~
出处: https://www.cnblogs.com/keeptesting
关于作者:专注软件测试,测试运维相关工作,请多多赐教!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题,可加微信联系。 微信:yangguangkg20140901 暗号:博客园.