docker官方命令文档
菜鸟dockerm命令大全仅仅是个人的课程实验报告,学习Docker还请参考其他更专业的文档。
云计算实验报告
专 业: 数据科学与大数据技术 班 级: 数据Q201
实验1:Docker镜像容器命令测试
实验设备:
OS: Ubuntu/Centos
应用软件:Docker
实验目的:
1.掌握部分镜像与容器命令
2.Docker注册中心的使用与操作
实验内容:
1、测试Docker镜像与容器命令
2、测试Docker注册中心的用法
实验报告要求:
以截图的形式显示代码运行结果、验证操作是否成功。
实验结果:
1.1、Docker的常用命令
帮助命令:
docker version #显示docker的版本信息
[root@lh2022 Docker]# docker -v
Docker version 24.0.7, build afdd53b
[root@lh2022 Docker]# docker version
Client: Docker Engine - Community
Version: 24.0.7
API version: 1.43
Go version: go1.20.10
Git commit: afdd53b
Built: Thu Oct 26 09:11:35 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 24.0.7
API version: 1.43 (minimum version 1.12)
Go version: go1.20.10
Git commit: 311b9ff
Built: Thu Oct 26 09:10:36 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.24
GitCommit: 61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
runc:
Version: 1.1.9
GitCommit: v1.1.9-0-gccaecfc
docker-init:
Version: 0.19.0
GitCommit: de40ad0
docker info #显示docker的系统信息,包括镜像和容器的数量
[root@lh2022 Docker]# docker info
Client: Docker Engine - Community
Version: 24.0.7
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.11.2
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.21.0
Path: /usr/libexec/docker/cli-plugins/docker-compose
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 24.0.7
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
runc version: v1.1.9-0-gccaecfc
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 3.10.0-693.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 1.781GiB
Name: lh2022
ID: fb7e86a2-fd72-44f2-9033-6e476d82aa8e
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://d45q22s3.mirror.aliyuncs.com/
Live Restore Enabled: false
docker --help #万能命令,查阅命令详情
[root@lh2022 Docker]# docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Common Commands:
run Create and run a new container from an image
exec Execute a command in a running container
ps List containers
build Build an image from a Dockerfile
pull Download an image from a registry
push Upload an image to a registry
images List images
login Log in to a registry
logout Log out from a registry
search Search Docker Hub for images
version Show the Docker version information
info Display system-wide information
Management Commands:
builder Manage builds
buildx* Docker Buildx (Docker Inc., v0.11.2)
compose* Docker Compose (Docker Inc., v2.21.0)
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
plugin Manage plugins
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Swarm Commands:
swarm Manage Swarm
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
events Get real time events from the server
export Export a container's filesystem as a tar archive
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
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
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
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
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
wait Block until one or more containers stop, then print their exit codes
Global 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 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
Run 'docker COMMAND --help' for more information on a command.
For more help on how to use Docker, head to https://docs.docker.com/go/guides/
docker具体命令–help # 查看docker命令帮助文档
[root@lh2022 Docker]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Aliases:
docker container ls, docker container list, docker container ps, docker ps
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Format output using a custom template:
'table': Print output in table format with column
headers (default)
'table TEMPLATE': Print output in table format using the given
Go template
'json': Print in JSON format
'TEMPLATE': Print output using the given Go template.
Refer to https://docs.docker.com/go/formatting/ for more
information about formatting output with templates
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
镜像命令:
docker images [options] #查看所有本地镜像
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 23 months ago 448MB
mysql latest 3218b38490ce 23 months ago 516MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
各个选项说明:
- REPOSITORY:表示镜像的仓库源
- TAG:镜像的标签版本号
- IMAGE ID:镜像ID
- CREATED:镜像创建时间
- SIZE:镜像大小
同一仓库源可以有多个TAG版本,代表这个仓库源的不同个版本,使用 REPOSITORY:TAG 来定义不同的镜像。
用–help命令查看该命令的详细使用方法。
[root@lh2022 Docker]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Format output using a custom template:
'table': Print output in table format with column
headers (default)
'table TEMPLATE': Print output in table format using the given
Go template
'json': Print in JSON format
'TEMPLATE': Print output using the given Go template.
Refer to https://docs.docker.com/go/formatting/ for more
information about formatting output with templates
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
Options
-a, --all #列出所有的镜像
-q, --quite #只显示镜像的id
[root@lh2022 Docker]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 23 months ago 448MB
mysql latest 3218b38490ce 23 months ago 516MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
[root@lh2022 Docker]# docker images -q
c20987f18b13
3218b38490ce
feb5d9fea6a5
docker search #搜索镜像
docker search 镜像名(搜索全部相关镜像)
[root@lh2022 Docker]# docker search hello-world
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
hello-world Hello World! (an example of minimal Dockeriz… 2137 [OK]
rancher/hello-world This container image is no longer maintained… 4
okteto/hello-world 0
tacc/hello-world 0
armswdev/c-hello-world Simple hello-world C program on Alpine Linux… 0
用 --help 命令查看该命令的详细使用方法。
[root@lh2022 Docker]# docker search --help
Usage: docker search [OPTIONS] TERM
Search Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results
--no-trunc Don't truncate output
docker search 镜像名 -f 过滤条件 #搜索符合过滤条件的镜像 f=filter 即过滤器。
搜索吗hello-world 镜像的 stats>=3000的镜像。
[root@lh2022 Docker]# docker search hello-world --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
搜索stats>=2000的镜像。
[root@lh2022 Docker]# docker search hello-world --filter=STARS=2000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
hello-world Hello World! (an example of minimal Dockeriz… 2137 [OK]
[root@lh2022 Docker]#
docker pull #下载镜像
docker pull 镜像名 #下载指定镜像,默认下载最新版本
docker pull 镜像名:版本号 #下载指定版本的镜像
默认下载最新版本的mysql。
[root@lh2022 Docker]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
下载指定版本的镜像。
[root@lh2022 Docker]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
查看本地所以镜像,确认刚才的操作拉取成功。
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 23 months ago 448MB
mysql latest 3218b38490ce 23 months ago 516MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
docker rmi #删除镜像
docker rmi -f IMAGE ID #根据镜像的编号镜像删除,-f :force(强制)删除
[root@lh2022 Docker]# docker rmi -f feb5d9fea6a5
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 23 months ago 448MB
mysql latest 3218b38490ce 23 months ago 516MB
docker rmi -f $(docker images -aq) #批量删除镜像
[root@lh2022 Docker]# docker rmi $(docker images -aq)
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@lh2022 Docker]#
docker system df # 查看镜像/容器/数据卷所占的空间
[root@lh2022 Docker]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 0 0 0B 0B
Containers 0 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
容器命令:
docker pull 镜像名
有镜像才可以创建容器。之前批量删除了镜像,现在需要再拉取一些镜像:
docker pull centos
docker pull ubuntu
分别拉取最新版本的centos镜像和ubuntu镜像。
[root@lh2022 Docker]# docker pull 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@lh2022 Docker]# 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@lh2022 Docker]#
查看镜像,拉取成功。
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
新建容器并启动
docker run [可选参数] image #启动容器
参数说明
- –name=“Name”容器名字
- -d后台方式运行
- -it使用交互方式运行,进入容器查看内容
- -p随机指定端口\ -p主机端口︰容器端口 -p容器端口
第一次尝试创建并启动容器,查看目录。
[root@lh2022 Docker]# docker run -it centos /bin/bash
[root@930c0fbc66fb /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@930c0fbc66fb /]# exit
exit
[root@lh2022 Docker]# docker run -it ubuntu /bin/bash
root@0091bc9b9ff3:/# ls
bin dev home lib32 libx32 mnt proc run srv tmp var
boot etc lib lib64 media opt root sbin sys usr
root@0091bc9b9ff3:/# exit
exit
docker ps #查看运行中的容器
docker ps -a #查看所有的容器(运行中的容器+运行过的容器)
docker ps -q #查看运行中的容器Id
docker ps -aq #查看所有的容器Id
查看运行中的容器。
[root@lh2022 Docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@lh2022 Docker]#
第二次创建并启动容器,且centos容器使用“Ctrl + Q + P”方式退出容器。Ubuntu容器直接“exit”退出容器。
[root@lh2022 Docker]# docker run -it centos /bin/bash
[root@d5268f5413d3 /]# [root@lh2022 Docker]#
[root@lh2022 Docker]# docker run -it ubuntu /bin/bash
root@566457c9d521:/# exit
exit
[root@lh2022 Docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d5268f5413d3 centos "/bin/bash" 2 minutes ago Up 2 minutes agitated_kapitsa
[root@lh2022 Docker]#
发现运行中的容器中有centos而没有Ubuntu,是因为他们的退出方式不同。这两种退出容器方式的区别:
- exit :退出容器,直接停止容器并退出;
- Ctrl + Q + P :退出容器,容器不停止运行。
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
566457c9d521 ubuntu "/bin/bash" About a minute ago Exited (0) About a minute ago laughing_leavitt
2882d1edd0ac ubuntu "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago quirky_blackwell
87494ab4a5c9 ubuntu "/bin/bash" 3 minutes ago Exited (0) 3 minutes ago tender_carver
d5268f5413d3 centos "/bin/bash" 4 minutes ago Up 4 minutes agitated_kapitsa
0091bc9b9ff3 ubuntu "/bin/bash" 10 minutes ago Exited (0) 9 minutes ago affectionate_pare
930c0fbc66fb centos "/bin/bash" 10 minutes ago Exited (0) 10 minutes ago pensive_franklin
[root@lh2022 Docker]#
docker rm 容器id #删除容器
删除指定的容器不能删除正在运行的容器,如果要强制删除利用 -f “docker rm -f 容器id ”强制删除容器。
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a -q | xargs docker rm #首先通过docker ps -a -q获取所有容器的ID列表,然后将这些ID列表作为参数传递给docker rm命令,以此来删除所有的容器。
注:xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。
查看运行中的容器id,并删除容器。
[root@lh2022 Docker]# docker ps -q
d5268f5413d3
[root@lh2022 Docker]# docker rm d5268f5413d3
Error response from daemon: You cannot remove a running container d5268f5413d3f1c7042fdf366a9257454ac827e0260a9195bbd2e6e9e475dc6a. Stop the container before attempting removal or force remove
[root@lh2022 Docker]# docker rm -f d5268f5413d3
d5268f5413d3
[root@lh2022 Docker]# docker ps -q
[root@lh2022 Docker]#
发现正常的删除语句不能删除正在运行的容器,但是加上-f就可以强制删除成功。
删除所有的容器。
[root@lh2022 Docker]# docker rm -f $(docker ps -aq)
566457c9d521
2882d1edd0ac
87494ab4a5c9
0091bc9b9ff3
930c0fbc66fb
[root@lh2022 Docker]#
创建一个新容器用后续容器的启动和停止操作。
[root@lh2022 Docker]# docker run -it ubuntu /bin/bash
root@23ba1c7ae31e:/# exit
exit
[root@lh2022 Docker]# docker ps -aq
23ba1c7ae31e
docker start 容器id #启动容器
docker stop 容器id #停止当前正在运行的容器
docker restart 容器id #重启容器
docker kill 容器id #停止当前正在运行的容器
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23ba1c7ae31e ubuntu "/bin/bash" 4 minutes ago Exited (0) 4 minutes ago optimistic_agnesi
[root@lh2022 Docker]# docker start 23ba1c7ae31e
23ba1c7ae31e
[root@lh2022 Docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23ba1c7ae31e ubuntu "/bin/bash" 4 minutes ago Up 5 seconds optimistic_agnesi
[root@lh2022 Docker]# docker stop 23ba1c7ae31e
23ba1c7ae31e
[root@lh2022 Docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@lh2022 Docker]# docker restart 23ba1c7ae31e
23ba1c7ae31e
[root@lh2022 Docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23ba1c7ae31e ubuntu "/bin/bash" 5 minutes ago Up 1 second optimistic_agnesi
[root@lh2022 Docker]# docker kill 23ba1c7ae31e
23ba1c7ae31e
[root@lh2022 Docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@lh2022 Docker]#
观察使用命令后的"STATUS "。
docker attach 容器名 #进入正在运行的容器
创建一个名叫ub1的容器,用“Ctrl + Q + P”退出容器但是不停止运行。再用docker attach:主要的作用是进入正在运行的容器。
[root@lh2022 Docker]# docker run -it --name ub1 ubuntu
root@16683ac148d2:/# ls
bin dev home lib32 libx32 mnt proc run srv tmp var
boot etc lib lib64 media opt root sbin sys usr
root@16683ac148d2:/# read escape sequence
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
16683ac148d2 ubuntu "bash" About a minute ago Up About a minute ub1
23ba1c7ae31e ubuntu "/bin/bash" 11 hours ago Exited (0) 11 hours ago optimistic_agnesi
[root@lh2022 Docker]# docker attach ub1
root@16683ac148d2:/# exit
exit
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
16683ac148d2 ubuntu "bash" 2 minutes ago Exited (0) 4 seconds ago ub1
23ba1c7ae31e ubuntu "/bin/bash" 11 hours ago Exited (0) 11 hours ago optimistic_agnesi
[root@lh2022 Docker]#
docker commit : 主要功能为当前容器提交打包为镜像
- -a:添加作者信息;
- -c:修改Dockerfile指令-m:提交修改说明;
- -p:在commit时,将容器暂停;
- -m:提交时的说明文字;
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
16683ac148d2 ubuntu "bash" 12 minutes ago Exited (0) 10 minutes ago ub1
23ba1c7ae31e ubuntu "/bin/bash" 11 hours ago Exited (0) 11 hours ago optimistic_agnesi
[root@lh2022 Docker]# docker commit -a "lh" -m "my ubuntu,tag=v1 " 16683ac148d2 myubuntu:v1
sha256:abfa3ff80204b3268c8756339627651dc2a2173bdce023eb7f2d4d7e17c1291a
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu v1 abfa3ff80204 8 seconds ago 72.8MB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@lh2022 Docker]#
docker cp 命令
作用:类似于Linux中的cp,用于在宿主机和容器之间移动复制文件
例子:把容器中的c_test.txt文件复制到宿主机中。
1、创建容器;
2、启动并进入容器;
3、在容器中/home/目录下创建c_test.txt;
4、使用cp命令完成复制。
实验环境:
主机本地/root/Docker下:
[root@lh2022 Docker]# pwd
/root/Docker
[root@lh2022 Docker]# ls
2000
进入容器:创建文件后退出容器但不停止(CTRL+P+Q)。
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
16683ac148d2 ubuntu "bash" 21 minutes ago Exited (0) 3 seconds ago ub1
23ba1c7ae31e ubuntu "/bin/bash" 11 hours ago Exited (0) 11 hours ago optimistic_agnesi
[root@lh2022 Docker]# docker start ub1
ub1
[root@lh2022 Docker]# docker attach ub1
root@16683ac148d2:/# cd /home
root@16683ac148d2:/home# ls
root@16683ac148d2:/home# echo "here is container~" > c_test.txt
root@16683ac148d2:/home# ls
c_test.txt
root@16683ac148d2:/home# cat c_test.txt
here is container~
root@16683ac148d2:/home# read escape sequence
[root@lh2022 Docker]#
在主机使用docker cp 命令。
[root@lh2022 Docker]# docker cp ub1:/home/c_test.txt /root/Docker/
Successfully copied 2.05kB to /root/Docker/
[root@lh2022 Docker]# ls
2000 c_test.txt
[root@lh2022 Docker]# cat c_test.txt
here is container~
[root@lh2022 Docker]#
可以发现本来在主机/root/Docker 目录下是没有c_test.txt文件的,但是使用docker cp命令后,成功将容器中/home/c_test.txt复制到宿主机/root/Docker 目录下。
在宿主机中创建文件h_test.txt,使用cp命令将文件复制到容器/home/目录。
[root@lh2022 Docker]# echo "这里是宿主机" > h_test.txt
[root@lh2022 Docker]# ls
2000 c_test.txt h_test.txt
[root@lh2022 Docker]# docker cp h_test.txt ub1:/home/
Successfully copied 2.05kB to ub1:/home/
[root@lh2022 Docker]# docker attach ub1
root@16683ac148d2:/home# ls
c_test.txt h_test.txt
root@16683ac148d2:/home# cat h_test.txt
这里是宿主机
root@16683ac148d2:/home#
删除所有容器后。
[root@lh2022 Docker]# docker rm $(docker ps -aq)
16683ac148d2
23ba1c7ae31e
[root@lh2022 Docker]# docker ps -aq
[root@lh2022 Docker]#
docker create #创建容器但不启动的命令
用法同 docker run
通过docker create命令创建容器后,可以使用docker start containerID的方式启动容器。
[root@lh2022 Docker]# docker create --name ub2 -it ubuntu /bin/bash
238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
238e629747a4 ubuntu "/bin/bash" 6 seconds ago Created ub2
[root@lh2022 Docker]# docker start 238e629747a4
238e629747a4
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
238e629747a4 ubuntu "/bin/bash" About a minute ago Up 2 seconds ub2
[root@lh2022 Docker]#
可以发现使用docker create命令创建ub2容器后,ub2并没有自动启动运行。如果要启动它可以使用docker start containerID的方式启动容器。
pdocker diff #查看容器里文件结构的更改的命令
作用:可列出容器内发生变化的文件和目录,变化包括(增,删,改)。
[root@lh2022 Docker]# docker diff ub2
[root@lh2022 Docker]# docker cp h_test.txt ub2:/home
Successfully copied 2.05kB to ub2:/home
[root@lh2022 Docker]# docker diff ub2
C /home
A /home/h_test.txt
[root@lh2022 Docker]#
docker events #查看事件的命令
docker events命令用于查看Docker引擎的实时事件。通过-f参数,可以过滤不必要的事件,包括容器、镜像、卷和网络相关的事件。
# 分屏1
[root@lh2022 Docker]# docker events
2023-11-15T18:48:49.628580359-08:00 container kill 238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd (image=ubuntu, name=ub2, signal=15)
2023-11-15T18:48:49.757849329-08:00 network disconnect 8e75735fe85954df37e129075fb78291b9b0d9dbfc3f5a79e0d3db6d3dfd80bb (container=238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd, name=bridge, type=bridge)
2023-11-15T18:48:49.767037301-08:00 container stop 238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd (image=ubuntu, name=ub2)
2023-11-15T18:48:49.769831302-08:00 container die 238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd (execDuration=2466, exitCode=0, image=ubuntu, name=ub2)
# 分配2
[root@lh2022 Docker]# docker stop ub2
ub2
[root@lh2022 Docker]#
以下是对各种事件类型的整理:
1.容器相关事件:
- attach:容器附加到标准输入、输出和错误流
- commit:保存容器的文件系统状态为新的镜像
- copy:从容器复制文件或目录
- create:创建新容器
- destroy:销毁容器
- …
docker events -f container=
# 分屏1
[root@lh2022 Docker]# docker events -f container=ub2
2023-11-15T18:50:51.562283074-08:00 container start 238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd (image=ubuntu, name=ub2)
# 分屏2
[root@lh2022 Docker]# docker start ub2
ub2
[root@lh2022 Docker]#
2.镜像相关事件:
- delete:删除镜像
- import:导入镜像
- load:从归档文件加载镜像
- pull:拉取镜像
- …
docker events -f image=
3.卷相关事件:
- create:创建卷
- mount:挂载卷
- unmount:卸载卷
- destroy:销毁卷
- …
docker events -f volume=
4.网络相关事件:
- create:创建网络
- connect:将容器连接到网络
- disconnect:将容器从网络断开
- destroy:销毁网络
- …
docker events -f network=
通过这些示例,可以过滤并查看特定类型的Docker事件,以便更好地监控和管理Docker引擎的活动。
docker exec #进入容器的命令:
该命令主要用于进入容器内部进行操作。比attach功能更强大(exec使用率较高)
- -d:分离模式,即在后台运行命令
- -i:交互模式
- -t:分配一个 tty(终端驱动程序)
- -u:指定用户和用户组,格式:<name|uid>[:<group|gid>]
exec命令进入容器内部就如同进入另一台机器一样,可以灵活操作,并且使用exit命令退出时,不会像attach那样导致容器停止。
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
238e629747a4 ubuntu "/bin/bash" 45 minutes ago Up About a minute ub2
[root@lh2022 Docker]# docker exec -it ub2 /bin/bash
root@238e629747a4:/# exit
exit
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
238e629747a4 ubuntu "/bin/bash" 46 minutes ago Up 2 minutes ub2
[root@lh2022 Docker]# docker attach ub2
root@238e629747a4:/# exit
exit
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
238e629747a4 ubuntu "/bin/bash" 46 minutes ago Exited (0) 3 seconds ago ub2
[root@lh2022 Docker]#
实现本地镜像的导出、导入(export、import、save、load)
docker export #导出容器的命令:
该命令用于导出本地存储的容器,导出的容器通常为tar包。
docker export -o /var/www/container.tar container
docker export container > /var/www/container.tar
- -o:指定目标文件位置和名称。
- container:容器名
注:使用 docker export 命令不会压缩容器大小
[root@lh2022 Docker]# ls
2000 c_test.txt h_test.txt
[root@lh2022 Docker]# docker export -o ./ub2.tar ub2
[root@lh2022 Docker]# ls
2000 c_test.txt h_test.txt ub2.tar
[root@lh2022 Docker]# docker export ub2 > ./ubub.tar
[root@lh2022 Docker]# ls
2000 c_test.txt h_test.txt ub2.tar ubub.tar
[root@lh2022 Docker]#
docker import #导入容器的命令
该命令与 docker export命令相对,用于导入容器,从归档文件中创建一个镜像,导入后变成镜像。
语法: docker import [OPTIONS] file |URL|- [REPOSITORY[:TAG]]
参数说明:
- -c : --change list :通过Dockerfile来创建镜像
- docker import -c ${filePath}
- -m : --message string :设置提交时的说明信息
- docker import -m ${message}
从本地导入:docker import /root/Docker/ub2.tar myubu:v2
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu v1 abfa3ff80204 2 hours ago 72.8MB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@lh2022 Docker]# docker import /root/Docker/ub2.tar myubu:v2
sha256:4e5c141c83f093d74f9bc4491d01354a79a60d1dd81f633f07b6eb25e8693945
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubu v2 4e5c141c83f0 2 seconds ago 72.8MB
myubuntu v1 abfa3ff80204 2 hours ago 72.8MB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@lh2022 Docker]#
docker save #保存镜像
docker load #载入镜像
docker load命令用于导入使用docker save命令导出的镜像。通过该命令,可以将之前保存的镜像文件重新加载到Docker中。
常用参数:
- -i:指定要导入的文件。
- -q:精简输出信息。
例如,要导入名为ubuntu.tar的镜像文件,可以使用以下命令:
docker load -i ubuntu.tar
或者
docker load < ubuntu.tar
这将导入ubuntu.tar文件作为一个镜像。
需要注意的是,docker load命令只能导入镜像,无法导入容器。如果需要导出容器,请使用docker export命令;如果需要将容器导入为镜像,请使用docker import命令。
在导入镜像之前,通常需要先使用docker save命令将镜像导出为一个文件。例如,使用以下命令将名为myubuntu:v1的镜像导出为ub100.tar文件:
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubu v2 4e5c141c83f0 21 minutes ago 72.8MB
myubuntu v1 abfa3ff80204 2 hours ago 72.8MB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@lh2022 Docker]# ls
2000 c_test.txt h_test.txt ub2.tar ubub.tar
[root@lh2022 Docker]# docker save -o ub100.tar myubuntu:v1
[root@lh2022 Docker]# ls
2000 c_test.txt h_test.txt ub100.tar ub2.tar ubub.tar
[root@lh2022 Docker]#
再删除myubuntu:v1 镜像。
[root@lh2022 Docker]# docker rmi myubuntu:v1
Untagged: myubuntu:v1
Deleted: sha256:abfa3ff80204b3268c8756339627651dc2a2173bdce023eb7f2d4d7e17c1291a
Deleted: sha256:5154950c32caaca682dc87951263b479f8187c6c74b0eb9f5bec36e276087823
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubu v2 4e5c141c83f0 23 minutes ago 72.8MB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@lh2022 Docker]#
然后,可以使用docker load命令将ub100.tar文件导入为一个镜像。docker load -i ub100.tar 。
[root@lh2022 Docker]# docker load -i ub100.tar
6914406b2a6b: Loading layer 2.56kB/2.56kB
Loaded image: myubuntu:v1
[root@lh2022 Docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubu v2 4e5c141c83f0 24 minutes ago 72.8MB
myubuntu v1 abfa3ff80204 2 hours ago 72.8MB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@lh2022 Docker]#
再查看镜像发现导入成功。
总结起来,docker load命令用于导入使用docker save命令导出的镜像文件,是将之前保存的镜像重新加载到Docker中的操作。
export、import和save、load是两种不同的方法,不能混用。
导出和保存的格式不同:
- Docker export 导出的是容器的文件系统,格式为 .tar 文件。
- Docker save 保存的是镜像,格式为 .tar 文件。
导入和加载的方式不同:
- Docker import 可以从 .tar 文件导入镜像,创建新的镜像。
- Docker load 可以从 .tar 文件加载镜像,创建新的镜像。
文件大小不同:
- export 导出的镜像文件体积小于 save 保存的镜像。
是否可以对镜像重命名:
- Docker import 可以为镜像指定新名称。
- Docker load 不能对载入的镜像重命名。
是否可以同时将多个镜像打包到一个文件中:
- Docker export 不支持。
- Docker save 支持。
是否包含镜像历史:
- export 导出(import 导入)丢失镜像所有的历史记录和元数据信息,无法进行回滚操作。
- save 保存(load 加载)的镜像保留镜像的历史,可以回滚到之前的层(layer)。
应用场景不同:
- Docker export 适用于制作基础镜像,用于容器文件系统备份和共享。
- Docker save 适用于多个镜像组合的部署场景,用于镜像的备份和共享。
docker history #查看镜像历史的命令:
该命令用于显示镜像的历史,查看镜像的历史变化。
例:dockerhistory ubuntu:latest
[root@lh2022 Docker]# docker history ubuntu:latest
IMAGE CREATED CREATED BY SIZE COMMENT
ba6acccedd29 2 years ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 2 years ago /bin/sh -c #(nop) ADD file:5d68d27cc15a80653… 72.8MB
[root@lh2022 Docker]# docker history myubu:v2
IMAGE CREATED CREATED BY SIZE COMMENT
4e5c141c83f0 27 minutes ago 72.8MB Imported from -
[root@lh2022 Docker]# docker history myubuntu:v1
IMAGE CREATED CREATED BY SIZE COMMENT
abfa3ff80204 2 hours ago bash 8B my ubuntu,tag=v1
<missing> 2 years ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 2 years ago /bin/sh -c #(nop) ADD file:5d68d27cc15a80653… 72.8MB
docker inspect #查看各项详细信息的命令:
获取容器/镜像的元数据。
语法:docker inspect [OPTIONS] NAME|ID [NAME|ID…]
OPTIONS说明:
- -f :指定返回值的模板文件。
- -s :显示总的文件大小。
- –type :为指定类型返回JSON。
例:以ub1容器为例
此命令可以查看容器:IP地址、Mac地址、日志等等文章来源:https://www.toymoban.com/news/detail-755406.html
[root@lh2022 Docker]# docker inspect ub2
[
{
"Id": "238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd",
"Created": "2023-11-16T02:06:36.095363783Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-11-16T02:50:51.557878979Z",
"FinishedAt": "2023-11-16T02:53:29.19971954Z"
},
"Image": "sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1",
"ResolvConfPath": "/var/lib/docker/containers/238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd/hostname",
"HostsPath": "/var/lib/docker/containers/238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd/hosts",
"LogPath": "/var/lib/docker/containers/238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd/238e629747a48a847af3d9300ce007f5c47884ffcd6029339b099215bf5055bd-json.log",
"Name": "/ub2",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
17,
95
],
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware",
"/sys/devices/virtual/powercap"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/cc7de16eeedc3032ee3e4d63e43f4eec607d861ed87000a58f1473b93ffb4c6f-init/diff:/var/lib/docker/overlay2/81040a27ad2991a0f03d38a70d3f541dc867955b596d36671bbcc4ff89e80b66/diff",
"MergedDir": "/var/lib/docker/overlay2/cc7de16eeedc3032ee3e4d63e43f4eec607d861ed87000a58f1473b93ffb4c6f/merged",
"UpperDir": "/var/lib/docker/overlay2/cc7de16eeedc3032ee3e4d63e43f4eec607d861ed87000a58f1473b93ffb4c6f/diff",
"WorkDir": "/var/lib/docker/overlay2/cc7de16eeedc3032ee3e4d63e43f4eec607d861ed87000a58f1473b93ffb4c6f/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "238e629747a4",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "ubuntu",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "c19b11e3e2d34dce5d767270e64a89e83c29c90e4cc43954b577e4759cdc06d4",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/c19b11e3e2d3",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "8e75735fe85954df37e129075fb78291b9b0d9dbfc3f5a79e0d3db6d3dfd80bb",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
[root@lh2022 Docker]#
[root@lh2022 Docker]# docker inspect --format='{{range.NetworkSettings.Networks}}{{.NetworkID}}{{end}}' ub2
8e75735fe85954df37e129075fb78291b9b0d9dbfc3f5a79e0d3db6d3dfd80bb
docker kill #杀死容器的命令:
该命令用于快速杀掉容器的进程,常用于无法停止容器的时候,使用docker kill命令可以让一个无响应的容器停止。
与docker stop的区别:给容器发送不同的信号,stop命令将会发送SIGTERM信号,容器接收到此信号的默认行为会导致容器退出,同时也允许容器内的程序自行处理,而kill发出的信号为SIGKILL信号,此信号使得容器必然退出。文章来源地址https://www.toymoban.com/news/detail-755406.html
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
238e629747a4 ubuntu "/bin/bash" 2 hours ago Up 2 seconds ub2
[root@lh2022 Docker]# docker kill ub2
ub2
[root@lh2022 Docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
238e629747a4 ubuntu "/bin/bash" 2 hours ago Exited (137) 1 second ago ub2
[root@lh2022 Docker]#
到了这里,关于云计算Docker实验报告 实验一1.1 Docker命令的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!