docker之容器管理

1.创建容器

 docker create -it ubuntu:latest

[root@k8s-master ~]# docker create -it ubuntu:latest 
1b68a19ef4a286729dcd01b339bb702a0687e2ee1a768a359a2b59f51b3c6ac8
[root@k8s-master ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND       CREATED         STATUS                     PORTS     NAMES
1b68a19ef4a2   ubuntu:latest   "/bin/bash"   6 seconds ago   Created                              exciting_chaplygin
a88076332788   ubuntu:latest   "/bin/bash"   2 hours ago     Exited (137) 2 hours ago             jovial_sutherland
82a84790ae2d   ubuntu:latest   "/bin/bash"   2 hours ago     Exited (0) 2 hours ago               intelligent_hermann

2.启动容器

[root@k8s-master ~]# docker start 1b68a19ef4a2
1b68a19ef4a2
[root@k8s-master ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND       CREATED         STATUS                     PORTS     NAMES
1b68a19ef4a2   ubuntu:latest   "/bin/bash"   3 minutes ago   Up 12 seconds                        exciting_chaplygin
a88076332788   ubuntu:latest   "/bin/bash"   2 hours ago     Exited (137) 2 hours ago             jovial_sutherland
82a84790ae2d   ubuntu:latest   "/bin/bash"   3 hours ago     Exited (0) 2 hours ago               intelligent_hermann

3.创建并启动容器

docker run nginx-test /bin/echo 'hello world'

[root@k8s-master ~]# docker run nginx-test /bin/echo 'hello world'
hello world
[root@k8s-master ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                     PORTS     NAMES
ee0cb3389875   nginx-test      "/docker-entrypoint.…"   9 seconds ago    Exited (0) 8 seconds ago             youthful_ptolemy
1b68a19ef4a2   ubuntu:latest   "/bin/bash"              18 minutes ago   Up 14 minutes                        exciting_chaplygin
a88076332788   ubuntu:latest   "/bin/bash"              3 hours ago      Exited (137) 2 hours ago             jovial_sutherland
82a84790ae2d   ubuntu:latest   "/bin/bash"              3 hours ago      Exited (0) 3 hours ago               intelligent_hermann

当利用docker run来启动容器的时候,docker在后台运行的标准操作包括:

        1.检查镜像本地是否存在,如果不存在就从公有仓库下载

        2.利用镜像创建一个容器,并启动该容器

        3.分配一个文件系统给容器,并在只读的镜像层外面挂载一层可读写层

        4.从宿主主机配置的网桥接口桥接一个虚拟接口到容器中去

        5.从网桥地址池分配一个ip地址给容器

        6.执行用户指定的应用程序

        7.执行完毕后容器终止

docker run -it ubuntu:latest /bin/bash #-it选项表示可以交互,并分配一个伪终端

[root@k8s-master ~]# docker run -it ubuntu:latest 
root@4a41f6768877:/# 

执行docker run报错时,一般会有下面几个错误码:

        125:Docker daemon执行出错,例如指定了不支持的Docker命令参数

        126:所指定命令无法执行,例如权限出错

        127:容器内命令无法找到

4.守护态运行

        docker run -d ubuntu:latest /bin/sh -c "while true;do echo 'hello world';sleep 2;done"#-d 表示后台运行

[root@k8s-master ~]# docker run -d ubuntu:latest /bin/sh -c "while true;do echo 'hello world';sleep 2;done"
048f0e109be5b77caecf7dbbb324c18faa75b337081383a637bf23d804c9330c
[root@k8s-master ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                      PORTS     NAMES
048f0e109be5   ubuntu:latest   "/bin/sh -c 'while t…"   16 seconds ago   Up 15 seconds                         xenodochial_herschel
4a41f6768877   ubuntu:latest   "/bin/bash"              6 minutes ago    Exited (0) 4 minutes ago              laughing_turing
ee0cb3389875   nginx-test      "/docker-entrypoint.…"   16 minutes ago   Exited (0) 15 minutes ago             youthful_ptolemy
1b68a19ef4a2   ubuntu:latest   "/bin/bash"              34 minutes ago   Up 30 minutes                         exciting_chaplygin
a88076332788   ubuntu:latest   "/bin/bash"              3 hours ago      Exited (137) 3 hours ago              jovial_sutherland
82a84790ae2d   ubuntu:latest   "/bin/bash"              3 hours ago      Exited (0) 3 hours ago                intelligent_hermann

5.查看容器输出

docker logs -f -t 048f0e109be5 

docker logs常见选项:--details:打印详细信息 -f:持续保持输出 -t:显示时间戳

[root@k8s-master ~]# docker logs -f -t 048f0e109be5
2023-09-22T08:04:11.108564375Z hello world
2023-09-22T08:04:13.110121112Z hello world
2023-09-22T08:04:15.112201243Z hello world
2023-09-22T08:04:17.113795000Z hello world

6.停止容器

6.1 暂停容器

        docker pause 048f0e109be5

       

[root@k8s-master ~]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS     NAMES
048f0e109be5   ubuntu:latest   "/bin/sh -c 'while t…"   13 minutes ago   Up 13 minutes             xenodochial_herschel
1b68a19ef4a2   ubuntu:latest   "/bin/bash"              47 minutes ago   Up 43 minutes             exciting_chaplygin
[root@k8s-master ~]# docker pause 048f0e109be5
048f0e109be5
[root@k8s-master ~]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                   PORTS     NAMES
048f0e109be5   ubuntu:latest   "/bin/sh -c 'while t…"   13 minutes ago   Up 13 minutes (Paused)             xenodochial_herschel
1b68a19ef4a2   ubuntu:latest   "/bin/bash"              47 minutes ago   Up 43 minutes                      exciting_chaplygin

6.2 终止容器

docker stop 048f0e109be5 

[root@k8s-master ~]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                   PORTS     NAMES
048f0e109be5   ubuntu:latest   "/bin/sh -c 'while t…"   13 minutes ago   Up 13 minutes (Paused)             xenodochial_herschel
1b68a19ef4a2   ubuntu:latest   "/bin/bash"              47 minutes ago   Up 43 minutes                      exciting_chaplygin
[root@k8s-master ~]# docker stop 048f0e109be5
048f0e109be5
[root@k8s-master ~]# docker ps
CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS          PORTS     NAMES
1b68a19ef4a2   ubuntu:latest   "/bin/bash"   48 minutes ago   Up 45 minutes             exciting_chaplygin

 7. 进入容器

当我们用-d参数后,容器启动会进入后台,当我们需要进入容器时,可以使用attach或exec命令

我个人推荐是用exec命令

docker exec -it 1b68a19ef4a2 bash

[root@k8s-master ~]# docker exec -it 1b68a19ef4a2 bash
root@1b68a19ef4a2:/# whoami
root

attach和exec的区别:

        attach进入容器后直接输入exit或者ctrl+D不仅会退出容器,还会直接停止容器

        exec进入容器后直接输入exit直接退出容器,但不会导致容器停止

8.删除容器 

可以使用docker rm来删除处于终止或者退出的容器

docker rm 82a84790ae2d

[root@k8s-master ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                        PORTS     NAMES
048f0e109be5   ubuntu:latest   "/bin/sh -c 'while t…"   25 minutes ago   Exited (137) 10 minutes ago             xenodochial_herschel
4a41f6768877   ubuntu:latest   "/bin/bash"              31 minutes ago   Exited (0) 29 minutes ago               laughing_turing
ee0cb3389875   nginx-test      "/docker-entrypoint.…"   40 minutes ago   Exited (0) 40 minutes ago               youthful_ptolemy
1b68a19ef4a2   ubuntu:latest   "/bin/bash"              59 minutes ago   Up 55 minutes                           exciting_chaplygin
a88076332788   ubuntu:latest   "/bin/bash"              3 hours ago      Exited (137) 3 hours ago                jovial_sutherland
82a84790ae2d   ubuntu:latest   "/bin/bash"              3 hours ago      Exited (0) 3 hours ago                  intelligent_hermann
[root@k8s-master ~]# docker rm 82a84790ae2d
82a84790ae2d
[root@k8s-master ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                        PORTS     NAMES
048f0e109be5   ubuntu:latest   "/bin/sh -c 'while t…"   25 minutes ago   Exited (137) 10 minutes ago             xenodochial_herschel
4a41f6768877   ubuntu:latest   "/bin/bash"              32 minutes ago   Exited (0) 29 minutes ago               laughing_turing
ee0cb3389875   nginx-test      "/docker-entrypoint.…"   41 minutes ago   Exited (0) 41 minutes ago               youthful_ptolemy
1b68a19ef4a2   ubuntu:latest   "/bin/bash"              59 minutes ago   Up 55 minutes                           exciting_chaplygin
a88076332788   ubuntu:latest   "/bin/bash"              3 hours ago      Exited (137) 3 hours ago                jovial_sutherland

如果容器还没有正常退出,我们要删除必须加上-f强制删除,一般不推荐

docker rm -f 1b68a19ef4a2

[root@k8s-master ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                        PORTS     NAMES
048f0e109be5   ubuntu:latest   "/bin/sh -c 'while t…"   25 minutes ago   Exited (137) 10 minutes ago             xenodochial_herschel
4a41f6768877   ubuntu:latest   "/bin/bash"              32 minutes ago   Exited (0) 29 minutes ago               laughing_turing
ee0cb3389875   nginx-test      "/docker-entrypoint.…"   41 minutes ago   Exited (0) 41 minutes ago               youthful_ptolemy
1b68a19ef4a2   ubuntu:latest   "/bin/bash"              59 minutes ago   Up 55 minutes                           exciting_chaplygin
a88076332788   ubuntu:latest   "/bin/bash"              3 hours ago      Exited (137) 3 hours ago                jovial_sutherland
[root@k8s-master ~]# docker rm -f 1b68a19ef4a2
1b68a19ef4a2
[root@k8s-master ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                        PORTS     NAMES
048f0e109be5   ubuntu:latest   "/bin/sh -c 'while t…"   27 minutes ago   Exited (137) 12 minutes ago             xenodochial_herschel
4a41f6768877   ubuntu:latest   "/bin/bash"              33 minutes ago   Exited (0) 31 minutes ago               laughing_turing
ee0cb3389875   nginx-test      "/docker-entrypoint.…"   42 minutes ago   Exited (0) 42 minutes ago               youthful_ptolemy
a88076332788   ubuntu:latest   "/bin/bash"              3 hours ago      Exited (137) 3 hours ago                jovial_sutherland

9.导入和导出容器

9.1 导入容器

docker export -o test.tar 048f0e109be5

[root@k8s-master ~]# docker export -o test.tar 048f0e109be5
[root@k8s-master ~]# ls
anaconda-ks.cfg  Dockerfile  docker-tag.sh  dockertags.sh  original-ks.cfg  ssh_test.sh  test.tar  ubuntu.tar

9.2 导出容器

docker import test.tar test/ubuntu:0.2

[root@k8s-master ~]# docker import test.tar test/ubuntu:0.2
sha256:515a769c6033adb2317cf1028683fdf179c678852695feb3db6158c418d497f2
[root@k8s-master ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
test/ubuntu   0.2       515a769c6033   20 seconds ago   77.8MB
nginx-test    latest    72e95c88b023   3 hours ago      187MB
test          0.1       d766b1cab14e   3 hours ago      77.8MB
ubuntu        latest    c6b84b685f35   5 weeks ago      77.8MB

10.查看容器

主要三个命令:inspect、top、stats

10.1 查看容器详细信息

docker inspect 048f0e109be5

[root@k8s-master ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                        PORTS     NAMES
048f0e109be5   ubuntu:latest   "/bin/sh -c 'while t…"   31 minutes ago   Exited (137) 16 minutes ago             xenodochial_herschel
4a41f6768877   ubuntu:latest   "/bin/bash"              38 minutes ago   Exited (0) 35 minutes ago               laughing_turing
ee0cb3389875   nginx-test      "/docker-entrypoint.…"   47 minutes ago   Exited (0) 47 minutes ago               youthful_ptolemy
a88076332788   ubuntu:latest   "/bin/bash"              3 hours ago      Exited (137) 3 hours ago                jovial_sutherland
[root@k8s-master ~]# docker inspect  048f0e109be5
[
    {
        "Id": "048f0e109be5b77caecf7dbbb324c18faa75b337081383a637bf23d804c9330c",
        "Created": "2023-09-22T08:04:10.732808074Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo 'hello world';sleep 2;done"
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 137,
            "Error": "",
            "StartedAt": "2023-09-22T08:04:11.108208327Z",
            "FinishedAt": "2023-09-22T08:18:53.865953641Z"
        },
        "Image": "sha256:c6b84b685f35f1a5d63661f5d4aa662ad9b7ee4f4b8c394c022f25023c907b65",
        "ResolvConfPath": "/var/lib/docker/containers/048f0e109be5b77caecf7dbbb324c18faa75b337081383a637bf23d804c9330c/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/048f0e109be5b77caecf7dbbb324c18faa75b337081383a637bf23d804c9330c/hostname",
        "HostsPath": "/var/lib/docker/containers/048f0e109be5b77caecf7dbbb324c18faa75b337081383a637bf23d804c9330c/hosts",
        "LogPath": "/var/lib/docker/containers/048f0e109be5b77caecf7dbbb324c18faa75b337081383a637bf23d804c9330c/048f0e109be5b77caecf7dbbb324c18faa75b337081383a637bf23d804c9330c-json.log",
        "Name": "/xenodochial_herschel",
        "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": [
                55,
                231
            ],
            "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/4b94629f76df5425b8725d69339f8391fe26f52c0c038a9429b57fecb67fcbd5-init/diff:/var/lib/docker/overlay2/fcf536c198e10b2268b678fee88c581d2389d5f55d48f5662cd5187aecb72329/diff",
                "MergedDir": "/var/lib/docker/overlay2/4b94629f76df5425b8725d69339f8391fe26f52c0c038a9429b57fecb67fcbd5/merged",
                "UpperDir": "/var/lib/docker/overlay2/4b94629f76df5425b8725d69339f8391fe26f52c0c038a9429b57fecb67fcbd5/diff",
                "WorkDir": "/var/lib/docker/overlay2/4b94629f76df5425b8725d69339f8391fe26f52c0c038a9429b57fecb67fcbd5/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "048f0e109be5",
            "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",
                "while true;do echo 'hello world';sleep 2;done"
            ],
            "Image": "ubuntu:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.opencontainers.image.ref.name": "ubuntu",
                "org.opencontainers.image.version": "22.04"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "0e5faba17150587cc0738d844aaaa975fc6c713102b8ef9cc0872031ab7c3445",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/0e5faba17150",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "65c39da08a2e2b26d28dd2a903c9d20d65ba896380c86870413f0985f0244f72",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]

10.2 查看容器内进程信息

docker top 3b6d47f0a2f7

[root@k8s-master ~]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED         STATUS         PORTS     NAMES
3b6d47f0a2f7   ubuntu:latest   "/bin/sh -c 'while t…"   9 seconds ago   Up 7 seconds             optimistic_cerf
[root@k8s-master ~]# docker top 3b6d47f0a2f7
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                30697               30674               95                  01:40               ?                   00:00:13            /bin/sh -c while true;do echo 'hello world';sleep2;done

10.3 查看容器资源信息

docker stats 3b6d47f0a2f7

docker stats 3b6d47f0a2f7
CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT   MEM %     NET I/O     BLOCK I/O   PIDS
3b6d47f0a2f7   optimistic_cerf   100.34%   112KiB / 1.777GiB   0.01%     656B / 0B   0B / 0B     1

CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT   MEM %     NET I/O     BLOCK I/O   PIDS
3b6d47f0a2f7   optimistic_cerf   100.34%   112KiB / 1.777GiB   0.01%     656B / 0B   0B / 0B     1

11.docker其他常用命令

主要有:cp、diff、port、update

11.1复制文件

docker cp ssh_test.sh 3b6d47f0a2f7:/tmp #将本地文件复制到docker容器内

[root@k8s-master ~]# docker cp ssh_test.sh 3b6d47f0a2f7:/tmp
Successfully copied 3.58kB to 3b6d47f0a2f7:/tmp

docker cp 3b6d47f0a2f7:/tmp/ssh_test.sh ~/wyx.sh #将docker容器内文件复制到本地

[root@k8s-master ~]# docker cp 3b6d47f0a2f7:/tmp/ssh_test.sh ~/wyx.sh
Successfully copied 3.58kB to /root/wyx.sh

11.2 查看变更

docker container diff 3b6d47f0a2f7

[root@k8s-master ~]# docker container diff 3b6d47f0a2f7
C /tmp
A /tmp/ssh_test.sh

C:change   A:add

11.3 查看端口映射

docker container port 3b6d47f0a2f7
 

[root@k8s-master ~]# docker container port 3b6d47f0a2f7
[root@k8s-master ~]# 
#显示无表示无端口映射出去

tips:

1.docker查看资源信息

查看内存使用信息:

docker stats --no-stream |awk 'NR>1{print "dockerid:"$1,"CPU:"$3}'

docker 查看内存使用信息:docker stats --no-stream |awk '{print "dockerid:"$1,"MEM:"$4}'

 2.清理Exited状态的容器脚本

[root@k8s-master docker]# for i in `docker ps -a|grep "Exited"|awk '{print $1}'`
> do
> docker rm $i
> done
e793c6ce0911
759c07f1e4b2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值