- Published on
Docker-常用命令
一、 Docker 管理命令
config Manage Docker configs --管理docker的配置
container Manage containers --管理容器
context Manage contexts --管理上下文
image Manage images --管理镜像
network Manage networks --管理网络
node Manage Swarm nodes --管理集群节点swarm nodes
plugin Manage plugins --管理docker插件
secret Manage Docker secrets --管理docker安全组件(密码之类的)
service Manage services --管理Swarm编排中的服务
stack Manage Docker stacks --管理docker部署
swarm Manage Swarm --管理集群
system Manage Docker --管理docker(磁盘空间、系统事件event等)
trust Manage trust on Docker images --docker镜像中的内容信任
volume Manage volumes --管理磁盘卷
attach Attach local standard input, output, and error streams to a running container --分离模式
build Build an image from a Dockerfile --通过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 --从tarball导出内容到文件系统镜像
info Display system-wide information --显示docker 系统层信息
inspect Return low-level information on Docker objects --显示docker对象的底层信息
kill Kill one or more running containers --杀死容器
load Load an image from a tar archive or STDIN --载入一个镜像
login Log in to a Docker registry --登入docker仓库
logout Log out from a Docker registry --退出登录仓库
logs Fetch the logs of a container --获取容器的log信息
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 --从仓库中pull一个镜像
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) --保存镜像到tar文件
search Search the Docker Hub for images --到docker hub中搜索镜像
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 --给镜像创建一个tag标签
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 --显示docker版本信息
wait Block until one or more containers stop, then print their exit codes --阻塞到某个容器停止
attach container attach 附加到一个运行的容器
build image build 从一个Dockerfile构建镜像
commit container commit 从一个容器的修改创建一个新的镜像
cp container cp 在容器与本地文件系统之间复制文件/文件夹
create container create 创建新的容器
diff container diff 检阅一个容器文件系统的修改
events system events 获取服务器的实时时间
exec container exec 在运行的容器内执行命令
export container export 打包一个容器文件系统到tar文件
history image history 展示镜像历史信息
images image ls 展示镜像列表
import image import 用tar文件导入并创建镜像文件
info system info 展示整个系统信息
inspect container inspect 展示一个容器/镜像或者任务的底层信息
kill container kill 终止一个或者多个运行中的容器
load image load 从tar文件或者标准输入载入镜像
login login 登录Docker registry
logout logout 从Docker registry登出
logs container logs 获取容器的日志
network network 管理Docker网络
node node 管理Docker Swarm节点
pause container pause 暂停一个或者多个容器的所有进程
port container port 展示容器的端口映射
ps container ls 展示容器列表
pull image pull 从某个registry拉取镜像或者仓库
push image push 推送镜像或者仓库到某个registry
rename container rename 重命名容器
restart container restart 重启容器
rm container rm 移除一个或多个容器
rmi image rm 移除一个或多个镜像
run container run 运行一个新的容器
save image save 打包一个或多个镜像到tar文件(默认是到标准输出)
search search 在Docker Hub搜索镜像
service service 管理Docker services
start container start 启动一个或者多个容器
stats container stats 获取容器的实时资源使用统计
stop container stop 停止一个或多个运行容器
swarm swarm 管理Docker Swarm
tag image tag 标记一个镜像到仓库
top container top 展示容器运行进程
unpause container unpause 解除暂停一个或多个容器的所有进程
update container update 更新一个或多个容器的配置
version version 显示Docker版本信息
volume volume 管理Docker volumes
wait container wait 阻塞直到容器停止,然后打印退出代码
二、 Docker 镜像管理命令
2.1 镜像管理帮助文档
[root@iZ2ze7zn4sk5icvlnhwj5yZ ~]# 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.
2.2 下载镜像(新增)
[root@node1 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
说明:
- nginx:镜像名
- latest:镜像版本
2.3.查看镜像列表(列表查询)
[root@node1 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 weeks ago 141MB
说明:
- REPOSITORY:表示镜像的仓库源
- TAG:镜像的标签
- IMAGE ID:镜像ID
- CREATED:镜像创建时间
- SIZE:镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。
2.4.运行容器
[root@node1 ~]# docker run -itd nginx:latest
b745ef44b7c52346716b0447813467ccb7dcca41a1fe21ee262f29e4be6bae8e
说明:
- -i:交互式操作
- -t:终端
- -d: 守护进程运行
- nginx:latest:镜像
2.5.查看镜像详情(详情查询)
docker image inspect 605c77e624dd
[
{
"Id": "sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31"
],
"Parent": "",
"Comment": "",
"Created": "2021-12-29T19:28:29.892199479Z",
"Container": "ca3e48389f7160bc9d9a892d316fcbba459344ee3679998739b1c3cd8e56f7da",
"ContainerConfig": {
"Hostname": "ca3e48389f71",
"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.21.5",
"NJS_VERSION=0.7.1",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"nginx\" \"-g\" \"daemon off;\"]"
],
"Image": "sha256:82941edee2f4d17c55563bb926387c3ae39fa1a99777f088bc9d3db885192209",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"DockerVersion": "20.10.7",
"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.21.5",
"NJS_VERSION=0.7.1",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "sha256:82941edee2f4d17c55563bb926387c3ae39fa1a99777f088bc9d3db885192209",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 141455520,
"VirtualSize": 141455520,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/04382c25326d6e816ad029a7c77561eb2374a0bf2bac4929f961ac3b29700b17/diff:/var/lib/docker/overlay2/234a1d8e168648f23f6b8e48e44383b9b8d8814d2078d07b034cb94b16873fdd/diff:/var/lib/docker/overlay2/5a0474a4ed05b22cb012a7531be4d42565d2f8f2a0969b184921cd6022a912bb/diff:/var/lib/docker/overlay2/6d99c765ce728a4be22c90d48d9e24a8b03fd6c069c4d5a002bddac1798c90af/diff:/var/lib/docker/overlay2/9c1857f4104cc98bcdce831a4e2b68fef76dfc59ee4d285b1f1539c2614da693/diff",
"MergedDir": "/var/lib/docker/overlay2/c1c8aa43cd75610b20baa0be45185cbe7ba1e66de6cc89d6c6a9450ec1e5f868/merged",
"UpperDir": "/var/lib/docker/overlay2/c1c8aa43cd75610b20baa0be45185cbe7ba1e66de6cc89d6c6a9450ec1e5f868/diff",
"WorkDir": "/var/lib/docker/overlay2/c1c8aa43cd75610b20baa0be45185cbe7ba1e66de6cc89d6c6a9450ec1e5f868/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f",
"sha256:e379e8aedd4d72bb4c529a4ca07a4e4d230b5a1d3f7a61bc80179e8f02421ad8",
"sha256:b8d6e692a25e11b0d32c5c3dd544b71b1085ddc1fddad08e68cbd7fda7f70221",
"sha256:f1db227348d0a5e0b99b15a096d930d1a69db7474a1847acbc31f05e4ef8df8c",
"sha256:32ce5f6a5106cc637d09a98289782edf47c32cb082dc475dd47cbf19a4f866da",
"sha256:d874fd2bc83bb3322b566df739681fbd2248c58d3369cb25908d68e7ed6040a6"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
说明:
- ContainerConfig:容器配置
- GraphDiver:镜像驱动
- RootFS:Docker文件系统
2.6.查看镜像构建历史(历史查询)
[root@node1 ~]# docker image history 605c77e624dd
IMAGE CREATED CREATED BY SIZE COMMENT
605c77e624dd 2 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
<missing> 2 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:09a214a3e07c919a… 4.61kB
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7… 1.04kB
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0… 1.96kB
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:65504f71f5855ca0… 1.2kB
<missing> 2 weeks ago /bin/sh -c set -x && addgroup --system -… 61.1MB
<missing> 2 weeks ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~bullseye 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV NJS_VERSION=0.7.1 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.21.5 0B
<missing> 3 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
<missing> 3 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:09675d11695f65c55… 80.4MB
2.7.删除镜像容器
[root@node1 ~]# docker rm 605c77e624dd
Untagged: nginx:latest
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
2.8.删除镜像
[root@node1 ~]# docker rmi 605c77e624dd
[root@node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 weeks ago 141MB
nginx 1.15.0 5699ececb21c 3 years ago 109MB
[root@node1 ~]#
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node1 ~]#
[root@node1 ~]# 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@node1 ~]#
[root@node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 weeks ago 141MB
nginx 1.15.0 5699ececb21c 3 years ago 109MB
[root@node1 ~]#
[root@node1 ~]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: nginx:latest
untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
untagged: nginx:1.15.0
untagged: nginx@sha256:62a095e5da5f977b9f830adaf64d604c614024bf239d21068e4ca826d0d629a4
deleted: sha256:5699ececb21caf07b92cbda9daa1e965407e3793a72000ecbf6b8e8595a0824a
deleted: sha256:c34974813e70c230445629bfb7d58dfff99440d9e11fd8aa4230a50c6cb101f3
deleted: sha256:85dd24921f1b33cd733671fbe604c4630899a19ef3a1822041f4b298636b79ca
deleted: sha256:9c46f426bcb704beffafc951290ee7fe05efddbc7406500e7d0a3785538b8735
Total reclaimed space: 250.4MB
2.9.备份镜像
[root@node1 ~]# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
[root@node1 ~]#
[root@node1 ~]# docker save -o nginx.tar nginx:latest
[root@node1 ~]# ls -lhtr
total 140M
drwxr-xr-x 3 root root 4.0K Jan 6 12:12 erikguan
-rw------- 1 root root 140M Jan 17 17:01 nginx.tar
2.10.加载镜像
[root@node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@node1 ~]#
[root@node1 ~]# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
[root@node1 ~]#
[root@node1 ~]# docker image load -i nginx.tar
2edcec3590a4: Loading layer [==================================================>] 83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>] 62MB/62MB
b8d6e692a25e: Loading layer [==================================================>] 3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>] 4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>] 3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>] 7.168kB/7.168kB
Loaded image: nginx:latest
[root@node1 ~]#
[root@node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 weeks ago 141MB
2.11.导入镜像
[root@node1 ~]# docker rmi nginx:latest
Untagged: nginx:latest
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
[root@node1 ~]#
[root@node1 ~]# docker image import nginx.tar
sha256:960f878310ce342fea20730a50a7969b78378fa3f5e68e29a0a23d396ac8ffa5
[root@node1 ~]#
[root@node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 960f878310ce 6 seconds ago 146MB
2.12.镜像构建
[root@node1 dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@node1 dockerfile]#
[root@node1 dockerfile]# docker build -t erikguan/nginx .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM nginx
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
---> 605c77e624dd
Step 2/2 : RUN echo '这是一个本地构建的nginx镜像' > /usr/share/nginx/html/index.html
---> Running in 63a47232fed6
Removing intermediate container 63a47232fed6
---> 69a98baf817f
Successfully built 69a98baf817f
Successfully tagged erikguan/nginx:latest
[root@node1 dockerfile]#
[root@node1 dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
erikguan/nginx latest 69a98baf817f 7 seconds ago 141MB
nginx latest 605c77e624dd 2 weeks ago 141MB
说明:
- -t :指定要创建的目标镜像名
- . :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径终端
2.13.给镜像打Tag
[root@node1 dockerfile]# docker image tag --help
Usage: docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
[root@node1 dockerfile]#
[root@node1 dockerfile]# docker image tag dc/nginx:v2 dc/nginx:v3
[root@node1 dockerfile]#
[root@node1 dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dc/nginx latest 69a98baf817f 2 minutes ago 141MB
dc/nginx v2 69a98baf817f 2 minutes ago 141MB
dc/nginx v3 69a98baf817f 2 minutes ago 141MB
nginx latest 605c77e624dd 2 weeks ago 141MB
2.14.推送镜像仓库
[root@node1 dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dc/nginx latest 69a98baf817f 3 minutes ago 141MB
dc/nginx v2 69a98baf817f 3 minutes ago 141MB
dc/nginx v3 69a98baf817f 3 minutes ago 141MB
nginx latest 605c77e624dd 2 weeks ago 141MB
[root@node1 dockerfile]#
[root@node1 dockerfile]# docker push --help
Usage: docker push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
Options:
-a, --all-tags Push all tagged images in the repository
--disable-content-trust Skip image signing (default true)
-q, --quiet Suppress verbose output
[root@node1 dockerfile]#
[root@node1 dockerfile]#
[root@node1 dockerfile]# docker push dc/nginx:v3
The push refers to repository [docker.io/dc/nginx]
96e4ba58d7f7: Pushed
d874fd2bc83b: Layer already exists
32ce5f6a5106: Layer already exists
f1db227348d0: Layer already exists
b8d6e692a25e: Layer already exists
e379e8aedd4d: Layer already exists
2edcec3590a4: Layer already exists
v3: digest: sha256:2f6b859aa099e30ea614f6d479e6619735c68adecff93dfddd768d6dc4a46210 size: 1777
三、 Docker 容器管理命令
3.1 运行容器
[root@node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 weeks ago 141MB
[root@node1 ~]#
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node1 ~]#
[root@node1 ~]# docker run -itd nginx:latest
c37bd4c81224f3130e332077a3deaaa876d77b9f1ba2b53972097fe2ebe9df94
[root@node1 ~]#
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c37bd4c81224 nginx:latest "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp gracious_snyder
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]# docker run -itd nginx:latest nginx_container
8f113314689ba3be619fd90e51dbd46719aec3e57c746596ddc1325e0e2573bd
[root@node1 ~]#
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c37bd4c81224 nginx:latest "/docker-entrypoint.…" 34 seconds ago Up 34 seconds 80/tcp gracious_snyder
3.2 创建一个容器
[root@node1 ~]# docker create nginx:latest
e2073a6f19847af4ec9af0b1b6c1315d3d17aabd7c7f689d53b9e9ed30939f7e
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c37bd4c81224 nginx:latest "/docker-entrypoint.…" 39 minutes ago Up 29 minutes 80/tcp nginx_container
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2073a6f1984 nginx:latest "/docker-entrypoint.…" 8 seconds ago Created clever_jennings
c37bd4c81224 nginx:latest "/docker-entrypoint.…" 39 minutes ago Up 29 minutes 80/tcp nginx_container
3.3 查看容器列表
[root@node1 ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c37bd4c81224 nginx:latest "/docker-entrypoint.…" 15 minutes ago Up 5 minutes 80/tcp nginx_container
3.4 停止容器
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c37bd4c81224 nginx:latest "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp gracious_snyder
[root@node1 ~]#
[root@node1 ~]# docker stop c37bd4c81224
c37bd4c81224
[root@node1 ~]#
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node1 ~]#
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6b5241926ff nginx:latest "/docker-entrypoint.…" 3 minutes ago Exited (127) 3 minutes ago stoic_chaplygin
8f113314689b nginx:latest "/docker-entrypoint.…" 3 minutes ago Exited (127) 3 minutes ago bold_torvalds
c37bd4c81224 nginx:latest "/docker-entrypoint.…" 3 minutes ago Exited (0) 10 seconds ago gracious_snyder
[root@node1 ~]#
3.5 启动容器
[root@node1 ~]# docker start c37bd4c81224
c37bd4c81224
[root@node1 ~]#
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c37bd4c81224 nginx:latest "/docker-entrypoint.…" 5 minutes ago Up 3 seconds 80/tcp gracious_snyder