[译]将在Microsoft IIS 10上运行的Node.js应用程序部署到Docker的Windows Server Core 2016容器中
By robot-v1.0
本文链接 https://www.kyfws.com/applications/deploy-node-js-application-running-on-microsoft-ii-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 7 分钟阅读 - 3300 个词 阅读量 0[译]将在Microsoft IIS 10上运行的Node.js应用程序部署到Docker的Windows Server Core 2016容器中
原文地址:https://www.codeproject.com/Articles/1257955/Deploy-Node-js-Application-Running-on-Microsoft-II
原文作者:Arthur V. Ratz
译文由本站 robot-v1.0 翻译
前言
In this brief article, we will demonstrate how to deploy a Node.js application and run it on Docker’s Windows Server Core 2016 Containers
在这篇简短的文章中,我们将演示如何部署Node.js应用程序并在Docker的Windows Server Core 2016容器上运行它.
- 下载antispamnb_docker_on_azure_deploy.zip-1.8 MB(Download antispamnb_docker_on_azure_deploy.zip - 1.8 MB) 注意(Note):(:)您可以通过访问以下链接来评估本文中讨论的即用型Web应用程序(You can evaluate the ready-to-use web-application being discussed in this article by visiting the following link) http://antispamnb.eastus.cloudapp.azure.com/(http://antispamnb.eastus.cloudapp.azure.com/) .(.)
编者注(Editorial Note)
在这篇简短的文章中,我们将讨论如何将运行在Microsoft IIS 10上的Node.js Web应用程序部署到Docker的Windows Server Core 2016容器中.本文读者将了解如何在Windows上安装和配置Docker,以及如何通过创建和使用Dockerfile来部署Node.js Web应用程序,本文将对此进行详细讨论.(In this brief article, we will discuss how to deploy Node.js web-application running on Microsoft IIS 10 to Docker’s Windows Server Core 2016 containers. The audience of this article’s readers will find out how to install and configure Docker on Windows, as well as how to deploy a Node.js web-application by creating and using a Dockerfile, thoroughly discussed in this article.)
背景(Background)
在Windows上安装和配置Docker(Installing And Configuring Docker On Windows)
在部署和运行本文介绍的Web应用程序之前,我们首先必须在Windows Server上安装Docker.为此,我们必须通过以下链接从Docker网站下载安装文件:(Before deploying and running the web-application introduced in this article, we first must install Docker on Windows Server. To do this, we must download the installation file from Docker web-site by visiting the following link:) https://store.docker.com/editions/community/docker-ce-desktop-windows(https://store.docker.com/editions/community/docker-ce-desktop-windows) .在下载特定文件之前,我们必须创建一个Docker帐户.(. Before downloading the specific file, we must create a Docker account.)
由于我们已经下载了Docker安装文件,因此我们现在可以运行Docker安装:(Since we’ve downloaded the Docker installation file, now we can run Docker installation:)
创建Dockerfile(Creating Dockerfile)
在本段中,我们将演示如何创建用于执行Windows Server Core 2016实例自动构建的Dockerfile,该Dockerfile已预先配置为在Microsoft IIS 10下运行Node.js Web应用程序.在此步骤中,我们要做的全部工作要做的是:(In this paragraph, we will demonstrate how to create a Dockerfile used to perform an automatic build of Windows Server Core 2016 instance, pre-configured to run Node.js web-applications under Microsoft IIS 10. During this step, all that we have to do is:)
- 从Docker仓库中提取Microsoft/windowsservercore映像(Pull Microsoft/windowsservercore image from Docker’s repository)
- 将所有Web应用程序的文件复制到我们自己创建的nodejs-core映像中(Copy all web-application’s files into our own nodejs-core image being created)
- 将Microsoft IIS 10 Web服务器角色添加到Windows Server Core 2016映像(Add Microsoft IIS 10 web-server role to Windows Server Core 2016 image)
- 下载并预安装运行Node.js应用程序所需的所有IIS 10组件(Download and pre-install all IIS 10 components required to run Node.js applications)
- 安装运行Web应用程序所需的所有Node.js模块(Install all Node.js modules required to run the web-application)
- 设置运行Node.js Web应用程序所需的环境变量(Set the environment variables required to run Node.js web-application)
- 公开Web应用程序侦听的TCP端口(Expose a TCP-port listened by the web-application)
- 为Web应用程序运行提供特定的入口点(Provide a specific entry point for the web-application to run) 为了完成所有这些步骤,我们需要在Dockerfile中添加以下几行.(To accomplish all these steps, we need to add the following lines to our Dockerfile.)
通过添加(By adding the) FROM
该文件顶部的子句,我们实际上是指定一个从Docker存储库下载的图像,该图像将用于构建我们自己的nodejs-core图像:(clause at the top of this file, we’re actually specifying an image, downloaded from Docker’s repository, that we’re about to use for building our own nodejs-core image:)
FROM microsoft/windowsservercore:latest
在这种情况下,我们选择了Microsoft/windowsservercore映像,并在此映像的基础上创建了自己的包含Windows Server Core 2016的nodejs-core映像.(In this particular case, we’ve selected Microsoft/windowsservercore image, based on which we will create our own nodejs-core image containing Windows Server Core 2016.)
之后,我们必须指定Web应用程序的工作目录路径,所有应用程序的文件都将复制到该目录中.为此,我们必须将以下行添加到要创建的Dockerfile中:(After that, we must specify the working directory path of our web-application, to which all application’s files will be copied. To do this, we must add the following line to the Dockerfile being created:)
WORKDIR /inetpub/wwwroot
由于已经提供了应用程序的工作目录路径,因此现在可以使用COPY指令将所需的所有Web应用程序文件传输到正在构建的图像中,如下所示:(Since we’ve provided the application’s working directory path, now we can use COPY directive to transfer all web-application’s files required to the image being constructed, as follows:)
COPY sources/ .
COPY sources/node_modules/webworker-threads
C:/Users/ContainerAdministrator/AppData/Roaming/npm/node_modules/webworker-threads
COPY /sources/package*.json ./
最后,我们必须向正在创建的Windows Server Core 2016映像中添加Microsoft IIS 10 Web服务器角色:(Finally, we have to add Microsoft IIS 10 web-server role to Windows Server Core 2016 image being created:)
RUN powershell Add-WindowsFeature Web-Asp-Net45,Web-Http-Tracing,Web-Scripting-Tools,Web-WebSockets;
下一个导入步骤是提供执行Web应用程序运行所需的软件组件的自动安装所需的说明:(The next import step is to provide the instruction required to perform an automated install of software components required for the web-application to run:)
ADD https://nodejs.org/dist/v8.11.4/node-v8.11.4-x64.msi node-v8.11.4-x64.msi
RUN powershell Start-Process msiexec -ArgumentList '/i node-v8.11.4-x64.msi /qn /l*v nodejs.log' -Wait ;
ADD http://go.microsoft.com/fwlink/?LinkID=615137 rewrite_amd64.msi
RUN powershell Start-Process msiexec -ArgumentList '/i rewrite_amd64.msi /qn /l*v rewrite.log' -Wait ;
ADD https://github.com/tjanczuk/iisnode/releases/download/v0.2.21/iisnode-full-v0.2.21-x64.msi
iisnode-full-v0.2.21-x64.msi
RUN powershell Start-Process msiexec -ArgumentList '/i iisnode-full-v0.2.21-x64.msi /qn /l*v
iisnode.log' -Wait ;
具体来说,我们需要为Microsoft IIS 10 Web服务器安装Node.js v8.11.4,URL Rewrite和IISNode组件.要下载这些组件,我们必须使用ADD指令子句,该子句使用wget-function来下载所需的安装文件并将其保存到正在创建的映像中.(Specifically, what we have to install Node.js v8.11.4, URL Rewrite and IISNode components for Microsoft IIS 10 web-server. To download these components, we must use ADD directive clause that uses wget-functionality to download required installation files and save them into the image being created.)
成功下载并安装Node.js之后,我们需要安装运行Web应用程序所需的Node.js模块数量.为此,我们将以下行添加到我们的Dockerfile中:(After we’ve successfully downloaded and installed Node.js, we need to install the number of Node.js modules required for our web-application to run. To do this, we add the following lines to our Dockerfile:)
RUN powershell npm install --global --production npm
RUN powershell npm install --global --production node-gyp
RUN powershell npm --vcc-build-tools-parameters='[""--allWorkloads""]'
install --global --production windows-build-tools
为此,我们使用(For that purpose, we use the) npm install
命令以下载并安装特定的Node.js模块,例如(command to download and install specific Node.js modules such as) npm
,(,) node-gyp
和(and) build tools for Visual Studio 2017
.(.)
另一个重要的配置步骤是,我们需要设置特定Node.js模块正常运行所需的环境变量数量:(Another important configuration step is that we need to setup the number of environment variables required by specific Node.js modules to function properly:)
ENV PATH 'C:\users\containeradministrator\.windows-build-tools\Python27;
C:\Program Files (x86)\MSBuild\14.0\bin\;C:\Windows\system32;C:\Windows;
C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files\nodejs\;C:\Users\ContainerAdministrator\AppData\Local\Microsoft\WindowsApps;
C:\Users\ContainerAdministrator\AppData\Roaming\npm;'
ENV PYTHON '%USERPROFILE%\.windows-build-tools\Python27\python.exe'
ENV PYTHONPATH '%USERPROFILE%\.windows-build-tools\python27'
ENV VCTargetsPath "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\v140"
ENV NODE_PATH "C:\Users\ContainerAdministrator\AppData\Roaming\npm\node_modules"
最后,我们还必须下载并安装Web应用程序运行所需的Node.js模块:(Finally, we also must download and install Node.js modules required by the web-application to run:)
RUN powershell npm install --global --production body-parser busboy cluster
consolidate cookie-parser debug express express-fileupload favicon http logger math net
path querystring url util jade bindings
之后,我们需要指定将由我们的Web应用程序侦听的TCP端口(例如TCP-8080端口),并为我们的应用程序提供一个入口点:(After that, we need to specify the TCP-port (e.g., TCP-8080 port) that will be listened by our web-application, and provide an entry point for our application:)
EXPOSE 8080
ENTRYPOINT node.exe ./server.js
构建并运行Node.js Web应用程序(Building And Running Node.js Web Application)
要使用上一步中创建的Dockerfile生成映像,我们必须进入PowerShell控制台并执行以下命令:(To build an image by using Dockerfile that we have created in the previous step, we have to enter PowerShell console and execute the following command:)
PS C:\antispamnb> docker build --no-cache -t nodejs-core .
由于我们已经构建了nodejs-core映像,因此现在我们可以通过输入以下命令来运行Web应用程序:(Since we’ve built nodejs-core image, now we can run the web-application by entering the following command:)
PS C:\antispamnb> docker run -d -p 80:8080 --restart=always --name=antispamnb nodejs-core
之后,让我们检查一下我们的应用程序是否真正起作用.为此,我们必须打开Web浏览器和以下链接:(After that, let’s check if our application really works. To do that, we must open web browser and the following link:)
http://antispamnb.eastus.cloudapp.azure.com:80/(http://antispamnb.eastus.cloudapp.azure.com:80/)
兴趣点(Points of Interest)
在本文中,我们讨论了如何创建Dockerfile,该文件可用于将Windows Server Core 2016上运行的Node.js Web应用程序部署到Docker Windows容器.(In this article, we’ve discussed how to create a Dockerfile that can be used to deploy a Node.js web-application running on Windows Server Core 2016 to Docker Windows containers.)
历史(History)
- 2018年9月1日(September 1, 2018)-本文的第一版已发布(- The first revision of this article was published)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C Javascript XML C++ Node.js VS2013 PowerShell text virtualization JSON 新闻 翻译