Docker 简明教程

Docker - Instruction Commands

Docker 拥有一系列指令命令。这些命令放入 Dockerfile 中。我们来看看可用的命令。

Docker has a host of instruction commands. These are commands that are put in the Docker File. Let’s look at the ones which are available.

CMD Instruction

这个命令用于在运行时执行命令,即在执行容器时。

This command is used to execute a command at runtime when the container is executed.

Syntax

CMD command param1

Options

  1. command − This is the command to run when the container is launched.

  2. param1 − This is the parameter entered to the command.

Return Value

命令会相应地执行。

The command will execute accordingly.

Example

在我们的示例中,我们在 Dockerfile 中输入一个简单的 Hello World echo 命令并基于该文件创建一个镜像,然后从中启动一个容器。

In our example, we will enter a simple Hello World echo in our Docker File and create an image and launch a container from it.

Step 1 − 使用以下命令构建 Dockerfile −

Step 1 − Build the Docker File with the following commands −

FROM ubuntu
MAINTAINER demousr@gmail.com
CMD [“echo” , “hello world”]

这里,CMD 仅用于打印 hello world

Here, the CMD is just used to print hello world.

cmd

Step 2 − 使用 Docker build 命令构建镜像。

Step 2 − Build the image using the Docker build command.

build command

Step 3 − 从镜像运行一个容器。

Step 3 − Run a container from the image.

run a container

ENTRYPOINT

这个命令也可用于在运行时为容器执行命令。但是,我们使用 ENTRYPOINT 命令可以更灵活。

This command can also be used to execute commands at runtime for the container. But we can be more flexible with the ENTRYPOINT command.

Syntax

ENTRYPOINT command param1

Options

  1. command − This is the command to run when the container is launched.

  2. param1 − This is the parameter entered into the command.

Return Value

命令会相应地执行。

The command will execute accordingly.

Example

让我们看一个有关 ENTRYPOINT 的示例,以便进一步了解。在我们的示例中,我们在 Dockerfile 中输入一个简单的 echo 命令并基于该文件创建一个镜像,然后从中启动一个容器。

Let’s take a look at an example to understand more about ENTRYPOINT. In our example, we will enter a simple echo command in our Docker File and create an image and launch a container from it.

Step 1 − 使用以下命令构建 Dockerfile −

Step 1 − Build the Docker File with the following commands −

FROM ubuntu
MAINTAINER demousr@gmail.com
ENTRYPOINT [“echo”]
entry point

Step 2 − 使用 Docker build 命令构建镜像。

Step 2 − Build the image using the Docker build command.

docker build command

Step 3 − 从镜像运行一个容器。

Step 3 − Run a container from the image.

container from image

ENV

此命令用于在容器中设置环境变量。

This command is used to set environment variables in the container.

Syntax

ENV key value

Options

  1. Key − This is the key for the environment variable.

  2. value − This is the value for the environment variable.

Return Value

命令会相应地执行。

The command will execute accordingly.

Example

在我们的示例中,将在 Docker File 中输入一个简单的 echo 命令并创建一个镜像,然后从中启动一个容器。

In our example, we will enter a simple echo command in our Docker File and create an image and launch a container from it.

Step 1 − 使用以下命令构建 Dockerfile −

Step 1 − Build the Docker File with the following commands −

FROM ubuntu
MAINTAINER demousr@gmail.com
ENV var1=Tutorial var2=point
env

Step 2 − 使用 Docker build 命令构建镜像。

Step 2 − Build the image using the Docker build command.

env docker build command

Step 3 − 从镜像运行一个容器。

Step 3 − Run a container from the image.

env run a container

Step 4 − 最后,执行 env 命令以查看环境变量。

Step 4 − Finally, execute the env command to see the environment variables.

env command

WORKDIR

此命令用于设置容器的工作目录。

This command is used to set the working directory of the container.

Syntax

WORKDIR dirname

Options

  1. dirname − The new working directory. If the directory does not exist, it will be added.

Return Value

命令会相应地执行。

The command will execute accordingly.

Example

在我们的示例中,将在 Docker File 中输入一个简单的 echo 命令并创建一个镜像,然后从中启动一个容器。

In our example, we will enter a simple echo command in our Docker File and create an image and launch a container from it.

Step 1 − 使用以下命令构建 Dockerfile −

Step 1 − Build the Docker File with the following commands −

FROM ubuntu
MAINTAINER demousr@gmail.com
WORKDIR /newtemp
CMD pwd
workdir

Step 2 − 使用 Docker build 命令构建镜像。

Step 2 − Build the image using the Docker build command.

workdir build command

Step 3 − 从镜像运行一个容器。

Step 3 − Run a container from the image.

workdir run command