node.js轻量级
While working on a project recently, I had to containerize NodeJs Windows application. To my surprise, there is not a single official image for NodeJs (Windows Container) on Docker hub. So, I decided to create one which I later uploaded to Docker Hub. This post is a step by step guide on how to build a lightweight NodeJs Windows container using Windows Nano Server. We chose Windows Nano Server due to its optimized small size.
最近在从事一个项目时,我不得不将NodeJs Windows应用程序容器化。 令我惊讶的是,Docker Hub上没有一个用于NodeJ(Windows容器)的正式映像。 因此,我决定创建一个,然后上传到Docker Hub。 这篇文章是有关如何使用Windows Nano Server构建轻量级NodeJs Windows容器的逐步指南。 我们选择Windows Nano Server是因为它具有优化的小尺寸。
Create a folder docker-nodejs on your hard disk.
在硬盘上创建一个文件夹docker-nodejs。
Download the NodeJs ZIP (x64) package from the list of available versions on the following web-page:
从以下网页上的可用版本列表中下载NodeJs ZIP(x64)软件包:
For this example, I have chosen version v14.9.0
在此示例中,我选择了v14.9.0版
Once the file has been downloaded, extract the content of the zip file into a folder. Rename the extracted folder to nodejs. Move the nodejs folder inside the folder docker-nodejs (created in the step above).
下载文件后,将zip文件的内容提取到一个文件夹中。 将提取的文件夹重命名为nodejs。 将nodejs文件夹移至docker-nodejs文件夹(在以上步骤中创建)内。
Inside docker-nodejs folder, Shift + Right Click. This will open the context menu, from the menu, select the option Open PowerShell window here. Type the following command in the PowerShell to open the notepad application and to create a file (with the name Dockerfile).
在docker-nodejs文件夹中, 按住Shift并单击右键 。 这将打开上下文菜单,从菜单中选择选项Open PowerShell window here 。 在PowerShell中键入以下命令以打开记事本应用程序并创建一个文件(名称为Dockerfile)。
notepad Dockerfile.In notepad, paste the following docker commands and save the file.
在记事本中,粘贴以下docker命令并保存文件。
FROM mcr.microsoft.com/windows/nanoserver:1803COPY nodejs /windows/system32CMD [ “node.exe” ]After saving the file, docker-nodejs folder will have a nodejs folder (folder contains extracted version of NodeJs) and a Dockerfile.
保存文件后,docker-nodejs文件夹将具有一个nodejs文件夹(文件夹包含NodeJs的提取版本)和一个Dockerfile。
Now in the PowerShell, we will run command to build an image from the Dockerfile (created in the step above). For this example, I am not specifying any Tag in the command because Docker will add the latest tag to the image by default. But you can add it by adding node:14.9.0 instead of node in the following command:
现在在PowerShell中,我们将运行命令以从Dockerfile构建映像(在上述步骤中创建)。 在此示例中,我未在命令中指定任何标签,因为Docker默认会将最新标签添加到映像中。 但是您可以通过添加 node:14.9.0 代替 以下命令中 的 node 来添加它 :
docker build -t node .Before running the image that is created in the step above, let’s list all the images with the following command:
在运行在以上步骤中创建的图像之前,让我们使用以下命令列出所有图像:
docker imagesThe output will look like the following screenshot. As we have used Windows Nano Server to build the image, the image size is quite small — 457MB only. Note — As we have not provided any Tag, docker has added the latest tag to the node image, as you can see in the screenshot. If you would have provided a tag in the step above, you would have seen that tag under the TAG column.
输出将类似于以下屏幕截图。 由于我们使用Windows Nano Server生成映像,因此映像大小非常小-仅457MB。 注意 —由于我们未提供任何标签,因此docker已将最新标签添加到节点图像,如您在屏幕快照中所见。 如果您在上面的步骤中提供了标签,则可以在TAG列下看到该标签。
Optional: If you want, now you can remove the Nano Server image, as it is not required anymore.
可选:如果需要,现在可以删除Nano Server映像,因为不再需要它。
// Command to remove an image docker rmi <Image ID>Run the image with the following command. Note: If you have provided a tag in the step above, you will need to change the following command node:<tag_provided_in_step_above> instead of node.
使用以下命令运行映像。 注意:如果在上述步骤中提供了标签,则需要更改以下命令node:<tag_provided_in_step_above>而不是node 。
docker run -t -d node cmdThis will start the container, you can see the running container with the following command
这将启动容器,您可以使用以下命令查看正在运行的容器
docker psNow, enter the following command to connect to the running container to verify the NodeJs version installed. Note: 614 are the first three letters of the container id, your container id will be different. So, update the following command accordingly:
现在,输入以下命令以连接到正在运行的容器,以验证已安装的NodeJs版本。 注意: 614是容器ID的前三个字母,您的容器ID将有所不同。 因此,相应地更新以下命令:
docker exec -it 614 cmdThe command above will connect to the terminal of the running container. Now type node -v and press enter, this will display the node version. To display the version of npm installed type — npm -v and press Enter.
上面的命令将连接到正在运行的容器的终端。 现在输入node -v并按Enter,这将显示节点版本。 要显示已安装的npm版本,请输入— npm -v并按Enter。
You can now remove the docker-nodejs folder created in the First step and you can upload the image to your docker hub account and use it for your NodeJs projects.
现在,您可以删除在第一步中创建的docker-nodejs文件夹,然后可以将映像上传到docker hub帐户,并将其用于NodeJs项目。
Hopefully, this post will help you in creating your own image of NodeJs Windows Container.
希望本文能帮助您创建自己的NodeJs Windows容器映像。
翻译自: https://medium.com/@taranpreet_94321/lightweight-nodejs-windows-container-nano-server-372c8fbf667a
node.js轻量级
相关资源:nanocrawler:基于Web的前端,用于查看有关Nano节点的信息并浏览Nano网络-源码