docker容器生命周期管理
查看容器版本
docker version
[root@node1 ~]# docker version
Client: Docker Engine - Community
Version: 20.10.9
API version: 1.41
Go version: go1.16.8
Git commit: c2ea9bc
Built: Mon Oct 4 16:08:14 2021
OS/Arch: linux/amd64
Context: default
Experimental: true Server: Docker Engine - Community
Engine:
Version: 20.10.9
API version: 1.41 (minimum version 1.12)
Go version: go1.16.8
Git commit: 79ea9d3
Built: Mon Oct 4 16:06:37 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.11
GitCommit: 5b46e404f6b9f661a205e28d59c982d3634148f8
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
运行容器
docker run ‘image’ 或者docker container run ‘image’
如果docker run 后面指定的镜像没有加tag,那么默认就会运行tag为latest的镜像。
交互式运行容器
docker run -i -t 镜像名
-i 表示交互式
-t 表示为容器开启一个终端
[root@node1 ~]# docker run -i -t centos
[root@5fd531998278 /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 1.1 0.2 12036 2088 pts/0 Ss 06:22 0:00 /bin/bash
root 15 0.0 0.1 44652 1780 pts/0 R+ 06:22 0:00 ps aux
以后台的形式运行容器(最常使用的)
docker run -dt “image” “command” 后面的command会替代image中构建的最后一个命令
-d 参数是后台运行
[root@node1 ~]# docker run -dt centos bash
fec14b595a81d8c47f5e81035c883b75147a3f52da51eb8b79298dede4c72945
[root@node1 docker]# docker run -dt --name web nginx bash
7c741b579a562d264a4bd0d24ed84405c19476875f579d2f8dc230e183349ec9
[root@node1 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c741b579a56 nginx "/docker-entrypoint.…" 7 seconds ago Up 7 seconds 80/tcp web
b713aabd323d centos "/bin/bash" 5 minutes ago Up 5 minutes dazzling_raman
进入一个已运行的容器
docker exec -it ‘容器id或NAMES’ ‘command’
[root@node1 docker]# docker exec -it b713aabd323d bash
[root@b713aabd323d /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 12036 1936 pts/0 Ss+ 14:11 0:00 /bin/bash
root 27 0.1 0.2 12036 2100 pts/1 Ss 14:18 0:00 bash
root 41 0.0 0.1 44652 1784 pts/1 R+ 14:19 0:00 ps aux
退出容器
exit
临时退出容器
ctrl+p+q
返回临时退出的容器
docker attach +容器的id
停止容器
docker stop + 容器id 正常停止一个容器
docker kill + 容器id 强行杀掉一个容器
上图中容器状态STATUS可以看到退出容器的状态码,docker stop +容器id停止的状态码为0,docker kill + 容器id停止的状态码是非零
重启容器
docker restart +容器id
查看运行的容器
docker ps
docker ps -q 只查看运行容器id
查看所有的容器,包括已退出的容器
docker ps -a
docker ps -aq 只查看所有容器的id
删除容器
docker container rm +容器id
默认情况下只能删除已经停止的容器,也可以加-f参数强行删除正在运行的容器(不建议)
查看容器log
docker logs
首先运行一个容器nginx,然后执行docker logs web 查看这个容器的日志
[root@node1 ~]# docker run -dt --name web nginx #启动一个指定名为web的nginx容器
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd9d8a5a355e nginx "/docker-entrypoint.…" 51 seconds ago Up 50 seconds 80/tcp web
fec14b595a81 centos "/bin/bash" 6 hours ago Up 6 hours condescending_benz
67c3f5089a59 centos "/bin/bash" 6 hours ago Up 6 hours zealous_greider
[root@node1 ~]# docker logs web #查看web的容器log
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/10/20 12:56:57 [notice] 1#1: using the "epoll" event method
2021/10/20 12:56:57 [notice] 1#1: nginx/1.21.3
2021/10/20 12:56:57 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/10/20 12:56:57 [notice] 1#1: OS: Linux 3.10.0-1062.el7.x86_64
2021/10/20 12:56:57 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/10/20 12:56:57 [notice] 1#1: start worker processes
2021/10/20 12:56:57 [notice] 1#1: start worker process 31
查看容器的进程
docker top 进程id或者 NAMES
[root@node1 ~]# docker top web
UID PID PPID C STIME TTY TIME CMD
root 12080 12061 0 20:56 pts/0 00:00:00 nginx: master process nginx -g daemon off;
101 12128 12080 0 20:56 pts/0 00:00:00 nginx: worker process
[root@node1 ~]# docker top condescending_benz
UID PID PPID C STIME TTY TIME CMD
root 11236 11218 0 17:11 pts/0 00:00:00 /bin/bash
[root@node1 ~]# docker top fec14b595a81
UID PID PPID C STIME TTY TIME CMD
root 11236 11218 0 17:11 pts/0 00:00:00 /bin/bash
查看容器内部的详细信息
docker inspect web #查看名为web的nginx容器详细信息 ,这个命令返回结果为json格式
[root@node1 ~]# docker inspect web
[
{
"Id": "cd9d8a5a355e21c423af1c53286af9e4670ec9457f0e22b1a68e0d699bcfa78f",
"Created": "2021-10-20T12:56:57.15455763Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 12080,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-10-20T12:56:57.915592015Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:87a94228f133e2da99cb16d653cd1373c5b4e8689956386c1c12b60a20421a02",
"ResolvConfPath": "/var/lib/docker/containers/cd9d8a5a355e21c423af1c53286af9e4670ec9457f0e22b1a68e0d699bcfa78f/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/cd9d8a5a355e21c423af1c53286af9e4670ec9457f0e22b1a68e0d699bcfa78f/hostname",
"HostsPath": "/var/lib/docker/containers/cd9d8a5a355e21c423af1c53286af9e4670ec9457f0e22b1a68e0d699bcfa78f/hosts",
"LogPath": "/var/lib/docker/containers/cd9d8a5a355e21c423af1c53286af9e4670ec9457f0e22b1a68e0d699bcfa78f/cd9d8a5a355e21c423af1c53286af9e4670ec9457f0e22b1a68e0d699bcfa78f-json.log",
"Name": "/web",
"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,
"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",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"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"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/c34d13e1617f850faa442b48ee067cee6b4c786617a68bc0f14efac09680cf5e-init/diff:/var/lib/docker/overlay2/916b4b09d26638d695f00d92703ea74c3ca25ec2b45553bbbfb8124b94c97d51/diff:/var/lib/docker/overlay2/646e9fdde9960bf6b7751d19d12921668857653cbcbe12ece1f8778560f26fac/diff:/var/lib/docker/overlay2/c23d2e4620ca9dad82d584da266ac08650339db8b0256cb0c6af396f6acf52c9/diff:/var/lib/docker/overlay2/0978fa2b8de789fe52c284e7189c672b1ae149a23a1120cac53764ca58ffc26a/diff:/var/lib/docker/overlay2/1806b9060674b511023a35cb7d2d0eb05f2819bc7af1e884ad6eb5bc21a98fb9/diff:/var/lib/docker/overlay2/0a4ab40a97f3191a3c1ff84f2653499dbf8a4f73f2b771750212eaf5a1355bca/diff",
"MergedDir": "/var/lib/docker/overlay2/c34d13e1617f850faa442b48ee067cee6b4c786617a68bc0f14efac09680cf5e/merged",
"UpperDir": "/var/lib/docker/overlay2/c34d13e1617f850faa442b48ee067cee6b4c786617a68bc0f14efac09680cf5e/diff",
"WorkDir": "/var/lib/docker/overlay2/c34d13e1617f850faa442b48ee067cee6b4c786617a68bc0f14efac09680cf5e/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "cd9d8a5a355e",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": true,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.3",
"NJS_VERSION=0.6.2",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "7e0664529b4530566bb81f703c9ed5cb7c3d2a8d52922fafcd812fcf570a9953",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": null
},
"SandboxKey": "/var/run/docker/netns/7e0664529b45",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "a510f09a369fea8067cd1e9a5f13fc6b1905437246f386395aefa1e7b295651a",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:04",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "1e97883068f16a7653829f2adc3fa1da86d219b4ce00f9eac7d1c96b66a9aeee",
"EndpointID": "a510f09a369fea8067cd1e9a5f13fc6b1905437246f386395aefa1e7b295651a",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
}
}
}
}
]
[root@node1 ~]#
docker inspect web
将主机文件拷贝到容器中
docker cp
将docker目录下的ubunto_latest_image.tar 拷贝到名为busy_buck的容器/目录下
[root@node1 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2bb4aeab2410 centos "bash" About a minute ago Up About a minute busy_buck
[root@node1 docker]# ls
daemon.json key.json ubunto_latest_image.tar
[root@node1 docker]# docker cp ubunto_latest_image.tar busy_buck:/
[root@node1 docker]# docker attach 2bb4aeab2410
[root@2bb4aeab2410 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp ubunto_latest_image.tar usr var
将容器文件拷贝到主机上
[root@node1 docker]# docker cp dazzling_raman:/ubunto_latest_image.tar ./
[root@node1 docker]# ls
daemon.json key.json ubunto_latest_image.tar
docker容器生命周期管理的更多相关文章
- Docker 容器生命周期管理命令
docker run 命令 -d: 后台运行容器,并返回容器ID: -i: 以交互模式运行容器,通常与 -t 同时使用: -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用: --name= ...
- 004-docker命令-容器生命周期管理、容器操作
1.容器生命周期管理 docker run :创建一个新的容器并运行一个命令 语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明: - ...
- 存储-docker volume 生命周期管理(14)
volume 生命周期管理 - 每天5分钟玩转 Docker 容器技术(44) Data Volume 中存放的是重要的应用数据,如何管理 volume 对应用至关重要.前面我们主要关注的是 volu ...
- Docker系列02: 容器生命周期管理 镜像&容器
A) Docker信息1. 查看docker运行状态 systemctl status docker docker.service - Docker Application Container Eng ...
- Docker容器与镜像管理
目录 容器管理 运行容器 容器的启停操作 容器导入导出 容器生命周期管理 容器资源限制 内存限制 CPU限制 io 限制 镜像管理 镜像命名规范 镜像基本操作 容器管理 运行容器 1.运行一个容器示例 ...
- Castle IOC容器组件生命周期管理
主要内容 1.生命处理方式 2.自定义生命处理方式 3.生命周期处理 一.生命处理方式 我们通常创建一个组件的实例使用new关键字,这样每次创建出来的都是一个新的实例,如果想要组件只有一个实例,我们会 ...
- 理解Docker容器的进程管理
摘要: Docker在进程管理上有一些特殊之处,如果不注意这些细节中的魔鬼就会带来一些隐患.另外Docker鼓励"一个容器一个进程(one process per container)&qu ...
- Docker容器集群管理之Swarm
Docker容器集群管理主流方案 Swarm Docker公司自研发的集群管理系统. Kubernetes Google开源的一个容器集群管理系统,用于自动化部署.扩展和管理容器应用.也称为K8S ...
- 第 6 章 存储 - 044 - volume 生命周期管理
volume 生命周期管理 1)备份 因为 volume 实际上是 host 文件系统中的目录和文件,所以 volume 的备份实际上是对文件系统的备份 例如:本地的Registry,所有镜像都存在/ ...
- Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之集群部署环境规划(一)
0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.环境规划 软件 版本 ...
随机推荐
- iOS Charles抓包
一.Charles 安装 官方下载地址:https://www.charlesproxy.com/ 二.Charles 破解 1.破解地址:https://www.zzzmode.com/mytool ...
- SDN拓扑实践
一.使用Mininet可视化工具,生成下图所示的拓扑,并保存拓扑文件名为学号.py 二.使用Mininet的命令行生成如下拓扑: 1) 3台交换机,每个交换机连接1台主机,3台交换机连接成一条线. 2 ...
- 浅谈storm
storm分布式,可容错的实时计算框架,低延迟能做到毫秒级的响应,storm进程是常驻内存,Hadoop是不断启停的,storm中的数据不经过磁盘,都在内存中,处理完成后就没有了,但是可以写到数据库中 ...
- centos 7 删除乱码文件
1.通过ls -li 获取要删除乱码文件名文件的inode号,比如得到的是 33575029 2.通过以下命令删除文件:find ./ -inum 33575029 -exec rm -rf {} \ ...
- wendows 批量修改文件后缀(含递归下级)
for /r %%a in (*.jpg)do ren "%%a" "%%~na.png" //-- or :for /r %a in (*.jpg)do re ...
- C. Tea Tasting
https://codeforces.com/contest/1795/problem/C 用二分+前缀和+差分卡过 #include <iostream> #include <cs ...
- 02.java基础(一)java的基础、方法和数组
目录 Java基础 Java特性 Java程序运行机制 Java基础语法 1.数据类型 基本类型 引用类型 数据类型扩展 String类型内存分配过程 转义字符 类型转换 变量 常量 2.运算符 逻辑 ...
- Qt实现抽奖程序
一.简介 该程序命名为Lucky,实现的功能如下: 1. 加载抽奖人员名单,并保存加载路径: 2. 单击左键或者点击ctrl+s开始抽奖,并滚动显示人员名单,显示的人员名单格式为 部门-姓名. 3. ...
- VS+QT创建的项目 UI界面更新控件,代码里识别不到
1.如果安装了小番茄,看下自己的小番茄的设置里,source of C/C++ content需要选择 Default Intellisense,选择visual assist是识别不到的,具体是为什 ...
- Python与CSharp之间内存共享互传信息
C#写入字符串到共享内存 try { long t = 1 << 10 << 10; var mmf = MemoryMappedFile.CreateOrOpen(" ...