如何在Docker上构建Node.js应用程序

Docker has been the latest trending topic over the past couple of years and if you haven’t been to space or spent some time in the caves, you probably must have come across this tech buzzword.

在过去的几年中,Docker一直是最新的趋势话题,如果您没有去过太空或在洞穴中度过了一段时间,那么您可能一定遇到过这个技术流行语。

So, what is docker?

那么,码头工人是什么?

Docker is a free and open source development platform that allows developers to develop and deploy their applications in containers. Containers come with their own libraries and frameworks and enable applications to be developed in an isolated environment from the host Operating System. This guide takes you the process of how to build a Node.js application on Docker.

Docker是一个免费的开源开发平台,允许开发人员在容器中开发和部署其应用程序。 容器带有自己的库和框架,并使应用程序可以在与主机操作系统隔离的环境中进行开发。 本指南将带您了解如何在Docker上构建Node.js应用程序的过程。

那么,为什么要在docker上构建应用程序呢? (So why should you build your application on docker?)

  1. The consistency of the working environment. The application will run in the same manner for all developers and clients alike at whatever phase; be it in development, staging or in production.

    工作环境的一致性。 在任何阶段,该应用程序将以相同的方式对所有开发人员和客户端运行。 无论是在开发,分阶段还是在生产中。
  2. Docker containers ship their own libraries and frameworks as earlier pointed out. Containerization eliminates the need for manually installing libraries and dependencies. You can launch a fully fledged and complete development environment ready for deployment of your application.

    Docker容器如先前指出的那样,将运送自己的库和框架。 容器化消除了手动安装库和依赖项的需要。 您可以启动功能齐全的完整开发环境,以准备部署应用程序。

Now let’s dive in and see how you can build a Node.js application on Docker.

现在,让我们深入了解如何在Docker上构建Node.js应用程序。

先决条件 (Prerequisites)

  • Docker installed on your system

    系统上已安装Docker
  • NPM

    NPM

步骤1:创建Node.js应用程序 (Step 1: Creating the Node.js application)

The first step is to create a Node.JS application
Create a new directory and navigate into it

第一步是创建一个Node.JS应用程序
创建一个新目录并进入该目录

$ mkdir nodejs-app && cd nodejs-app

Next, initialize the directory with npm (NodeJS Package Manager). This creates a package.json file that will handle dependencies of the application.

接下来,使用npm(NodeJS Package Manager)初始化目录。 这将创建一个package.json文件,该文件将处理应用程序的依赖项。

$ npm init

Sample Output

样本输出

You will be prompted for the details of the application such as Name, Version, description, etc. You can decide to specify your won preferred details of if you desire to leave the defaults as they are, simply press ENTER.

系统将提示您输入应用程序的详细信息,例如名称,版本,描述等。如果您希望保留默认值,则可以决定指定赢得的首选详细信息,只需按Enter。

At the end of the prompt, type Yes to accept the changes.

在提示的末尾,键入Yes接受更改。

Next, run

接下来,运行

$ npm install express –save

Sample Output

样本输出

This adds the express framework as the first dependency

这将快速框架添加为第一个依赖项

步骤2.为Node.js应用程序创建文件 (Step 2. Creating files for the Node.js application)

We are going to create 3 files for the Node.Js application.

我们将为Node.Js应用程序创建3个文件。

  • index.js

    index.js
  • index.html

    index.html
  • about.html

    about.html

For the index.js file, add the following content.

对于index.js文件,添加以下内容。

const express = require('express')
const app = express()
app.get('/', ( req, res ) => {
    res.sendFile(`${__dirname}/index.html`)
})

app.get('/about', (req, res) => {
    res.sendFile(`${__dirname}/about.html`)
})
app.listen(3000, () => {
    console.log('Listening on port 3000!')
})

The above file creates 2 routes for the index.html and about.html pages which listen on port 3000.

上面的文件为侦听端口3000的index.htmlabout.html页面创建2条路由。

For the index.html file, add some test content as shown.

对于index.html文件,添加一些测试内容,如图所示。

For the about.html page, add the following content.

对于about.html页面,添加以下内容。

Great! Our Node.JS application is now ready for launching.

大! 我们的Node.JS应用程序现在可以启动了。

步骤3:运行Node.JS应用程序 (Step 3: Running the Node.JS application)

To run the application, run the command

要运行该应用程序,请运行以下命令

$ node index.js

Sample Output

样本输出

Now, open your browser and go to index.html page by typing the URL as shown

现在,打开浏览器并通过输入URL进入index.html页面,如下所示

https://ip-address:3000

To visit the about.html page, append the /about at the end of the URL

要访问about.html页面,请在网址末尾附加/ about

https://ip-address:3000/about

第4步:Docker化Node.JS应用程序 (Step 4: Dockerizing the Node.JS application)

Next, you are going to define a working environment inside your container. This working environment encompasses dependencies, pre-installed applications, and libraries to mention just a few. The container doesn’t come with the whole Operating System, as opposed to a VM. It only ships dependencies, applications and config files and this makes it much lighter, faster, and resource friendly than ordinary virtual machines.

接下来,您将在容器内定义一个工作环境。 这个工作环境包括依赖项,预安装的应用程序和库,仅举几例。 与VM相比,该容器并不随整个操作系统一起提供。 它仅提供依赖项,应用程序和配置文件,与普通的虚拟机相比,它更加轻巧,快速且资源友好。

Contrary to VM, however, the container doesn’t hold the whole operating system—just applications, dependencies, and configuration. This makes Docker containers much lighter and faster than regular VM’s.
Create a file dockerfile in the app’s directory

但是,与VM相反,该容器不能容纳整个操作系统-仅包含应用程序,依赖项和配置。 这使得Docker容器比常规VM更加轻巧和快速。
在应用程序目录中创建文件dockerfile

$ touch dockerfile

Add the following content

添加以下内容

FROM node:carbon

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "index.js"]

The first line indicates that we went to use the latest node version for building our image
The second line creates the directory /usr/src/app for holding the app’s code inside the image.
The third and fourth line copies the package.json, file and runs the npm install command

第一行表示我们使用最新的节点版本来构建映像
第二行创建目录/usr/src/app用于将应用程序的代码保存在映像中。
第三和第四行复制package.json,文件,并运行npm install命令

The COPY instruction bundles our app inside the Docker image, so our HTML files and index.js file in our case.
The EXPOSE instruction exposes the 3000 port that our app uses.
Finally, the CMD instruction specifies the command that needs to be run for our app to start.

COPY指令将我们的应用程序捆绑在Docker映像内,因此我们HTML文件和index.js文件都属于我们。
EXPOSE指令公开了我们的应用程序使用的3000端口。
最后, CMD指令指定了启动我们的应用程序需要运行的命令。

步骤5.构建Node.JS应用程序 (Step 5. Building the Node.JS application)

With the dockerfile now created, let’s build our app using the command. Please note the presence of space and period sign at the end.

现在创建了dockerfile,让我们使用命令构建应用程序。 请注意结尾处有空格和句号。

$ docker build -t node-app .

Sample Output

Build Node App on Ubuntu 18.04

样本输出

The -t enables you to tag your docker image. In this example, we have tagged our docker image as node-app

-t使您可以标记docker映像。 在此示例中,我们已将Docker映像标记为node-app

To verify the existence of the application run

验证应用程序是否存在运行

$ docker images

Sample Output

样本输出

步骤6.运行docker容器 (Step 6. Run docker container)

We can now launch our container

现在我们可以启动我们的容器

# docker run -p 8080:3000 -d node-app

This redirects the app to open via port 8080 instead of port 3000.

这会将应用程序重定向为通过端口8080(而不是端口3000)打开。

Sample Output

样本输出

步骤7.将应用程序推送到Docker中心 (Step 7. Pushing the App to Docker hub)

The final step will be to share the node application to docker hub where people can access it and download its image.
To do so, ensure you have an account at https://hub.docker.com
Next, build the image again using your docker hub credentials as shown

最后一步是将节点应用程序共享到docker hub,人们可以在其中访问它并下载其映像。
为此,请确保您在https://hub.docker.com拥有一个帐户
接下来,使用您的Docker Hub凭据再次构建映像

# docker build -t [USERNAME]/tag .

In our case, the command will be

在我们的例子中,命令将是

# docker build -t winnieondara/node-app .

Where username => winnieondara
Image tag => node-app

其中用户名=> winnieondara
图片标签=> node-app

Next, login to docker on the terminal using your credentials

接下来,使用您的凭据在终端上登录docker

# docker login

Sample Output

样本输出

Finally, push the image to docker hub

最后,将映像推送到docker hub

# docker push winnieondara/node-app

Sample Output

样本输出

Congratulations! Now, log in to your docker account and be sure to find your Node.JS application image as shown. The image can then be downloaded and run by other users

恭喜你! 现在,登录到您的docker帐户,并确保找到所示的Node.JS应用程序映像。 然后可以下载图像并由其他用户运行

This wraps up our tutorial on how to build a Node.js application on docker. Feel free to give it a try and tell us how it went. We value your feedback.

这总结了有关如何在Docker上构建Node.js应用程序的教程。 随时尝试一下,告诉我们它如何进行。 我们重视您的反馈。

翻译自: https://www.journaldev.com/27423/node-js-docker

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值