0%

Docker 容器的使用

Docker 命令集合

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
30
31
32
33
[root@VM-0-13-centos ~]# docker container --help
Usage: docker container COMMAND
Manage containers
Options:
--help Print usage
Commands:
attach Attach to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.

使用场景

容器启动

  • 新建容器并启动

    • 涉及命令: docker run

    • 示例:

      • 启动并操作后则终止容器

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        [root@VM-0-13-centos ~]# docker image ls
        REPOSITORY TAG IMAGE ID CREATED SIZE
        nginx v2 33a05755d7f3 3 weeks ago 142 MB
        docker.io/nginx latest 51086ed63d8c 3 weeks ago 142 MB
        docker.io/ubuntu 18.04 71cb16d32be4 3 weeks ago 63.1 MB
        <none> <none> 618d482e06b4 3 months ago 52.6 MB
        <none> <none> 4c0257ee9d10 3 months ago 5.53 MB
        docker.io/ubuntu latest 27941809078c 4 months ago 77.8 MB
        [root@VM-0-13-centos ~]# docker run docker.io/ubuntu:18.04 /bin/echo 'Hello, World!'
        Hello, World!
      • 启动bash终端,并进行交互

        1
        2
        3
        4
        5
        6
        [root@VM-0-13-centos ~]# docker run -it docker.io/ubuntu:18.04 /bin/bash
        root@8c2f4c3d6f16:/# pwd
        /
        root@8c2f4c3d6f16:/# exit
        exit
        [root@VM-0-13-centos ~]#
  • 启动已中止容器

    • 涉及命令: docker container start

后台运行

很多场景下, 需要Docker后台运行而不是讲命令结果输出在宿主机上, 可通过 -d 参数实现;

  • 涉及命令: docker run -d

  • 示例:

    • 不用 -d:

      1
      2
      3
      4
      5
      6
      7
      [root@VM-0-13-centos ~]# docker run docker.io/ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
      hello world
      hello world
      hello world
      hello world
      hello world
      hello world

      可以看到容器的输出结果STDOUT , 打印在了宿主机上;

    • -d:

      1
      2
      [root@VM-0-13-centos ~]# docker run -d docker.io/ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
      86ef2217c3d59179524323c4f957bb74cb7f47d8e173eb7f0f2c66d9a7eb9128

      此时容器选择后台运行;

容器终止

容器进入终止状态的方式是很多的, 例如:

  • Docker 容器指定的应用终结时, 自动终止容器;

  • Docker 容器内通过, exit, Ctrl+d 退出终端后, 容器终止;

  • Docker container stop 命令, 可以终止一个处于运行状态的容器;

    • 示例: 将上面启动的后台运行的Docker容器终止掉:

      1
      2
      3
      4
      5
      6
      7
      [root@VM-0-13-centos ~]# docker container ls
      CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
      86ef2217c3d5 docker.io/ubuntu:18.04 "/bin/sh -c 'while..." 9 minutes ago Up 9 minutes practical_montalcini
      [root@VM-0-13-centos ~]# docker container stop 86ef2217c3d5
      86ef2217c3d5
      [root@VM-0-13-centos ~]# docker container ls
      CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

      进入容器

在某些场景内, 需要进入容器,进行操作, 可以使用 docker attachdocker exec -it 命令, 推荐使用 docker exec -it;

  • 使用 docker attach

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@VM-0-13-centos ~]# docker run -dit docker.io/ubuntu:18.04
    5d5cbafd10fdb777c7063444db737130768dae3c4c36a048bf0c1f66cba5656d
    [root@VM-0-13-centos ~]# docker container ls
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    5d5cbafd10fd docker.io/ubuntu:18.04 "bash" 12 seconds ago Up 11 seconds brave_bohr
    [root@VM-0-13-centos ~]# docker attach 5d5cbafd10fd
    root@5d5cbafd10fd:/# exit
    exit
    [root@VM-0-13-centos ~]# docker container ls
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

    可以看到 使用exit 命令推出后, 容器变为终止状态;

  • 使用 docker exec

    1
    2
    3
    4
    5
    6
    7
    8
    [root@VM-0-13-centos ~]# docker run -dit docker.io/ubuntu:18.04
    f170bccfbdcff331f02bbdbc5dfd163fa929caf87ebc4555dd768236cd696d19
    [root@VM-0-13-centos ~]# docker exec -it f170 bash
    root@f170bccfbdcf:/# exit
    exit
    [root@VM-0-13-centos ~]# docker container ls
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    f170bccfbdcf docker.io/ubuntu:18.04 "bash" 37 seconds ago Up 36 seconds mystifying_brattain

    可以看到即使使用了exit 命令, 也没有导致容器进入终止状态;

删除容器

  • 删除一个终止状态的容器, docker container rm

  • 删除所有终止状态的容器, docker container prune

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    [root@VM-0-13-centos ~]# docker container prune
    WARNING! This will remove all stopped containers.
    Are you sure you want to continue? [y/N] y
    Deleted Containers:
    5d5cbafd10fdb777c7063444db737130768dae3c4c36a048bf0c1f66cba5656d
    86ef2217c3d59179524323c4f957bb74cb7f47d8e173eb7f0f2c66d9a7eb9128
    a3b8f09e5fe30f219e15a026ef52d070b204dbb12bad945fff16e4297c8c8d6e
    8c2f4c3d6f164d3e32dff74eeab1562828b3bda4fdf77b3e079d41a839e64a4b
    54a804e8935f520fa9915907bab8c21c5d09ea7fa064c46d01a0ed14f9f6cc6a
    857e62419b36ea769c2655d1eb13fe268a3a516a731e2c5cf7848582aa7d2233
    72b48ef7e44ba46b7746387c7041280bd5c065e5aa3e2672996d2affe675a005
    8194230a842c3469242b4c9dbc81cb4a83cf21e23a718b6cd0e0d456215c8c2c
    6be1289236e9321d86de1ca7aaded317b136b78c6174ed4192ead4a03cf44044
    0dc2228117db93521c7048248eb56c2be289401d0630965010eb93e903a6aa7b
    e0d023f15495e37eb3b78355cfda033eca7a1672f48c1574333157d09bab7976
    a93b2d91a8c3dd59713017e423e7ad34108b87904bbbcd4b9143f9854667877e
    7b44171dcfe7c63bb2aeab7df8a65e2a64afd1fe5182d07ef108b845143deb0b

    Total reclaimed space: 9.85 MB
  • 删除一个运行状态的容器, docker container rm -f

    1
    2
    3
    4
    5
    6
    7
    8
    [root@VM-0-13-centos ~]# docker container ls
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    f170bccfbdcf docker.io/ubuntu:18.04 "bash" 5 minutes ago Up 5 minutes mystifying_brattain
    3fa97808f299 docker.io/ubuntu:18.04 "/bin/bash" 45 minutes ago Up 45 minutes upbeat_swanson
    8bcaacf3fee6 nginx:v2 "/docker-entrypoin..." 3 weeks ago Up 3 weeks 0.0.0.0:81->80/tcp web2
    c795eabb7c7d nginx "/docker-entrypoin..." 3 weeks ago Up 3 weeks 0.0.0.0:80->80/tcp webserver
    [root@VM-0-13-centos ~]# docker rm -f upbeat_swanson
    upbeat_swanson