Docker - Docker Container及Container命令详解

娄飞鸾
2023-12-01

Docker - Docker Container及Container命令详解
什么是Docker容器(Docker Container)
上一篇博客介绍了Docker镜像(Docker Image)以及Image的一些命令:Docker - Docker Image及Image命令详解。

其实镜像(Image)和容器(Container)的关系,就像是程序和进程一样,镜像是静态的定义,容器则是动态的定义,是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。每个容器都是相互隔离的、保证安全的平台。可以把容器看做是一个简易版的Linux环境(包括root用户权限、进程空间、用户空间和网络空间等)和运行在其中的应用程序。 下图从顶层设计层面展示了镜像和容器间的关系。一旦容器从镜像上启动后,二者之间就变成了互相依赖的关系,并且在镜像上启动的容器全部停止之前,镜像是无法被删除的。尝试删除镜像而不停止或销毁使用它的容器,会导致出错。

现在大概知道Docker容器是什么了,那接下来开始介绍Docker容器的一些命令。

Docker PS
展示容器列表。

docker ps --help
1
[root@izoq008ryseuupz ~]# docker ps --help

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 numeric IDs
  -s, --size            Display total file sizes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
默认展示的是正在运行的容器(default shows just running),即状态是up的容器。

docker ps
1
[root@izoq008ryseuupz ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a258385ce592        centos:7            "/bin/bash"              5 hours ago         Up 5 hours                              centos.7.2
ae23a046566b        7eed8df88d3b        "docker-entrypoint.s…"   30 hours ago        Up 30 hours         6379/tcp            quirky_beaver
aa6a63e58682        7eed8df88d3b        "docker-entrypoint.s…"   30 hours ago        Up 30 hours         6379/tcp            goofy_montalcini
1655972bf5cd        7eed8df88d3b        "docker-entrypoint.s…"   31 hours ago        Up 31 hours         6379/tcp            cool_leakey
1
2
3
4
5
6
通过-a选项可以展示所有容器。

docker ps -a
1
[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                                        NAMES
a258385ce592        centos:7            "/bin/bash"              5 hours ago         Up 5 hours                                                              centos.7.2
cbd85da38a58        centos:7            "/bin/bash"              5 hours ago         Exited (0) 5 hours ago                                                  centos.7.1
74d0a8d032ff        centos:7            "/bin/bash"              5 hours ago         Exited (137) 5 hours ago                                                centos.7
fd00901dfbbb        kaven/hello:v1      "/hello"                 6 hours ago         Exited (1) 6 hours ago                                                  hardcore_babbage
fab31562aeac        kaven/hello:v1      "/hello"                 6 hours ago         Exited (1) 6 hours ago                                                  stoic_carson
fecbf8c1b956        kaven/hello:v1      "/hello"                 6 hours ago         Exited (1) 6 hours ago                                                  hello.v2
a4cfeb0679fd        kaven/hello:v1      "/hello"                 6 hours ago         Exited (1) 6 hours ago                                                  hello
ae23a046566b        7eed8df88d3b        "docker-entrypoint.s…"   30 hours ago        Up 30 hours                6379/tcp                                     quirky_beaver
aa6a63e58682        7eed8df88d3b        "docker-entrypoint.s…"   31 hours ago        Up 31 hours                6379/tcp                                     goofy_montalcini
5e73372c49d3        7eed8df88d3b        "docker-entrypoint.s…"   31 hours ago        Exited (0) 31 hours ago                                                 stoic_einstein
1655972bf5cd        7eed8df88d3b        "docker-entrypoint.s…"   31 hours ago        Up 31 hours                6379/tcp                                     cool_leakey
ed1eb87a4d97        7eed8df88d3b        "docker-entrypoint.s…"   31 hours ago        Exited (0) 31 hours ago                                                 frosty_dirac
da76541620b1        7eed8df88d3b        "docker-entrypoint.s…"   31 hours ago        Exited (0) 31 hours ago                                                 trusting_franklin
1539964c89a1        5377c9a2fb1f        "docker-entrypoint.s…"   5 weeks ago         Exited (0) 2 days ago                                                   peaceful_mayer
c1b657c7608c        98d8bb571885        "/usr/bin/entry /usr…"   8 weeks ago         Exited (111) 2 days ago    3306/tcp, 8080/tcp, 0.0.0.0:9090->9090/tcp   confident_hugle
64105665545b        1850194f377c        "/bin/sh -c /zipkin/…"   2 months ago        Exited (143) 2 days ago    9410/tcp, 0.0.0.0:9411->9411/tcp             zipkin
bc3a0ef096ed        7eed8df88d3b        "docker-entrypoint.s…"   7 months ago        Exited (0) 4 days ago                                                   awesome_shockley
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
其他选项可以自己试一试。

Docker Create
该docker create命令会在指定的镜像上创建可写的容器,然后将容器ID打印到STDOUT。之后,你可以随时使用 docker start命令来启动容器。当你想提前设置容器配置时,以便在需要时启动,此命令很有用。新容器的初始状态为created。

docker create --help
1
通过选项--help,可见docker create命令的选项非常多。

先拉取centos:7镜像。

docker image pull centos:7
1
[root@izoq008ryseuupz ~]# docker image pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
1
2
3
4
5
6
docker images
1
[root@izoq008ryseuupz ~]# docker images
REPOSITORY                                                     TAG                 IMAGE ID            CREATED             SIZE
centos                                                         7                   8652b9f0cb4c        9 days ago          204MB
1
2
3
在centos:7镜像上创建容器,通过--name选项将容器命名为centos.7,-it是-i和-t的简写。

docker create -it --name centos.7 centos:7
1
[root@izoq008ryseuupz ~]# docker create -it --name centos.7 centos:7
74d0a8d032ffb17492e1acaf1cedafb62c5c2ed5921a7fee6ced2d6a8758ab5a
1
2
查看容器是否创建成功,新容器的初始状态为created。

docker ps -a
1
[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                                        NAMES
74d0a8d032ff        centos:7            "/bin/bash"              7 seconds ago       Created                                                                  centos.7
1
2
3
其他选项可以自己试一试。

Docker Start
启动一个或多个已经停止的容器。

docker start --help
1
[root@izoq008ryseuupz ~]# docker start --help

Usage:    docker start [OPTIONS] CONTAINER [CONTAINER...]

Start one or more stopped containers

Options:
  -a, --attach                  Attach STDOUT/STDERR and forward signals
      --checkpoint string       Restore from this checkpoint
      --checkpoint-dir string   Use a custom checkpoint storage directory
      --detach-keys string      Override the key sequence for detaching a container
  -i, --interactive             Attach container's STDIN
1
2
3
4
5
6
7
8
9
10
11
12
启动容器centos.7,容器启动成功后,容器的状态会变成up。

docker start centos.7
1
[root@izoq008ryseuupz ~]# docker start centos.7
centos.7
[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                                        NAMES
74d0a8d032ff        centos:7            "/bin/bash"              35 seconds ago      Up 10 seconds                                                            centos.7
1
2
3
4
5
-i选项可以进入到容器内部。

docker start -i centos.7
1
[root@izoq008ryseuupz ~]# docker start -i centos.7
[root@dfe33f1c3ab1 /]# ll
total 60
-rw-r--r--  1 root root 12114 Nov 13 01:55 anaconda-post.log
lrwxrwxrwx  1 root root     7 Nov 13 01:53 bin -> usr/bin
drwxr-xr-x  5 root root   360 Nov 23 14:26 dev
drwxr-xr-x  1 root root  4096 Nov 23 11:34 etc
drwxr-xr-x  2 root root  4096 Apr 11  2018 home
-rw-r--r--  1 root root    14 Nov 23 12:14 kaven
lrwxrwxrwx  1 root root     7 Nov 13 01:53 lib -> usr/lib
lrwxrwxrwx  1 root root     9 Nov 13 01:53 lib64 -> usr/lib64
drwxr-xr-x  2 root root  4096 Apr 11  2018 media
drwxr-xr-x  2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x  2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x 89 root root     0 Nov 23 14:26 proc
dr-xr-x---  1 root root  4096 Nov 23 11:34 root
drwxr-xr-x 11 root root  4096 Nov 13 01:55 run
lrwxrwxrwx  1 root root     8 Nov 13 01:53 sbin -> usr/sbin
drwxr-xr-x  2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x 13 root root     0 Apr 11  2018 sys
drwxrwxrwt  7 root root  4096 Nov 13 01:55 tmp
drwxr-xr-x 13 root root  4096 Nov 13 01:53 usr
drwxr-xr-x 18 root root  4096 Nov 13 01:54 var
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
其他选项可以自己试一试。

Docker Stop
停止一个或多个运行中的容器。

docker stop --help
1
[root@izoq008ryseuupz ~]# docker stop --help

Usage:    docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers

Options:
  -t, --time int   Seconds to wait for stop before killing it (default 10)
1
2
3
4
5
6
7
8
停止容器centos.7,容器停止成功后,容器的状态会变成exited。

docker stop centos.7
1
[root@izoq008ryseuupz ~]# docker stop centos.7
centos.7
[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS                                        NAMES
74d0a8d032ff        centos:7            "/bin/bash"              16 minutes ago      Exited (137) About a minute ago                                                centos.7
1
2
3
4
5
-t选项是在停止容器前等待数秒(默认值10)。

docker stop -t 50 centos.7
1
[root@izoq008ryseuupz ~]# docker stop -t 50 centos.7
centos.7.2
1
2
Docker Run
运行容器。

docker run --help
1
通过选项--help,可见docker run命令的选项非常多。

docker run -it --name centos.7.1 centos:7
1
该命令会在镜像centos:7上创建并运行容器 centos.7.1,如果Docker已经拉取过centos:7镜像,则该命令会直接创建一个基于镜像centos:7的容器 centos.7.1,否则会先去仓库拉取该centos:7镜像,如果没有指定版本,默认拉取:latest最新版,拉取成功后再运行该拉取的centos:7镜像,生成一个容器 centos.7.1,最后再运行该容器。

[root@izoq008ryseuupz ~]# docker run -it --name centos.7.1 centos:7
[root@cbd85da38a58 /]# ll
total 56
-rw-r--r--  1 root root 12114 Nov 13 01:55 anaconda-post.log
lrwxrwxrwx  1 root root     7 Nov 13 01:53 bin -> usr/bin
drwxr-xr-x  5 root root   360 Nov 23 05:03 dev
drwxr-xr-x  1 root root  4096 Nov 23 05:03 etc
drwxr-xr-x  2 root root  4096 Apr 11  2018 home
lrwxrwxrwx  1 root root     7 Nov 13 01:53 lib -> usr/lib
lrwxrwxrwx  1 root root     9 Nov 13 01:53 lib64 -> usr/lib64
drwxr-xr-x  2 root root  4096 Apr 11  2018 media
drwxr-xr-x  2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x  2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x 92 root root     0 Nov 23 05:03 proc
dr-xr-x---  2 root root  4096 Nov 13 01:55 root
drwxr-xr-x 11 root root  4096 Nov 13 01:55 run
lrwxrwxrwx  1 root root     8 Nov 13 01:53 sbin -> usr/sbin
drwxr-xr-x  2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x 13 root root     0 Apr 11  2018 sys
drwxrwxrwt  7 root root  4096 Nov 13 01:55 tmp
drwxr-xr-x 13 root root  4096 Nov 13 01:53 usr
drwxr-xr-x 18 root root  4096 Nov 13 01:54 var
[root@cbd85da38a58 /]# exit
exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
exit容器后,容器的状态就是exited了。

[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                                        NAMES
cbd85da38a58        centos:7            "/bin/bash"              5 minutes ago       Exited (0) 38 seconds ago                                                  centos.7.1
1
2
3
docker run -it --name centos.7.1 centos:7和docker start -i centos.7.1类似,只不过后者需要先创建容器。

[root@izoq008ryseuupz ~]# docker start -i centos.7.1
[root@cbd85da38a58 /]# ll
total 56
-rw-r--r--  1 root root 12114 Nov 13 01:55 anaconda-post.log
lrwxrwxrwx  1 root root     7 Nov 13 01:53 bin -> usr/bin
drwxr-xr-x  5 root root   360 Nov 23 05:10 dev
drwxr-xr-x  1 root root  4096 Nov 23 05:03 etc
drwxr-xr-x  2 root root  4096 Apr 11  2018 home
lrwxrwxrwx  1 root root     7 Nov 13 01:53 lib -> usr/lib
lrwxrwxrwx  1 root root     9 Nov 13 01:53 lib64 -> usr/lib64
drwxr-xr-x  2 root root  4096 Apr 11  2018 media
drwxr-xr-x  2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x  2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x 93 root root     0 Nov 23 05:10 proc
dr-xr-x---  1 root root  4096 Nov 23 05:08 root
drwxr-xr-x 11 root root  4096 Nov 13 01:55 run
lrwxrwxrwx  1 root root     8 Nov 13 01:53 sbin -> usr/sbin
drwxr-xr-x  2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x 13 root root     0 Apr 11  2018 sys
drwxrwxrwt  7 root root  4096 Nov 13 01:55 tmp
drwxr-xr-x 13 root root  4096 Nov 13 01:53 usr
drwxr-xr-x 18 root root  4096 Nov 13 01:54 var
[root@cbd85da38a58 /]# exit
exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
一般需要容器在后台运行,-d选项就很有用了。

docker run -d -it --name centos.7.2  centos:7
1
[root@izoq008ryseuupz ~]# docker run -d -it --name centos.7.2  centos:7
a258385ce59290615b250405beaf403aecc9ff75679641a0d8091795b7dc44f7
[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                                        NAMES
a258385ce592        centos:7            "/bin/bash"              6 seconds ago       Up 6 seconds                                                               centos.7.2
1
2
3
4
5
其他选项可以自己试一试。

Exit
exit是在容器内部执行的命令,执行后会退出当前容器,并且将当前容器停止(容器状态会是exited),在上面也使用过。

[root@izoq008ryseuupz ~]# docker start -i centos.7.2
[root@a258385ce592 /]# ll
total 56
-rw-r--r--  1 root root 12114 Nov 13 01:55 anaconda-post.log
lrwxrwxrwx  1 root root     7 Nov 13 01:53 bin -> usr/bin
drwxr-xr-x  5 root root   360 Nov 23 11:00 dev
drwxr-xr-x  1 root root  4096 Nov 23 05:13 etc
drwxr-xr-x  2 root root  4096 Apr 11  2018 home
lrwxrwxrwx  1 root root     7 Nov 13 01:53 lib -> usr/lib
lrwxrwxrwx  1 root root     9 Nov 13 01:53 lib64 -> usr/lib64
drwxr-xr-x  2 root root  4096 Apr 11  2018 media
drwxr-xr-x  2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x  2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x 95 root root     0 Nov 23 11:00 proc
dr-xr-x---  1 root root  4096 Nov 23 10:54 root
drwxr-xr-x 11 root root  4096 Nov 13 01:55 run
lrwxrwxrwx  1 root root     8 Nov 13 01:53 sbin -> usr/sbin
drwxr-xr-x  2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x 13 root root     0 Apr 11  2018 sys
drwxrwxrwt  7 root root  4096 Nov 13 01:55 tmp
drwxr-xr-x 13 root root  4096 Nov 13 01:53 usr
drwxr-xr-x 18 root root  4096 Nov 13 01:54 var
[root@a258385ce592 /]# exit
exit
[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                                        NAMES
a258385ce592        centos:7            "/bin/bash"              6 hours ago         Exited (0) 10 seconds ago                                                centos.7.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Docker Restart
重启一个或多个容器。

docker restart --help
1
[root@izoq008ryseuupz ~]# docker restart --help

Usage:    docker restart [OPTIONS] CONTAINER [CONTAINER...]

Restart one or more containers

Options:
  -t, --time int   Seconds to wait for stop before killing the container (default 10)
1
2
3
4
5
6
7
8
重启容器centos.7.2(不需要容器是运行状态)。

docker restart centos.7.2
1
[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                                        NAMES
a258385ce592        centos:7            "/bin/bash"              6 hours ago         Exited (0) 7 minutes ago                                                centos.7.2
[root@izoq008ryseuupz ~]# docker restart centos.7.2
centos.7.2
[root@izoq008ryseuupz ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a258385ce592        centos:7            "/bin/bash"              6 hours ago         Up 39 seconds                           centos.7.2
[root@izoq008ryseuupz ~]# docker restart centos.7.2
centos.7.2
[root@izoq008ryseuupz ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a258385ce592        centos:7            "/bin/bash"              6 hours ago         Up 3 seconds                            centos.7.2
1
2
3
4
5
6
7
8
9
10
11
12
13
-t选项是在重启容器前等待数秒(默认值10)。

docker restart -t 100 centos.7.2
1
[root@izoq008ryseuupz ~]# docker restart -t 100 centos.7.2
centos.7.2
[root@izoq008ryseuupz ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a258385ce592        centos:7            "/bin/bash"              6 hours ago         Up 14 seconds                           centos.7.2
1
2
3
4
5
Docker RM
移除一个或多个容器。

docker rm --help
1
[root@izoq008ryseuupz ~]# docker rm --help

Usage:    docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)
  -l, --link      Remove the specified link
  -v, --volumes   Remove anonymous volumes associated with the container
1
2
3
4
5
6
7
8
9
10
移除容器centos.7.1和centos.7。

docker rm centos.7.1 centos.7
1
[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                                        NAMES
a258385ce592        centos:7            "/bin/bash"              6 hours ago         Up 4 minutes                                                            centos.7.2
cbd85da38a58        centos:7            "/bin/bash"              6 hours ago         Exited (0) 6 hours ago                                                  centos.7.1
74d0a8d032ff        centos:7            "/bin/bash"              7 hours ago         Exited (137) 6 hours ago                                                centos.7
[root@izoq008ryseuupz ~]# docker rm centos.7.1 centos.7
centos.7.1
centos.7
[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS                                        NAMES
a258385ce592        centos:7            "/bin/bash"              6 hours ago         Up 5 minutes                                                           centos.7.2
1
2
3
4
5
6
7
8
9
10
11
这种方法不能移除正在运行的容器。

docker rm centos.7.2
1
[root@izoq008ryseuupz ~]# docker rm centos.7.2
Error response from daemon: You cannot remove a running container a258385ce59290615b250405beaf403aecc9ff75679641a0d8091795b7dc44f7. Stop the container before attempting removal or force remove
1
2
可以使用-f选项进行强制移除。

docker rm -f centos.7.2
1
[root@izoq008ryseuupz ~]# docker rm -f centos.7.2
centos.7.2
[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS                                        NAMES
1
2
3
4
移除所有容器;组合选项-aq是选项-a和-q的简写,即代表所有容器ID。

docker rm -f $(docker ps -aq)
1
[root@izoq008ryseuupz ~]# docker rm -f $(docker ps -aq)
220188bb364f
fd00901dfbbb
fab31562aeac
fecbf8c1b956
a4cfeb0679fd
ae23a046566b
aa6a63e58682
5e73372c49d3
1655972bf5cd
ed1eb87a4d97
da76541620b1
1539964c89a1
c1b657c7608c
64105665545b
bc3a0ef096ed
[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
其他选项可以自己试一试。

Docker Attach
进入容器内部。

docker attach --help
1
[root@izoq008ryseuupz ~]# docker attach --help

Usage:    docker attach [OPTIONS] CONTAINER

Attach local standard input, output, and error streams to a running container

Options:
      --detach-keys string   Override the key sequence for detaching a container
      --no-stdin             Do not attach STDIN
      --sig-proxy            Proxy all received signals to the process (default true)
1
2
3
4
5
6
7
8
9
10
进入容器centos.7.2内部。

docker attach centos.7.2
1
[root@izoq008ryseuupz ~]# docker attach centos.7.2
[root@dfe33f1c3ab1 /]# ll
total 56
-rw-r--r--  1 root root 12114 Nov 13 01:55 anaconda-post.log
lrwxrwxrwx  1 root root     7 Nov 13 01:53 bin -> usr/bin
drwxr-xr-x  5 root root   360 Nov 23 11:35 dev
drwxr-xr-x  1 root root  4096 Nov 23 11:34 etc
drwxr-xr-x  2 root root  4096 Apr 11  2018 home
lrwxrwxrwx  1 root root     7 Nov 13 01:53 lib -> usr/lib
lrwxrwxrwx  1 root root     9 Nov 13 01:53 lib64 -> usr/lib64
drwxr-xr-x  2 root root  4096 Apr 11  2018 media
drwxr-xr-x  2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x  2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x 89 root root     0 Nov 23 11:35 proc
dr-xr-x---  1 root root  4096 Nov 23 11:34 root
drwxr-xr-x 11 root root  4096 Nov 13 01:55 run
lrwxrwxrwx  1 root root     8 Nov 13 01:53 sbin -> usr/sbin
drwxr-xr-x  2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x 13 root root     0 Apr 11  2018 sys
drwxrwxrwt  7 root root  4096 Nov 13 01:55 tmp
drwxr-xr-x 13 root root  4096 Nov 13 01:53 usr
drwxr-xr-x 18 root root  4096 Nov 13 01:54 var
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
其他选项可以自己试一试。

Docker Logs
获取容器的日志。

docker logs --help
1
[root@izoq008ryseuupz ~]# docker logs --help

Usage:    docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
1
2
3
4
5
6
7
8
9
10
11
12
13
获取容器centos.7.2的日志。

docker logs centos.7.2
1
[root@izoq008ryseuupz ~]# docker logs centos.7.2
[root@dfe33f1c3ab1 /]# docker ps -a
bash: docker: command not found
[root@dfe33f1c3ab1 /]# exit
exit
[root@dfe33f1c3ab1 /]# ll
total 56
-rw-r--r--  1 root root 12114 Nov 13 01:55 anaconda-post.log
lrwxrwxrwx  1 root root     7 Nov 13 01:53 bin -> usr/bin
drwxr-xr-x  5 root root   360 Nov 23 11:35 dev
drwxr-xr-x  1 root root  4096 Nov 23 11:34 etc
drwxr-xr-x  2 root root  4096 Apr 11  2018 home
lrwxrwxrwx  1 root root     7 Nov 13 01:53 lib -> usr/lib
lrwxrwxrwx  1 root root     9 Nov 13 01:53 lib64 -> usr/lib64
drwxr-xr-x  2 root root  4096 Apr 11  2018 media
drwxr-xr-x  2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x  2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x 89 root root     0 Nov 23 11:35 proc
dr-xr-x---  1 root root  4096 Nov 23 11:34 root
drwxr-xr-x 11 root root  4096 Nov 13 01:55 run
lrwxrwxrwx  1 root root     8 Nov 13 01:53 sbin -> usr/sbin
drwxr-xr-x  2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x 13 root root     0 Apr 11  2018 sys
drwxrwxrwt  7 root root  4096 Nov 13 01:55 tmp
drwxr-xr-x 13 root root  4096 Nov 13 01:53 usr
drwxr-xr-x 18 root root  4096 Nov 13 01:54 var
[root@dfe33f1c3ab1 /]# ^C
[root@dfe33f1c3ab1 /]# exit
exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
使用-t选项可以得到每条日志的时间戳。

docker logs -t centos.7.2
1
[root@izoq008ryseuupz ~]# docker logs -t centos.7.2
[root@dfe33f1c3ab1 /]# docker ps -a
2020-11-23T11:34:43.566995980Z bash: docker: command not found
2020-11-23T11:34:49.893516887Z [root@dfe33f1c3ab1 /]# exit
2020-11-23T11:34:49.893542753Z exit
[root@dfe33f1c3ab1 /]# ll
2020-11-23T11:37:19.154564430Z total 56
2020-11-23T11:37:19.154568643Z -rw-r--r--  1 root root 12114 Nov 13 01:55 anaconda-post.log
2020-11-23T11:37:19.154571936Z lrwxrwxrwx  1 root root     7 Nov 13 01:53 bin -> usr/bin
2020-11-23T11:37:19.154575318Z drwxr-xr-x  5 root root   360 Nov 23 11:35 dev
2020-11-23T11:37:19.154577984Z drwxr-xr-x  1 root root  4096 Nov 23 11:34 etc
2020-11-23T11:37:19.154580430Z drwxr-xr-x  2 root root  4096 Apr 11  2018 home
2020-11-23T11:37:19.154583081Z lrwxrwxrwx  1 root root     7 Nov 13 01:53 lib -> usr/lib
2020-11-23T11:37:19.154585744Z lrwxrwxrwx  1 root root     9 Nov 13 01:53 lib64 -> usr/lib64
2020-11-23T11:37:19.154588450Z drwxr-xr-x  2 root root  4096 Apr 11  2018 media
2020-11-23T11:37:19.154590994Z drwxr-xr-x  2 root root  4096 Apr 11  2018 mnt
2020-11-23T11:37:19.154593486Z drwxr-xr-x  2 root root  4096 Apr 11  2018 opt
2020-11-23T11:37:19.154595932Z dr-xr-xr-x 89 root root     0 Nov 23 11:35 proc
2020-11-23T11:37:19.154598427Z dr-xr-x---  1 root root  4096 Nov 23 11:34 root
2020-11-23T11:37:19.154600899Z drwxr-xr-x 11 root root  4096 Nov 13 01:55 run
2020-11-23T11:37:19.154603363Z lrwxrwxrwx  1 root root     8 Nov 13 01:53 sbin -> usr/sbin
2020-11-23T11:37:19.154606005Z drwxr-xr-x  2 root root  4096 Apr 11  2018 srv
2020-11-23T11:37:19.154608471Z dr-xr-xr-x 13 root root     0 Apr 11  2018 sys
2020-11-23T11:37:19.154610953Z drwxrwxrwt  7 root root  4096 Nov 13 01:55 tmp
2020-11-23T11:37:19.154613413Z drwxr-xr-x 13 root root  4096 Nov 13 01:53 usr
2020-11-23T11:37:19.154615882Z drwxr-xr-x 18 root root  4096 Nov 13 01:54 var
2020-11-23T11:37:23.647675495Z [root@dfe33f1c3ab1 /]# ^C
2020-11-23T11:39:24.491498588Z [root@dfe33f1c3ab1 /]# exit
2020-11-23T11:39:24.491522428Z exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
获取最后几条日志。

docker logs --tail 5 centos.7.2
1
[root@izoq008ryseuupz ~]# docker logs --tail 5 centos.7.2
drwxr-xr-x 13 root root  4096 Nov 13 01:53 usr
drwxr-xr-x 18 root root  4096 Nov 13 01:54 var
[root@dfe33f1c3ab1 /]# ^C
[root@dfe33f1c3ab1 /]# exit
exit
1
2
3
4
5
6
其他选项可以自己试一试。

Docker Top
展示容器内部运行的进程。

docker top --help
1
[root@izoq008ryseuupz ~]# docker top --help

Usage:    docker top CONTAINER [ps OPTIONS]

Display the running processes of a container
1
2
3
4
5
查看容器centos.7.2中运行的进程。

docker top centos.7.2
1
[root@izoq008ryseuupz ~]# docker top centos.7.2
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                1009                993                 0                   19:58               pts/0               00:00:00            /bin/bash
1
2
3
Docker Inspect
返回Docker对象的低级信息,所以它可以对容器使用,也可以对镜像使用,只要是Docker objects即可。

docker inspect --help
1
[root@izoq008ryseuupz ~]# docker inspect --help

Usage:    docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type
1
2
3
4
5
6
7
8
9
10
镜像centos:7的低级信息。

docker inspect centos:7
1
[root@izoq008ryseuupz ~]# docker inspect centos:7
[
    {
        "Id": "sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf",
        "RepoTags": [
            "centos:7"
        ],
        "RepoDigests": [
            "centos@sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-11-14T00:20:04.644613188Z",
        "Container": "b454ba2d27117a169c53eb4c14faf363cb6aaca84df43408581fb748cc2bf796",
...
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
容器centos.7.2的低级信息。

docker inspect centos.7.2
1
[root@izoq008ryseuupz ~]# docker inspect centos.7.2
[
    {
        "Id": "dfe33f1c3ab1db72c06965fcf54da895d6466cf8b990f1147916eb3c9d6009ca",
        "Created": "2020-11-23T11:34:37.28727717Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1009,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-11-23T11:58:44.691540365Z",
            "FinishedAt": "2020-11-23T11:39:24.489513335Z"
        },
        "Image": "sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf",
...
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
其他选项可以自己试一试。

Docker CP
将容器中的文件copy到本地文件系统中。

docker cp --help
1
[root@izoq008ryseuupz ~]# docker cp --help

Usage:    docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
    docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.

Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
进入容器centos.7.2中创建kaven文件。

[root@dfe33f1c3ab1 /]# echo "this is kaven" > kaven
1

将容器centos.7.2中的kaven文件copy到本地。

[root@izoq008ryseuupz ~]# docker cp centos.7.2:/kaven kaven
1

可以看到copy的文件是正确的。

[root@izoq008ryseuupz ~]# vim kaven
1

其他选项可以自己试一试。

Docker Commit
根据容器的更改创建新的镜像。

docker commit --help
1
[root@izoq008ryseuupz ~]# docker commit --help

Usage:    docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)
1
2
3
4
5
6
7
8
9
10
11
根据当前的容器centos.7.2,创建一个新的镜像kaven/centos:7.2。

docker commit -a "kaven" -m "this is image" centos.7.2 kaven/centos:7.2
1
[root@izoq008ryseuupz ~]# docker commit -a "kaven" -m "this is image" centos.7.2 kaven/centos:7.2
sha256:6166378a65c385ec1622d3c5c63f4d1e88ac53cea80cf3703234c9cd48969620
[root@izoq008ryseuupz ~]# docker images
REPOSITORY                                                     TAG                 IMAGE ID            CREATED             SIZE
kaven/centos                                                   7.2                 6166378a65c3        9 seconds ago       204MB
1
2
3
4
5
根据新的镜像kaven/centos:7.2,再去创建并运行新的容器centos.7.3。

docker run -it --name centos.7.3 kaven/centos:7.2
1
可见新的容器centos.7.3中也有kaven文件。

[root@izoq008ryseuupz ~]# docker run -it --name centos.7.3 kaven/centos:7.2
[root@f6356144219e /]# ll
total 60
-rw-r--r--  1 root root 12114 Nov 13 01:55 anaconda-post.log
lrwxrwxrwx  1 root root     7 Nov 13 01:53 bin -> usr/bin
drwxr-xr-x  5 root root   360 Nov 23 12:32 dev
drwxr-xr-x  1 root root  4096 Nov 23 12:32 etc
drwxr-xr-x  2 root root  4096 Apr 11  2018 home
-rw-r--r--  1 root root    14 Nov 23 12:14 kaven
lrwxrwxrwx  1 root root     7 Nov 13 01:53 lib -> usr/lib
lrwxrwxrwx  1 root root     9 Nov 13 01:53 lib64 -> usr/lib64
drwxr-xr-x  2 root root  4096 Apr 11  2018 media
drwxr-xr-x  2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x  2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x 94 root root     0 Nov 23 12:32 proc
dr-xr-x---  1 root root  4096 Nov 23 11:34 root
drwxr-xr-x 11 root root  4096 Nov 13 01:55 run
lrwxrwxrwx  1 root root     8 Nov 13 01:53 sbin -> usr/sbin
drwxr-xr-x  2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x 13 root root     0 Apr 11  2018 sys
drwxrwxrwt  7 root root  4096 Nov 13 01:55 tmp
drwxr-xr-x 13 root root  4096 Nov 13 01:53 usr
drwxr-xr-x 18 root root  4096 Nov 13 01:54 var
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
其他选项可以自己试一试。

Docker Container及Container命令详解就介绍到这里。
————————————————
版权声明:本文为CSDN博主「ITKaven」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_37960603/article/details/109989376

 类似资料: