【Docker】Container 常用命令 - run

Posted by 西维蜀黍 on 2021-08-12, Last Modified on 2022-02-12

新建、启动并停止容器 - docker run

See https://docs.docker.com/engine/reference/commandline/run/ .

$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

docker run命令是新建并启动容器,每运行一次,就会新建一个容器。同样的命令运行两次,就会生成两个一模一样的容器文件(当前也会创建两个容器)。

在新建完成之后,容器会被终止。

比如:

# 查看当前正在运行的容器
$ docker ps
CONTAINER ID   IMAGE                                 COMMAND   CREATED          STATUS          PORTS     NAMES

# 创建一个容器,在创建完成后,会被自动终止
$ docker run ubuntu bash

# 查看当前正在运行的容器
$ docker ps
CONTAINER ID   IMAGE                                 COMMAND   CREATED          STATUS          PORTS     NAMES

# 查看所有的容器(包括不在运行的)
$ docker ps -a
CONTAINER ID   IMAGE                                 COMMAND    CREATED          STATUS                     PORTS     NAMES
8b9f9c13e0f7   ubuntu                                "bash"     3 minutes ago    Exited (0) 3 minutes ago             nervous_bose

# 再创建一个容器,在创建完成后,会被自动终止
$ docker run ubuntu bash

$ docker ps -a
CONTAINER ID   IMAGE                                 COMMAND    CREATED          STATUS                     PORTS     NAMES
a3711fe20b61   ubuntu                                "bash"     11 seconds ago   Exited (0) 9 seconds ago             heuristic_mendel
8b9f9c13e0f7   ubuntu                                "bash"     7 minutes ago    Exited (0) 7 minutes ago             nervous_bose

可以看到,每次执行 docker run ubuntu bash,都会创建一个容器,且在创建完成后,容器就会被立刻终止。

docker run命令具有自动抓取 image 文件的功能。如果发现本地没有指定的 image 文件,就会从仓库自动抓取。但是,docker image pull命令并不是 docker run 的必须步骤(因为显然,如果本地已经存在当前image,就不会再pull)。

Demo

# 下面命令,生成并启动了Ubuntu 的 image(的实例),并在控制台输出“Hello”,最后终止这个Ubuntu容器实例
$ docker container run ubuntu /bin/echo "Hello"

Parameters

-it - 交互模式

如果我们希望新建并启动一个Ubuntu容器实例,并将当前控制台作为这个实例的控制台。

$ docker run -it ubuntu /bin/bash

# 创建并启动一个容器,名为 test ,基于镜像 ubuntu
$ docker run --name test ubuntu

-d - Run in background

# 使用镜像nginx:latest,并以后台模式启动并运行一个容器
$ docker run -d nginx:latest

# 后台启动并运行一个ubuntu容器(如果docker run -d ubuntu 也不会在后台一直运行)
$ docker run -d -it ubuntu /bin/bash

--name [new_name] - 重命名

$ docker run -d --name test2 -it ubuntu
22c9ddb57e37b8ccb8e2b60993ab9b00ec84ee68f8155e59fff39128ae43f9c6

$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                       NAMES
22c9ddb57e37   ubuntu            "bash"                   3 seconds ago    Up 2 seconds                                                test2

# 进入 container
$ docker exec -it test2 bash
root@22c9ddb57e37:/#

-P - Publish all exposed ports to random ports

# 使用镜像nginx:latest,并以后台模式启动一个容器
$ docker run -P nginx:latest

-p list - 端口映射

# 使用镜像 nginx:latest 启动一个容器,将容器的 80 端口映射到主机的 80 端口
$ docker run -p 80:80 nginx:latest
$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash

This binds port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine. You can also specify udp and sctp po

-v list - Bind mount a volume

# 使用镜像 nginx:latest 启动一个容器,将主机的目录 /data 映射到容器的 /data
$ docker run -v /data:/data nginx:latest

-a - Attach to STDIN, STDOUT or STDERR

Refer to https://docs.docker.com/engine/reference/commandline/run/

--restart - Restart policies

Use Docker’s --restart to specify a container’s restart policy. A restart policy controls whether the Docker daemon restarts a container after exit. Docker supports the following restart policies:

Policy Result
no Do not automatically restart the container when it exits. This is the default.
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.
unless-stopped Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted.
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.
$ docker run --restart=always redis

This will run the redis container with a restart policy of always so that if the container exits, Docker will restart it.

More detailed information on restart policies can be found in the Restart Policies (–restart) section of the Docker run reference page.

Update

This command changes the restart policy for an already running container named redis.

$ docker update --restart unless-stopped redis

And this command will ensure all currently running containers will be restarted unless stopped.

$ docker update --restart unless-stopped $(docker ps -q)

Reference