龙空技术网

CKA备考实验 | 管理容器的命令

热爱编程的通信人 172

前言:

当前朋友们对“c语言52f什么意思”可能比较关注,朋友们都想要学习一些“c语言52f什么意思”的相关资讯。那么小编在网络上收集了一些对于“c语言52f什么意思””的相关内容,希望看官们能喜欢,小伙伴们一起来学习一下吧!

书籍来源:《CKA/CKAD应试指南:从Docker到Kubernetes完全攻略》

一边学习一边整理老师的课程内容及实验笔记,并与大家分享,侵权即删,谢谢支持!

附上汇总贴:CKA备考实验 | 汇总_热爱编程的通信人的博客-CSDN博客

容器如同一台没有显示器的电脑,如何查看容器里的东西,又如何在容器里执行命令呢?这可以利用docker exec来实现,如图1-15所示。

通过docker exec就可以执行容器里的命令了。

在容器里执行指定的命令

语法:

docker exec 容器名 命令

步骤1:在容器db里执行ip a | grep 'inet ' 命令。

##########实操验证##########[root@vms100 ~]# docker exec db ip a | grep 'inet '    inet 127.0.0.1/8 scope host lo    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0[root@vms100 ~]# 

如果容器里没有要执行的命令,就会出现报错。

##########实操验证##########[root@vms100 ~]# docker exec db ifconfigOCI runtime exec failed: exec failed: unable to start container process: exec: "ifconfig": executable file not found in $PATH: unknown[root@vms100 ~]# 

如果想获取shell控制台的话,需要加上-it选项。

步骤2:获取容器里bash控制台。

##########实操验证##########[root@vms100 ~]# docker exec -it db bashroot@b1fff2458132:/# root@b1fff2458132:/# root@b1fff2458132:/# exitexit[root@vms100 ~]# 

注意:有的镜像里不存在bash,可以使用sh替代。

物理机和容器互相拷贝文件

有时我们需要让物理机和容器之间互相拷贝一些文件,它们之间拷贝文件的语法如下。

docker cp /path/file 容器:/path2 把物理机的/path/file拷贝到容器里的/path2里docker cp 容器:/path2/file /path/ 把容器里的/path2/file拷贝到物理机的/path里

步骤1:把物理机的/etc/hosts拷贝到容器的/opt里。

##########实操验证##########[root@vms100 ~]# docker exec db ls /opt[root@vms100 ~]# [root@vms100 ~]# docker cp /etc/hosts db:/opt                             Successfully copied 2.05kB to db:/opt[root@vms100 ~]# docker exec db ls /opthosts[root@vms100 ~]# 

可以看到容器的/opt目录里原来是没有hosts文件的,现在已经拷贝进去了。

步骤2:把容器的/etc/passwd拷贝到物理机的/opt里。

##########实操验证##########[root@vms100 ~]# ls /opt/containerd  rh[root@vms100 ~]# docker cp db:/etc/passwd /opt/                                               Successfully copied 3.07kB to /opt/[root@vms100 ~]# ls /opt/containerd  passwd  rh[root@vms100 ~]# 

可以看到物理机的/opt目录里原来是没有passwd文件的,现在已经拷贝过来了。

关闭、启动、重启容器

一般情况下在操作系统里重启某个服务,可以通过systemctl restart服务名来重启,容器里一般是没法使用systemctl命令的。如果要重启容器里的程序,直接重启容器就可以了。下面演示如何关闭、启动、重启容器。

步骤1:关闭、启动、重启容器。

##########实操验证##########[root@vms100 ~]# docker ps -qb1fff2458132[root@vms100 ~]# docker stop dbdb[root@vms100 ~]# docker ps -q[root@vms100 ~]# docker start dbdb[root@vms100 ~]# docker ps -qb1fff2458132[root@vms100 ~]# docker restart dbdb[root@vms100 ~]# docker ps -qb1fff2458132[root@vms100 ~]# 

步骤2:查看容器里运行的进程。

语法:

docker top 容器名

这个类似于任务管理器,可以查看到容器里正在运行的进程。

##########实操验证##########[root@vms100 ~]# docker top dbUID                 PID                 PPID                C                   STIME               TTY                 TIME                CMDpolkitd             41342               41323               1                   11:26               ?                   00:00:00            mysqld[root@vms100 ~]# 
查看容器里的输出

当容器没法正常运行的时候,我们需要查看容器里的输出来进行排错。如果要查看容器里的日志信息,可以通过如下命令进行查看。

docker logs 容器名

查看容器日志时,如果要持续显示日志内容,即只要容器内容更新,日志中就能立刻显示出来,可以使用“docker logs -f 容器名”命令,操作如下。

步骤1:查看容器日志输出。

##########实操验证##########[root@vms100 ~]# docker logs dbInitializing database2023-05-03T03:19:46.157887Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).2023-05-03T03:19:46.312799Z 0 [Warning] InnoDB: New log files created, LSN=457902023-05-03T03:19:46.338833Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.... #省略其中的大量内容2023-05-03T03:26:02.814635Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check. 2023-05-03T03:26:02.814638Z 0 [Note] Beginning of list of non-natively partitioned tables2023-05-03T03:26:02.823602Z 0 [Note] End of list of non-natively partitioned tables[root@vms100 ~]# 

如果要查看容器的属性,可以通过“docker inspect 容器名”来实现。

步骤2:查看容器db的属性。

##########实操验证##########[root@vms100 ~]# docker inspect db[    {        "Id": "b1fff2458132d47eb89958600445dce3942071ba638f7d5d0c6cb0cd0c7b6f08",        "Created": "2023-05-03T03:19:45.765381166Z",        "Path": "docker-entrypoint.sh",        "Args": [            "mysqld"        ],        "State": {            "Status": "running",            "Running": true,            "Paused": false,            "Restarting": false,            "OOMKilled": false,            "Dead": false,            "Pid": 41342,            "ExitCode": 0,            "Error": "",            "StartedAt": "2023-05-03T03:26:02.458135184Z",            "FinishedAt": "2023-05-03T03:26:02.051540248Z"        },        "Image": "sha256:9e64176cd8a206f88336506fe52cd8f87423147dc197d0250175dddc39465e90",        "ResolvConfPath": "/var/lib/docker/containers/b1fff2458132d47eb89958600445dce3942071ba638f7d5d0c6cb0cd0c7b6f08/resolv.conf",        "HostnamePath": "/var/lib/docker/containers/b1fff2458132d47eb89958600445dce3942071ba638f7d5d0c6cb0cd0c7b6f08/hostname",        "HostsPath": "/var/lib/docker/containers/b1fff2458132d47eb89958600445dce3942071ba638f7d5d0c6cb0cd0c7b6f08/hosts",        "LogPath": "/var/lib/docker/containers/b1fff2458132d47eb89958600445dce3942071ba638f7d5d0c6cb0cd0c7b6f08/b1fff2458132d47eb89958600445dce3942071ba638f7d5d0c6cb0cd0c7b6f08-json.log",        "Name": "/db",        "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": "always",                "MaximumRetryCount": 0            },            "AutoRemove": false,            "VolumeDriver": "",            "VolumesFrom": null,            "ConsoleSize": [                54,                185            ],            "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"            ],            "ReadonlyPaths": [                "/proc/bus",                "/proc/fs",                "/proc/irq",                "/proc/sys",                "/proc/sysrq-trigger"            ]        },        "GraphDriver": {            "Data": {                "LowerDir": "/var/lib/docker/overlay2/2bdbb486ed87a51450b7d7ab356d5d137285623495ea36c083c233c1549048fb-init/diff:/var/lib/docker/overlay2/e2bd254108d4be1c214cb4c4d4f54e09f5ba9d2f878fab2c0ec8d35658ecabe7/diff:/var/lib/docker/overlay2/cbbb70bf8f6a80cfa3e52ce8a3fac6609e07147ff8f5f1d5ff8eeba4a1ca6114/diff:/var/lib/docker/overlay2/1c980ec9e79968c392c20a77818ad66cb0632ba555d857dc282e7ff88b23a430/diff:/var/lib/docker/overlay2/afbb53c5b5d1b58f6014fa7c39e200c2fe4c77f1149347fd96008fac7e4ffecb/diff:/var/lib/docker/overlay2/3bb52f39959e2e49fb8ec139365eab13e64418b05fe873b428bc718e562c0d04/diff:/var/lib/docker/overlay2/70fbdb12349c7b0e7bf812edb79b83570be349eb168a9746e664da5a26d1ea3a/diff:/var/lib/docker/overlay2/ebffc7c353c581b9d75c69b105ddcaca2379b3312522339cc8f46b614e0d3787/diff:/var/lib/docker/overlay2/42717b05fc08c3827da977e200965dec44f4eece4f309ff36e0a4f64bc08442e/diff:/var/lib/docker/overlay2/e0ffac8f8d872dff032b3e02ca3063e2d18b241b3c1d126fcc918e4bd1ca84b1/diff:/var/lib/docker/overlay2/d5dc3a9249255c9d42c0e5235fee07fa211172657d69ff9a80cc5e09d068811e/diff:/var/lib/docker/overlay2/5449c9e57d4aa616794cd3ea229264ff5a3ec0232b5e9ab2ecccb36d35d69d93/diff",                "MergedDir": "/var/lib/docker/overlay2/2bdbb486ed87a51450b7d7ab356d5d137285623495ea36c083c233c1549048fb/merged",                "UpperDir": "/var/lib/docker/overlay2/2bdbb486ed87a51450b7d7ab356d5d137285623495ea36c083c233c1549048fb/diff",                "WorkDir": "/var/lib/docker/overlay2/2bdbb486ed87a51450b7d7ab356d5d137285623495ea36c083c233c1549048fb/work"            },            "Name": "overlay2"        },        "Mounts": [            {                "Type": "volume",                "Name": "59e13d8a98549e71403c55dfb3653ddfdd325cb1e79af8485962dc110219beab",                "Source": "/var/lib/docker/volumes/59e13d8a98549e71403c55dfb3653ddfdd325cb1e79af8485962dc110219beab/_data",                "Destination": "/var/lib/mysql",                "Driver": "local",                "Mode": "",                "RW": true,                "Propagation": ""            }        ],        "Config": {            "Hostname": "b1fff2458132",            "Domainname": "",            "User": "",            "AttachStdin": false,            "AttachStdout": false,            "AttachStderr": false,            "ExposedPorts": {                "3306/tcp": {}            },            "Tty": false,            "OpenStdin": false,            "StdinOnce": false,            "Env": [                "MYSQL_ROOT_PASSWORD=redhat",                "MYSQL_DATABASE=blog",                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",                "GOSU_VERSION=1.7",                "MYSQL_MAJOR=5.7",                "MYSQL_VERSION=5.7.18-1debian8"            ],            "Cmd": [                "mysqld"            ],            "Image": "hub.c.163.com/library/mysql",            "Volumes": {                "/var/lib/mysql": {}            },            "WorkingDir": "",            "Entrypoint": [                "docker-entrypoint.sh"            ],            "OnBuild": null,            "Labels": {}        },        "NetworkSettings": {            "Bridge": "",            "SandboxID": "1c5900ee97fec43fde00cd3c90dd687c7aac3e41aa49b50675655750578ead17",            "HairpinMode": false,            "LinkLocalIPv6Address": "",            "LinkLocalIPv6PrefixLen": 0,            "Ports": {                "3306/tcp": null            },            "SandboxKey": "/var/run/docker/netns/1c5900ee97fe",            "SecondaryIPAddresses": null,            "SecondaryIPv6Addresses": null,            "EndpointID": "963e65b29c78971e86188648db68ba2edf33ac3a7177942a59693ccc0cf58c2b",            "Gateway": "172.17.0.1",            "GlobalIPv6Address": "",            "GlobalIPv6PrefixLen": 0,            "IPAddress": "172.17.0.2",            "IPPrefixLen": 16,            "IPv6Gateway": "",            "MacAddress": "02:42:ac:11:00:02",            "Networks": {                "bridge": {                    "IPAMConfig": null,                    "Links": null,                    "Aliases": null,                    "NetworkID": "3cd11d1bdf6950ae11dec919c8f9fdb20423180a976a04cc36368bc3376be4bf",                    "EndpointID": "963e65b29c78971e86188648db68ba2edf33ac3a7177942a59693ccc0cf58c2b",                    "Gateway": "172.17.0.1",                    "IPAddress": "172.17.0.2",                    "IPPrefixLen": 16,                    "IPv6Gateway": "",                    "GlobalIPv6Address": "",                    "GlobalIPv6PrefixLen": 0,                    "MacAddress": "02:42:ac:11:00:02",                    "DriverOpts": null                }            }        }    }][root@vms100 ~]# 

在这个输出里,可以查看到容器的各种信息,比如数据卷、网络信息等。

标签: #c语言52f什么意思