Docker 简明教程

Docker - Setting Node.js

Node.js 是一个 JavaScript 框架,用于开发服务端应用程序。它是一个开源框架,旨在运行在各种操作系统上。由于 Node.js 是开发人员经常使用的框架,因此 Docker 也确保了它支持 Node.js 应用程序。

Node.js is a JavaScript framework that is used for developing server-side applications. It is an open source framework that is developed to run on a variety of operating systems. Since Node.js is a popular framework for development, Docker has also ensured it has support for Node.js applications.

我们现在将看到使 Node.js 的 Docker 容器启动并运行的各个步骤。

We will now see the various steps for getting the Docker container for Node.js up and running.

Step 1 - 第一步是从 Docker Hub 拉取镜像。当你登录到 Docker Hub 时,你将能够搜索并查看 Node.js 的镜像,如下所示。只需在搜索框中键入 Node,然后单击搜索结果中出现的 node (official) 链接。

Step 1 − The first step is to pull the image from Docker Hub. When you log into Docker Hub, you will be able to search and see the image for Node.js as shown below. Just type in Node in the search box and click on the node (official) link which comes up in the search results.

pull image from docker hub

Step 2 - 你将看到 Docker pull 在 Docker Hub 中存储库的详细信息中为 node 设置了命令。

Step 2 − You will see that the Docker pull command for node in the details of the repository in Docker Hub.

documentation

Step 3 - 在 Docker 主机上,使用如上所示的 Docker pull 命令从 Docker Hub 下载最新的 node 镜像。

Step 3 − On the Docker Host, use the Docker pull command as shown above to download the latest node image from Docker Hub.

latest node image

pull 完成后,我们就可以继续下一步。

Once the pull is complete, we can then proceed with the next step.

pull complete

Step 4 - 在 Docker 主机上,让我们使用 vim 编辑器并创建一个 Node.js 示例文件。在此文件中,我们将添加一个简单的命令以将“HelloWorld”显示到命令提示符。

Step 4 − On the Docker Host, let’s use the vim editor and create one Node.js example file. In this file, we will add a simple command to display “HelloWorld” to the command prompt.

vim editor

在 Node.js 文件中,我们添加以下语句:

In the Node.js file, let’s add the following statement −

Console.log(‘Hello World’);

这将在我们通过 Node.js 运行它时输出“Hello World”短语。

This will output the “Hello World” phrase when we run it through Node.js.

hello world phrase

确保保存文件,然后继续下一步。

Ensure that you save the file and then proceed to the next step.

Step 5 − 要使用 Node Docker 容器运行我们的 Node.js 脚本,我们需要执行以下语句 -

Step 5 − To run our Node.js script using the Node Docker container, we need to execute the following statement −

sudo docker run –it –rm –name = HelloWorld –v “$PWD”:/usr/src/app
   –w /usr/src/app node node HelloWorld.js

需要对上述命令注意以下几点 −

The following points need to be noted about the above command −

  1. The –rm option is used to remove the container after it is run.

  2. We are giving a name to the container called “HelloWorld”.

  3. We are mentioning to map the volume in the container which is /usr/src/app to our current present working directory. This is done so that the node container will pick up our HelloWorld.js script which is present in our working directory on the Docker Host.

  4. The –w option is used to specify the working directory used by Node.js.

  5. The first node option is used to specify to run the node image.

  6. The second node option is used to mention to run the node command in the node container.

  7. And finally we mention the name of our script.

然后,我们将获得以下输出。从输出中,我们可以清楚地看到节点容器作为容器运行并执行 HelloWorld.js 脚本。

We will then get the following output. And from the output, we can clearly see that the Node container ran as a container and executed the HelloWorld.js script.

helloworld js script