title: Dockerizing a Node.js web app
layout: docs.hbs
Dockerizing a Node.js web app
The goal of this example is to show you how to get a Node.js application into a Docker container. It assumes you have a working Docker installation and a basic understanding of how a Node.js application is structured.
这个列子的目的是告诉你怎样将一个 Node.js应用放到Docker容器中。它假定你有一个工作中的Docker 安装并且基本了解如何构建Node.js应用。
In the first part of this guide we will create a simple web application in Node.js, then we will build a Docker image for that application, and lastly we will run the image as container.
在本指南的第一部分我们将创建一个简单的Node.js web应用,然后为应用建一个Docker image,最后作为容器运行 image。
Docker allows you to package an application with all of its dependencies into a standardized unit, called a container, for software development. A container is a stripped-to-basics version of a Linux operating system. An image is software you load into a container.
Docker 允许你将包含所有依赖的应用打包到一个被称为container(容器)的标准单位中,为软件开发。一个container是剥离基本版本的Linux操作系统。一个Image是你装入容器的软件。
Create the Node.js app
创建Node.js应用
First, create a new directory where all the files would live. In this directory create a package.json
file that describes your app and its dependencies:
首先,创建一个新目录为所有文件的根。在这个目录创建一个package.json
文件描述应用和它的依赖:
{
"name": "docker_web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "First Last <first.last@example.com>",
"main": "server.js",
"dependencies": {
"express": "^4.13.3"
}
}
Then, create an server.js
file that defines a web app using the
Express.js framework:
然后,创建server.js
文件定义web应用使用Express.js 框架:
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
// App
const app = express();
app.get('/', function (req, res) {
res.send('Hello world\n');
});
app.listen(PORT);
console.log('Running on http://localhost:' + PORT);
In the next steps, we'll look at how you can run this app inside a Docker container using the official Docker image. First, you'll need to build a Docker image of your app.
在下一步,我们将看看如何在一个Docker容器中使用官方Docker image运行这个应用。首先,你需要在应用中建一个Docker image。
Creating a Dockerfile
Create an empty file called Dockerfile
:
创建空文件名叫Dockerfile
:
touch Dockerfile
Open the Dockerfile
in your favorite text editor
用你喜欢的文本编辑器打开Dockerfile
The first thing we need to do is define from what image we want to build from.
第一件事我们要定义我们想要从什么 image 建造。
Here we will use the latest LTS (long term support) version argon
of node
available from the Docker Hub:
这里我们将使用最后 LTS(长期支持)版本 argon
of node
可从Docker Hub得到:
FROM node:argon
Next we create a directory to hold the application code inside the image, this will be the working directory for your application:
下一步我们在image中创建目录来存放应用代码,这将是你应用的工作目录:
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
This image comes with Node.js and NPM already installed so the next thing we need to do is to install your app dependencies using the npm
binary:
这个image 依赖Node.js和NPM已经安装,所以我们 下一步需要做的事情就是安装你的应用程序依赖性使用 npm
二进制文件:
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
To bundle your app's source code inside the Docker image, use the COPY
instruction:
使用COPY
指令复制应用代码到Docker image中。
# Bundle app source
COPY . /usr/src/app
Your app binds to port 8080
so you'll use the EXPOSE
instruction to have it mapped by the docker
daemon:
你的app绑定到8080
端口,所以使用EXPOSE
指令映射这个端口到 docker
守护进程:
EXPOSE 8080
Last but not least, define the command to run your app using CMD
which defines your runtime. Here we will use the basic npm start
which will run node server.js
to start your server:
最后,命令CMD
定义你的运行时运行应用。这里我们使用基本的npm start
将运行node server.js
来开启你的服务:
CMD [ "npm", "start" ]
Your Dockerfile
should now look like this:
你的Dockerfile
应该像这样:
FROM node:argon
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8080
CMD [ "npm", "start" ]
Building your image
建立你的 image
Go to the directory that has your Dockerfile
and run the following command to build the Docker image. The -t
flag lets you tag your image so it's easier to find later using the docker images
command:
去有Dockerfile
的目录运行下面的命令建立Docker image。-t
标志你的image方便你以后查找使用docker images
。
$ docker build -t <your username>/node-web-app .
Your image will now be listed by Docker:
你的image可以显示在Docker列表
$ docker images
# Example
REPOSITORY TAG ID CREATED
node argon 539c0211cd76 3 weeks ago
<your username>/node-web-app latest d64d3505b0d2 1 minute ago
Run the image
运行image
Running your image with -d
runs the container in detached mode, leaving the container running in the background. The -p
flag redirects a public port to a private port inside the container. Run the image you previously built:
运行您的image , -d
运行在分离模式的容器,离开的容器中运行在后台。-p
将公共端口到一个私有端口。运行你先前建立的image:
$ docker run -p 49160:8080 -d <your username>/node-web-app
Print the output of your app:
# Get container ID
$ docker ps
# Print app output
$ docker logs <container id>
# Example
Running on http://localhost:8080
If you need to go inside the container you can use the exec
command:
如果你需要进入容器可以使用exec命令:
# Enter the container
$ docker exec -it <container id> /bin/bash
Test
To test your app, get the port of your app that Docker mapped:
要测试你的应用,得到应用的Docker端口映射
$ docker ps
# Example
ID IMAGE COMMAND ... PORTS
ecce33b30ebf <your username>/node-web-app:latest npm start ... 49160->8080
In the example above, Docker mapped the 8080
port inside of the container to the port 49160
on your machine.
Now you can call your app using curl
(install if needed via: sudo apt-get install curl
):
$ curl -i localhost:49160
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 12
Date: Sun, 02 Jun 2013 03:53:22 GMT
Connection: keep-alive
Hello world
We hope this tutorial helped you get up and running a simple Node.js application on Docker.
You can find more information about Docker and Node.js on Docker in the following places: