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.
Options
-
command − This is the command to run when the container is launched.
-
param1 − This is the parameter entered to the command.
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.
Step 2 − 使用 Docker build 命令构建镜像。
Step 2 − Build the image using the Docker build command.
Step 3 − 从镜像运行一个容器。
Step 3 − Run a container from the image.
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.
Options
-
command − This is the command to run when the container is launched.
-
param1 − This is the parameter entered into the command.
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”]
Step 2 − 使用 Docker build 命令构建镜像。
Step 2 − Build the image using the Docker build command.
Step 3 − 从镜像运行一个容器。
Step 3 − Run a container from the image.
ENV
此命令用于在容器中设置环境变量。
This command is used to set environment variables in the container.
Options
-
Key − This is the key for the environment variable.
-
value − This is the value for the environment variable.
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
Step 2 − 使用 Docker build 命令构建镜像。
Step 2 − Build the image using the Docker build command.
Step 3 − 从镜像运行一个容器。
Step 3 − Run a container from the image.
Step 4 − 最后,执行 env 命令以查看环境变量。
Step 4 − Finally, execute the env command to see the environment variables.
WORKDIR
此命令用于设置容器的工作目录。
This command is used to set the working directory of the container.
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
Step 2 − 使用 Docker build 命令构建镜像。
Step 2 − Build the image using the Docker build command.
Step 3 − 从镜像运行一个容器。
Step 3 − Run a container from the image.