从头开始创建一个简单的Node.js Hello World Docker容器

In the Dockerfile introduction post I introduced a simple Node.js Dockerfile example:

Dockerfile简介中,我介绍了一个简单的Node.js Dockerfile示例:

FROM node:14
WORKDIR /usr/src/app
COPY package*.json app.js ./
RUN npm install
EXPOSE 3000
CMD ["node", "app.js"]

NOTE: use double quotes in the CMD line. Single quotes will result in an error.

注意:在CMD行中使用双引号 。 单引号将导致错误。

Let’s use this Dockerfile to build an image, and then run the container.

让我们使用此Dockerfile生成映像 ,然后运行container

I’m going to create this file in the dev/docker/examplenode folder. I create a simple Node.js app in the app.js file, using Express:

我将在dev/docker/examplenode文件夹中创建此文件。 我使用Express在app.js文件中创建一个简单的Node.js应用程序:

const express = require('express')
const app = express()

app.get('/', (req, res) => res.send('Hello World!'))
app.listen(3000, () => console.log('Server ready'))

Super simple, but we have one dependency. I need to add it to the package.json file, so I run

超级简单,但是我们有一个依赖性。 我需要将其添加到package.json文件,所以我运行

npm init -y
npm install express

Now you can run node app.js and make sure it works:

现在,您可以运行node app.js并确保它可以运行:

Stop this process and let’s create a Docker Image from this.

停止此过程,让我们从中创建一个Docker Image。

All you need are the app.js, package.json and package-lock.json files.

您只需要app.jspackage.jsonpackage-lock.json文件。

And the Dockerfile. Create a Dockerfile file in the same folder, with no extension (not Dockerfile.txt).

还有Dockerfile。 在同一文件夹中创建一个没有扩展名的Dockerfile文件(不是Dockerfile.txt)。

You can freely delete the node_modules folder that now contains the Express library and its dependencies, but you can also create a .dockerignore file and add node_modules inside it, to make Docker ignore this folder completely.

您可以自由删除现在包含Express库及其依赖项的node_modules文件夹,但也可以创建.dockerignore文件并在其中添加node_modules ,以使Docker完全忽略此文件夹。

It works like .gitignore in Git.

它的工作方式类似于Git中的.gitignore

Run the command

运行命令

docker build -t examplenode .

It will take a while to download the Node image and run npm install, then you’ll get a successful message.

下载Node映像并运行npm install会花费一些时间,然后您将获得一条成功消息。

It’s important to note that after you first download a base image like the node one we use here, that will be cached locally, so you don’t need to download it again and the image building process will be much faster.

重要的是要注意,在您首次下载基本映像(例如我们在此处使用的node ,该映像将被本地缓存,因此您无需再次下载它,并且映像构建过程将更快。

Now we can run a container from the image:

现在我们可以从图像运行容器:

docker run -d -p 3000:3000 --name node-app examplenode

Now you can see the image running in Docker Desktop:

现在,您可以看到在Docker Desktop中运行的映像:

And you can click the “Open in browser” button to open the app running on port 3000:

您可以单击“在浏览器中打开”按钮以打开在端口3000上运行的应用程序:

Just like before! Except now the app is running in its own container, completely isolated, and we can run whatever version of Node we want in the container, with all the benefits Docker gives us.

就像以前那样! 除了现在,该应用程序在完全隔离的容器中运行,而且我们可以在容器中运行所需版本的Node,并获得Docker给我们带来的所有好处。

For example you can remove the container and run it on port 80 instead of 3000, with:

例如,您可以使用以下命令删除容器并在端口80而不是3000上运行它:

docker run -d -p 80:3000 --name node-app examplenode

The image does not need to change, all you change is the port mapping. Here’s the result:

映像不需要更改,您只需更改端口映射即可。 结果如下:

翻译自: https://flaviocopes.com/docker-node-container-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值