docker-06

2022-04-15 16:14:34 浏览数 (1)

今天更新到06了,前五章的链接如下:

docker-01

docker-02

docker-03

docker-04

docker-05

今天我们继续用讲解关于容器的指令

一.列出所有当前在运行的容器实例

其实这条指令我们之前已经多次用到过

他就是

代码语言:javascript复制
docker ps

返回如下:

代码语言:javascript复制
CONTAINER ID   IMAGE         COMMAND                  CREATED        STATUS        PORTS                                       NAMES
ecca3c058a18   redis:6.0.8   "docker-entrypoint.s…"   3 hours ago    Up 3 hours    0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   serene_shockley
c34bf70a31a6   ubuntu        "bash"                   20 hours ago   Up 20 hours

我们再查看一下帮助手册

输入:

代码语言:javascript复制
docker ps --help

返回:

代码语言:javascript复制
Usage:  docker ps [OPTIONS]

List containers

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -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

虽然这个我们在之前的篇章中介绍过,但是这次算是正规介绍吧

-s就是代表显示每个镜像的总大小

-n就是现实最近创建的几个容器

代码语言:javascript复制
[root@happy2022 ~] docker ps -n 1
CONTAINER ID   IMAGE         COMMAND                  CREATED       STATUS       PORTS                                       NAMES
ecca3c058a18   redis:6.0.8   "docker-entrypoint.s…"   3 hours ago   Up 3 hours   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   serene_shockley

二.容器的启动&停止相关指令

2.1启动已经停止运行的容器

代码语言:javascript复制
docker start  容器id或者容器名

2.2重启容器

代码语言:javascript复制
docker restart 容器id或容器名

2.3停止容器

代码语言:javascript复制
docker stop 容器id或容器名

2.4强制停止容器

代码语言:javascript复制
docker kill 容器id或容器名

2.5删除已停止的容器

代码语言:javascript复制
docker rm 容器ID

2.5.1一次性删除多个容器实例

代码语言:javascript复制
docker rm -f ${docker ps -a -q}
或
docker ps -a -q|xargs docker rm

三.启动守护式容器

我们在docker-05的时候已经用过了一条指令

虽然没有介绍,但是我觉得大家应该能懂其实那就是后台守护式的启动方式

这里举个例子,如果我们想启动redis

有两种办法

第一种:

代码语言:javascript复制
docker run -it redies:6.0.8

输出如下:

代码语言:javascript复制
1:C 04 Mar 2022 11:09:09.082 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 04 Mar 2022 11:09:09.082 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 04 Mar 2022 11:09:09.082 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.8 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1:M 04 Mar 2022 11:09:09.084 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 04 Mar 2022 11:09:09.084 # Server initialized
1:M 04 Mar 2022 11:09:09.084 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 04 Mar 2022 11:09:09.084 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
1:M 04 Mar 2022 11:09:09.085 * Ready to accept connections

没错典型的redis启动页面

这样就是前台交互式的启动范式,因为这样我们可以输入命令

第二种启动方式就是后台守护式启动:

代码语言:javascript复制
docker run -d redis:6.0.8

我们启动完了之后可以再输入

代码语言:javascript复制
docker ps

查看一下是否启动成功

可以看到上图我们是启动成功了

四.相关指令

4.1查看容器日志

代码语言:javascript复制
docker logs 容器id
代码语言:javascript复制
[root@happy2022 ~]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS                                       NAMES
5011826fe018   redis:6.0.8   "docker-entrypoint.s…"   3 seconds ago   Up 2 seconds   6379/tcp                                    focused_morse
ecca3c058a18   redis:6.0.8   "docker-entrypoint.s…"   3 hours ago     Up 3 hours     0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   serene_shockley
c34bf70a31a6   ubuntu        "bash"                   21 hours ago    Up 21 hours                                                sharp_hamilton
[root@happy2022 ~]# docker logs 5011826fe018
1:C 04 Mar 2022 11:11:09.941 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 04 Mar 2022 11:11:09.941 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 04 Mar 2022 11:11:09.941 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 04 Mar 2022 11:11:09.944 * Running mode=standalone, port=6379.
1:M 04 Mar 2022 11:11:09.944 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 04 Mar 2022 11:11:09.944 # Server initialized
1:M 04 Mar 2022 11:11:09.944 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 04 Mar 2022 11:11:09.944 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
1:M 04 Mar 2022 11:11:09.945 * Ready to accept connections

4.2查看容器内运行的进程

代码语言:javascript复制
docker top 容器ID
代码语言:javascript复制
[root@happy2022 ~]# docker top 5011826fe018
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
polkitd             27662               27641               0                   06:11               ?                   00:00:00            redis-server *:6379

4.3查看容器内部细节(重要)

代码语言:javascript复制
docker inspect 容器ID
代码语言:javascript复制
[root@happy2022 ~]# docker inspect 5011826fe018
[
    {
        "Id": "5011826fe0188920a4513db6f3f99ec3c56f6a4707de8f1c0f5ea1ac71cdc2b6",
        "Created": "2022-03-04T11:11:09.105534353Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "redis-server"
],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 27662,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-03-04T11:11:09.918138588Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:16ecd277293476392b71021cdd585c40ad68f4a7488752eede95928735e39df4",
        "ResolvConfPath": "/var/lib/docker/containers/5011826fe0188920a4513db6f3f99ec3c56f6a4707de8f1c0f5ea1ac71cdc2b6/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/5011826fe0188920a4513db6f3f99ec3c56f6a4707de8f1c0f5ea1ac71cdc2b6/hostname",
        "HostsPath": "/var/lib/docker/containers/5011826fe0188920a4513db6f3f99ec3c56f6a4707de8f1c0f5ea1ac71cdc2b6/hosts",
        "LogPath": "/var/lib/docker/containers/5011826fe0188920a4513db6f3f99ec3c56f6a4707de8f1c0f5ea1ac71cdc2b6/5011826fe0188920a4513db6f3f99ec3c56f6a4707de8f1c0f5ea1ac71cdc2b6-json.log",
        "Name": "/focused_morse",
        "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/58c407e49aa6c52bf5b1753072a921614d813058e5f62f440cf0999f88856d78-init/diff:/var/lib/docker/overlay2/6548401235fc25b8a6ac9f7a701ccf89a1ac6f8d878211275c673cb32719c0bb/diff:/var/lib/docker/overlay2/08999e5da959617e44774dc234e8f685baa96b47eef241ba777e9e7b74af53a5/diff:/var/lib/docker/overlay2/c83319b25f97bde9c637fb2aaaefa71922863c6fa64f9e38a89be8e3fbcb2061/diff:/var/lib/docker/overlay2/bb8052a1aa0df6589edf28d1ea4b44f4c184322a07cfc374dffe899561031068/diff:/var/lib/docker/overlay2/17e35bfc55190175c78747408d7bc23a2bb7015994eee77ee240300881e58c1e/diff:/var/lib/docker/overlay2/4ec0b5fd10a333cef689554467a5eebcff036a288ca7b9cd50f712bc9170387e/diff",
                "MergedDir": "/var/lib/docker/overlay2/58c407e49aa6c52bf5b1753072a921614d813058e5f62f440cf0999f88856d78/merged",
                "UpperDir": "/var/lib/docker/overlay2/58c407e49aa6c52bf5b1753072a921614d813058e5f62f440cf0999f88856d78/diff",
                "WorkDir": "/var/lib/docker/overlay2/58c407e49aa6c52bf5b1753072a921614d813058e5f62f440cf0999f88856d78/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "59836f25301361ab517f5b3b01533ba5588744820a8ac77d5c5483cec6e4c068",
                "Source": "/var/lib/docker/volumes/59836f25301361ab517f5b3b01533ba5588744820a8ac77d5c5483cec6e4c068/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "5011826fe018",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "6379/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.12",
                "REDIS_VERSION=6.0.8",
                "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.0.8.tar.gz",
                "REDIS_DOWNLOAD_SHA=04fa1fddc39bd1aecb6739dd5dd73858a3515b427acd1e2947a66dadce868d68"
            ],
            "Cmd": [
                "redis-server"
            ],
            "Image": "redis:6.0.8",
            "Volumes": {
                "/data": {}
            },
            "WorkingDir": "/data",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "848e759a15720c80de75d00d22506cc6af4b9e521f136db832b9ea9dc6c61f23",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "6379/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/848e759a1572",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "631558821ba11c68885cec6ece5bab8f8af27e13b72ee88ef581ef3f09dfdbb9",
            "Gateway": "15.7.0.31",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "85.7.3.45",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:02:ac:55:G0:04",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "67efff5f91a90b8dbc28f59e146b66e74e75513361516983e34e4e4a6bb4786a",
                    "EndpointID": "631558821ba11c68885cec6ece5bab8f8af27e13b72ee88ef581ef3f09dfdbb9",
                    "Gateway": "1.17.8.1",
                    "IPAddress": "1.7.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "xx:42:ac:11:00:04",
                    "DriverOpts": null
                }
            }
        }
    }
]

inspect命令很重要,这一点我们在后面会有所体现。

今天的分享就到此结束了,

SEE YOU

0 人点赞