docker-容器启动入门

容器操作基础命令

容器生命令周期

docker-容器启动入门插图

启动容器

docker run 可以启动容器,进入到容器,并随机生成容器ID和名称

帮助:man docker-run

命令格式:

docker run [选项]  [镜像名]  [shell命令] [参数]
选项:
-i, --interactive    Keep STDIN open even if not attached,通常和-t一起使用
-t, --tty            Allocate a pseudo-TTY,通常和-i一起使用
-d, --detach         Run container in background and print container ID,台后运行,默认前台
--name string        Assign a name to the container
--rm                 Automatically remove the container when it exits
-p, --publish list   Publish a container's port(s) to the host
-P, --publish-all    Publish all exposed ports to random ports
--dns list           Set custom DNS servers
--entrypoint string  Overwrite the default ENTRYPOINT of the image
--restart policy  
--privileged         Give extended privileges to container

有四种不同的policy

policy 说明
no Default is no,Do not automatically restart the container when it exits.
on-failure[:max-retries] on-failure[:max-retries] Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts.
always Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.
unless-stopped Always restart the container regardless of the exit status, but do not start it on daemon startup if the container has been put to a stopped state before.

退出容器并停止容器

 exit 

退出容器且容器不停止

同时按三个键,ctrl+p+q

范例:运行docker 的hello world

[root@centos8 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[root@centos8 ~]#docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        12 months ago       1.84kB
[root@centos8 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
7f53a2a74edc        hello-world         "/hello"            21 seconds ago      Exited (0) 19 seconds ago                     nifty_yalow

范例:

[root@ubuntu1804 ~]#docker run -it docker.io/busybox  sh
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
bdbbaa22dec6: Pull complete 
Digest: sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
Status: Downloaded newer image for busybox:latest
/ # exit
[root@ubuntu1804 ~]#docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d7ece7f62532        centos              "/bin/bash"         7 minutes ago       Up 7 minutes                            ecstatic_franklin

范例:

 #启动的容器在执行完shel命令就退出,用于测试
[root@ubuntu1804 ~]#docker run busybox /bin/echo "hello world"
hello world
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
dcdf71d17177        busybox             "/bin/echo 'hello wo…"   24 seconds ago      Exited (0) 23 seconds ago                       cool_jepsen

范例:

#启动容器,自动随机字符作为容器名
[root@ubuntu1804 ~]#docker run  alpine 
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
95967eaefd6a        alpine              "/bin/sh"           8 seconds ago       Exited (0) 6 seconds ago                       confident_elion

#指定容器名称,注意每个容器的名称要唯一
[root@ubuntu1804 ~]#docker run --name alpine1 alpine 
[root@ubuntu1804 ~]#docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
edd2ac2690e6        alpine              "/bin/sh"           9 seconds ago       Exited (0) 8 seconds ago                       alpine1

#创建容器后直接进入,执行exit退出后容器关闭
[root@ubuntu1804 ~]#docker run -it --name alpine2 alpine 
/ # cat /etc/issue
Welcome to Alpine Linux 3.11
Kernel /r on an /m (/l)

/ # exit
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6d64f47a83e6        alpine              "/bin/sh"           13 seconds ago      Exited (0) 6 seconds ago                        alpine2
edd2ac2690e6        alpine              "/bin/sh"           52 seconds ago      Exited (0) 51 seconds ago                       alpine1
[root@ubuntu1804 ~]#docker run -it --name alpine3 alpine 
/ # [root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
df428caf7128        alpine              "/bin/sh"           8 seconds ago        Up 7 seconds                                        alpine3
6d64f47a83e6        alpine              "/bin/sh"           26 seconds ago       Exited (0) 20 seconds ago                           alpine2
edd2ac2690e6        alpine              "/bin/sh"           About a minute ago   Exited (0) About a minute ago                       alpine1

#后台启动容器
[root@ubuntu1804 ~]#docker run -d --name alpine4 alpine 
3a05bbf66dac8a6ac9829c876bdb5fcb70832bf4a2898d68f6979cd8e8c517cb
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
3a05bbf66dac        alpine              "/bin/sh"           3 seconds ago        Exited (0) 2 seconds ago                            alpine4
df428caf7128        alpine              "/bin/sh"           30 seconds ago       Up 28 seconds                                       alpine3
6d64f47a83e6        alpine              "/bin/sh"           48 seconds ago       Exited (0) 41 seconds ago                           alpine2
edd2ac2690e6        alpine              "/bin/sh"           About a minute ago   Exited (0) About a minute ago                       alpine1
[root@ubuntu1804 ~]#docker run -td --name alpine5 alpine 
868b33da850cfcc7db8b84150fb9c7686b577889f10425bb4c5e17f28cf68a29
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
868b33da850c        alpine              "/bin/sh"           2 seconds ago        Up 1 second                                         alpine5
3a05bbf66dac        alpine              "/bin/sh"           23 seconds ago       Exited (0) 23 seconds ago                           alpine4
df428caf7128        alpine              "/bin/sh"           50 seconds ago       Up 49 seconds                                       alpine3
6d64f47a83e6        alpine              "/bin/sh"           About a minute ago   Exited (0) About a minute ago                       alpine2
edd2ac2690e6        alpine              "/bin/sh"           About a minute ago   Exited (0) About a minute ago                       alpine1
[root@ubuntu1804 ~]#

范例:一次性运行容器,退出后立即删除,用于测试

[root@ubuntu1804 ~]#docker run -it --rm  alpine cat /etc/issue
Welcome to Alpine Linux 3.11
Kernel /r on an /m (/l)

[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

范例:开机自动运行容器

#默认容器不会自动启动
[root@ubuntu1804 ~]#docker run -d --name nginx  -p 80:80  nginx 
bce473b8b1d2f728847cdc32b664cca1bd7578bf7bdac850b501e2e5557a718a
[root@ubuntu1804 ~]#docker ps 
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                            NAMES
bce473b8b1d2        nginx                 "nginx -g 'daemon of…"   3 seconds ago       Up 2 seconds        0.0.0.0:80->80/tcp  
[root@ubuntu1804 ~]#reboot
[root@ubuntu1804 ~]#docker ps
CONTAINER ID        IMAGE       COMMAND     CREATED    STATUS       PORTS         NAMES

#设置容器总是运行
[root@ubuntu1804 ~]#docker run -d --name nginx --restart=always -p 80:80  nginx
[root@ubuntu1804 ~]#reboot
[root@ubuntu1804 ~]#docker ps 
CONTAINER ID   IMAGE        COMMAND     CREATED        STATUS      PORTS    NAMES
dbdba90076e1 nginx"nginx -g 'daemon of…"  About a minute agoUp 49 seconds0.0.0.0:80->80/tcp   nginx

–privileged 选项

大约在0.6版,–privileged 选项被引入docker。使用该参数,container内的root拥有真正的root权限。
否则,container内的root只是外部的一个普通用户权限。privileged启动的容器,可以看到很多host上的设备,并且可以执行mount。甚至允许你在docker容器中启动docker容器。

范例:使用–privileged 让容器获取 root 权限

[root@centos8 ~]#podman run -it   centos
[root@382ab09932a7 /]#cat /etc/redhat-release 
CentOS Linux release 8.1.1911 (Core) 
[root@382ab09932a7 /]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
|-sda1   8:1    0    1G  0 part 
|-sda2   8:2    0  100G  0 part 
|-sda3   8:3    0   50G  0 part 
|-sda4   8:4    0    1K  0 part 
`-sda5   8:5    0    2G  0 part [SWAP]
sr0     11:0    1    7G  0 rom  
[root@382ab09932a7 /]# mount /dev/sda3 /mnt
mount: /mnt: permission denied.
[root@382ab09932a7 /]# exit
exit

#利用--privileged 选项运行容器
[root@centos8 ~]#podman run -it --privileged   centos
#可以看到宿主机的设备
[root@a6391a8f82e3 /]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
|-sda1   8:1    0    1G  0 part 
|-sda2   8:2    0  100G  0 part 
|-sda3   8:3    0   50G  0 part 
|-sda4   8:4    0    1K  0 part 
`-sda5   8:5    0    2G  0 part [SWAP]
sr0     11:0    1    7G  0 rom  
[root@a6391a8f82e3 /]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
overlay        104806400 2754832 102051568   3% /
tmpfs              65536       0     65536   0% /dev
tmpfs             408092    5892    402200   2% /etc/hosts
shm                64000       0     64000   0% /dev/shm
tmpfs             408092       0    408092   0% /sys/fs/cgroup
[root@a6391a8f82e3 /]# mount /dev/sda5 /mnt
mount: /mnt: unknown filesystem type 'swap'.
[root@a6391a8f82e3 /]# mount /dev/sda3 /mnt
[root@a6391a8f82e3 /]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
overlay        104806400 2754632 102051768   3% /
tmpfs              65536       0     65536   0% /dev
tmpfs             408092    5892    402200   2% /etc/hosts
shm                64000       0     64000   0% /dev/shm
tmpfs             408092       0    408092   0% /sys/fs/cgroup
/dev/sda3       52403200  619068  51784132   2% /mnt
[root@a6391a8f82e3 /]# touch /mnt/containter.txt
[root@a6391a8f82e3 /]# echo container data > /mnt/containter.txt
[root@a6391a8f82e3 /]# cat  /mnt/containter.txt
container data
[root@a6391a8f82e3 /]#

#在宿主机查看是否生成文件
[root@centos8 ~]#lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0   50G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]
sr0     11:0    1    7G  0 rom 
[root@centos8 ~]#ll /data/containter.txt
-rw-r--r-- 1 root root 25 Feb 29 12:26 /data/containter.txt
[root@centos8 ~]#cat /data/containter.txt 
container data
[root@centos8 ~]#echo host data >> /data/containter.txt
[root@centos8 ~]#cat /data/containter.txt 
container data
host data

#在容器内可看文件是否发生变化
[root@a6391a8f82e3 /]# cat  /mnt/containter.txt
container data
host data

范例:运行docker官方文档容器

[root@centos8 ~]#podman run -it -d -p 4000:4000 docs/docker.github.io:latest
[root@centos8 ~]#podman images docs/docker.github.io
REPOSITORY                        TAG      IMAGE ID       CREATED      SIZE
docker.io/docs/docker.github.io   latest   ffd9131eeee7   2 days ago   1.99 GB
#用浏览器访问http://localhost:4000/可以看到下面docker文档资料

docker-容器启动入门插图(1)

显示容器
docker ps 可以显示当前存在容器

格式

docker ps [OPTIONS]
选项:
-a, --all             Show all containers (default shows just running)
-q, --quiet           Only display numeric IDs
-s, --size            Display total file sizes
-f, --filter filter   Filter output based on conditions provided
-l, --latest          Show the latest created container (includes all states)
-n, --last int        Show n last created containers (includes all states) (default -1)

范例:

#显示运行的容器
[root@ubuntu1804 ~]#docker ps  
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d7ece7f62532        centos              "/bin/bash"         23 seconds ago      Up 22 seconds                           ecstatic_franklin

#显示全部容器,包括退出状态的容器
[root@ubuntu1804 ~]#docker ps  -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
d7ece7f62532        centos              "/bin/bash"              27 seconds ago      Up 26 seconds                                  ecstatic_franklin
dcdf71d17177        busybox             "/bin/echo 'hello wo…"   8 minutes ago       Exited (0) 8 minutes ago                       cool_jepsen

#只显示容器ID
[root@ubuntu1804 ~]#docker ps  -a -q
d7ece7f62532
dcdf71d17177

#显示容器大小
[root@ubuntu1804 ~]#docker ps  -a -s
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES               SIZE
d7ece7f62532        centos              "/bin/bash"              51 seconds ago      Up 50 seconds                                  ecstatic_franklin   0B (virtual 237MB)
dcdf71d17177        busybox             "/bin/echo 'hello wo…"   8 minutes ago       Exited (0) 8 minutes ago                       cool_jepsen         0B (virtual 1.22MB)
[root@ubuntu1804 ~]             

范例:显示指定的容器

[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
dd002f947cbe        nginx               "nginx -g 'daemon of…"   19 minutes ago      Exited (137) 11 minutes ago                       nginx2
1f3f82995e05        nginx               "nginx -g 'daemon of…"   19 minutes ago      Up 2 minutes                  80/tcp              nginx1
[root@ubuntu1804 ~]#docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1f3f82995e05        nginx               "nginx -g 'daemon of…"   19 minutes ago      Up 2 minutes        80/tcp              nginx1
[root@ubuntu1804 ~]#docker ps  -f 'status=exited'
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
dd002f947cbe        nginx               "nginx -g 'daemon of…"   19 minutes ago      Exited (137) 11 minutes ago                       nginx2
[root@ubuntu1804 ~]#
查看容器内的进程
docker top CONTAINER [ps OPTIONS]

范例:

[root@ubuntu1804 ~]#docker run -d  httpd
db144f1978148242dc20bd0be951628f1c00371b2c69dee53d84469c52995d8f
[root@ubuntu1804 ~]#docker top db14
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                9821                9797                3                   22:02               ?                   00:00:00            httpd -DFOREGROUND
daemon              9872                9821                0                   22:02               ?                   00:00:00            httpd -DFOREGROUND
daemon              9873                9821                0                   22:02               ?                   00:00:00            httpd -DFOREGROUND
daemon              9874                9821                0                   22:02               ?                   00:00:00            httpd -DFOREGROUND

[root@ubuntu1804 ~]#docker run -d  alpine /bin/sh -c 'i=1;while true;do echo helloi;let i++;sleep 1;done'
9997053f9766d4adf709d46161d7ec6739eacafbe8d4721133874b89112ad1a6
[root@ubuntu1804 ~]#docker top 9997
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                10023               9997                3                   22:03               ?                   00:00:00            /bin/sh -c i=1;while true;do echo helloi;let i++;sleep 1;done
root                10074               10023               0                   22:03               ?                   00:00:00            sleep 1
[root@ubuntu1804 ~]#
查看容器的详细信息
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container

范例:

[root@ubuntu1804 ~]#docker inspect 9997
[
{
"Id": "9997053f9766d4adf709d46161d7ec6739eacafbe8d4721133874b89112ad1a6",
"Created": "2020-02-25T14:03:00.790597711Z",
"Path": "/bin/sh",
"Args": [
"-c",
"i=1;while true;do echo helloi;let i++;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 10023,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-02-25T14:03:01.407282144Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:e7d92cdc71feacf90708cb59182d0df1b911f8ae022d29e8e95d75ca6a99776a",
"ResolvConfPath": "/var/lib/docker/containers/9997053f9766d4adf709d46161d7ec6739eacafbe8d4721133874b89112ad1a6/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/9997053f9766d4adf709d46161d7ec6739eacafbe8d4721133874b89112ad1a6/hostname",
"HostsPath": "/var/lib/docker/containers/9997053f9766d4adf709d46161d7ec6739eacafbe8d4721133874b89112ad1a6/hosts",
"LogPath": "/var/lib/docker/containers/9997053f9766d4adf709d46161d7ec6739eacafbe8d4721133874b89112ad1a6/9997053f9766d4adf709d46161d7ec6739eacafbe8d4721133874b89112ad1a6-json.log",
"Name": "/gracious_wescoff",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"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,
"Capabilities": null,
"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/27f7bf9c29a0303e2526bd7fa7d61fcc3a2ef400e6948b5d9487b5b30f506853-init/diff:/var/lib/docker/overlay2/cb3f60a3d09e0555d06525b70cc30975a3cb70e23c87e7e46bd44b6cfffbcda0/diff",
"MergedDir": "/var/lib/docker/overlay2/27f7bf9c29a0303e2526bd7fa7d61fcc3a2ef400e6948b5d9487b5b30f506853/merged",
"UpperDir": "/var/lib/docker/overlay2/27f7bf9c29a0303e2526bd7fa7d61fcc3a2ef400e6948b5d9487b5b30f506853/diff",
"WorkDir": "/var/lib/docker/overlay2/27f7bf9c29a0303e2526bd7fa7d61fcc3a2ef400e6948b5d9487b5b30f506853/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "9997053f9766",
"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",
"i=1;while true;do echo helloi;let i++;sleep 1;done"
],
"Image": "alpine",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "4823297a262eaad3b63778c2fe04a74e38ce401c22404f05f3cf54ab7dc188e2",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/4823297a262e",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "019896bd99c9e158ef9f97b1617f1638fee94c7f92c9250e1ac1bfe58c97c911",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "d407310ec8ff57a6623ae2fa01ba860ab0615135585a00b19f0751561c99ab16",
"EndpointID": "019896bd99c9e158ef9f97b1617f1638fee94c7f92c9250e1ac1bfe58c97c911",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
[root@ubuntu1804 ~]#
删除容器

docker rm 可以删除容器,即使容正在运行当中,也可以被强制删除掉

格式

docker rm [OPTIONS] CONTAINER [CONTAINER...]
选项:
-f, --force     Force the removal of a running container (uses SIGKILL)
-v, --volumes   Remove the volumes associated with the container

范例:

[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
868b33da850c        alpine              "/bin/sh"           2 minutes ago       Up 2 minutes                                   alpine5
3a05bbf66dac        alpine              "/bin/sh"           3 minutes ago       Exited (0) 3 minutes ago                       alpine4
df428caf7128        alpine              "/bin/sh"           3 minutes ago       Up 3 minutes                                   alpine3
6d64f47a83e6        alpine              "/bin/sh"           3 minutes ago       Exited (0) 3 minutes ago                       alpine2
edd2ac2690e6        alpine              "/bin/sh"           4 minutes ago       Exited (0) 4 minutes ago                       alpine1
[root@ubuntu1804 ~]#docker rm 3a05bbf66dac
3a05bbf66dac
[root@ubuntu1804 ~]#docker rm alpine5
Error response from daemon: You cannot remove a running container 868b33da850cfcc7db8b84150fb9c7686b577889f10425bb4c5e17f28cf68a29. Stop the container before attempting removal or force remove
[root@ubuntu1804 ~]#docker rm -f  alpine5
alpine5

范例:删除所有容器

[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
df428caf7128        alpine              "/bin/sh"           4 minutes ago       Up 4 minutes                                   alpine3
6d64f47a83e6        alpine              "/bin/sh"           4 minutes ago       Exited (0) 4 minutes ago                       alpine2
edd2ac2690e6        alpine              "/bin/sh"           5 minutes ago       Exited (0) 5 minutes ago                       alpine1
[root@ubuntu1804 ~]#docker rm -f docker ps -a -q
df428caf7128
6d64f47a83e6
edd2ac2690e6
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@ubuntu1804 ~]#

范例:删除指定状态的容器

[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
dd002f947cbe        nginx               "nginx -g 'daemon of…"   22 minutes ago      Exited (137) 14 minutes ago                       nginx2
1f3f82995e05        nginx               "nginx -g 'daemon of…"   22 minutes ago      Up 4 minutes                  80/tcp              nginx1
[root@ubuntu1804 ~]#docker rm docker ps -qf status=exited
dd002f947cbe
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1f3f82995e05        nginx               "nginx -g 'daemon of…"   22 minutes ago      Up 4 minutes        80/tcp              nginx1
[root@ubuntu1804 ~]#

本文链接:http://www.yunweipai.com/34795.html

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/52650.html

(0)
上一篇 2021年8月6日
下一篇 2021年8月6日

相关推荐

发表回复

登录后才能评论