Dockerizing a Node.js web app

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應(yīng)用放到Docker容器中。它假定你有一個工作中的Docker 安裝并且基本了解如何構(gòu)建Node.js應(yīng)用徘熔。
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.
在本指南的第一部分我們將創(chuàng)建一個簡單的Node.js web應(yīng)用,然后為應(yīng)用建一個Docker image菩收,最后作為容器運(yùn)行 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 允許你將包含所有依賴的應(yīng)用打包到一個被稱為container(容器)的標(biāo)準(zhǔn)單位中宽堆,為軟件開發(fā)澎粟。一個container是剝離基本版本的Linux操作系統(tǒng)。一個Image是你裝入容器的軟件护侮。

Create the Node.js app

創(chuàng)建Node.js應(yīng)用

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:
首先,創(chuàng)建一個新目錄為所有文件的根储耐。在這個目錄創(chuàng)建一個package.json文件描述應(yīng)用和它的依賴:

{
  "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:
然后羊初,創(chuàng)建server.js文件定義web應(yīng)用使用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運(yùn)行這個應(yīng)用什湘。首先长赞,你需要在應(yīng)用中建一個Docker image。

Creating a Dockerfile

Create an empty file called Dockerfile:
創(chuàng)建空文件名叫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 nodeavailable 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中創(chuàng)建目錄來存放應(yīng)用代碼得哆,這將是你應(yīng)用的工作目錄:

# 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已經(jīng)安裝,所以我們 下一步需要做的事情就是安裝你的應(yīng)用程序依賴性使用 npm 二進(jìn)制文件:

# 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 COPYinstruction:
使用COPY指令復(fù)制應(yīng)用代碼到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 守護(hù)進(jìn)程:

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定義你的運(yùn)行時運(yùn)行應(yīng)用闸餐。這里我們使用基本的npm start將運(yùn)行node server.js來開啟你的服務(wù):

CMD [ "npm", "start" ]

Your Dockerfile should now look like this:
你的Dockerfile 應(yīng)該像這樣:

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的目錄運(yùn)行下面的命令建立Docker image饱亮。-t 標(biāo)志你的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

運(yùn)行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:
運(yùn)行您的image , -d運(yùn)行在分離模式的容器舍沙,離開的容器中運(yùn)行在后臺近上。-p將公共端口到一個私有端口。運(yùn)行你先前建立的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:
如果你需要進(jìn)入容器可以使用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:
要測試你的應(yīng)用拂铡,得到應(yīng)用的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:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末戈锻,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子和媳,更是在濱河造成了極大的恐慌,老刑警劉巖哈街,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件留瞳,死亡現(xiàn)場離奇詭異,居然都是意外死亡骚秦,警方通過查閱死者的電腦和手機(jī)她倘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門璧微,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人硬梁,你說我怎么就攤上這事前硫。” “怎么了荧止?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵屹电,是天一觀的道長。 經(jīng)常有香客問我跃巡,道長危号,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任素邪,我火速辦了婚禮外莲,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘兔朦。我一直安慰自己偷线,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布沽甥。 她就那樣靜靜地躺著声邦,像睡著了一般。 火紅的嫁衣襯著肌膚如雪安接。 梳的紋絲不亂的頭發(fā)上翔忽,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機(jī)與錄音盏檐,去河邊找鬼歇式。 笑死,一個胖子當(dāng)著我的面吹牛胡野,可吹牛的內(nèi)容都是我干的材失。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼硫豆,長吁一口氣:“原來是場噩夢啊……” “哼龙巨!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起熊响,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤旨别,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后汗茄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體秸弛,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了递览。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片叼屠。...
    茶點(diǎn)故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖绞铃,靈堂內(nèi)的尸體忽然破棺而出镜雨,到底是詐尸還是另有隱情,我是刑警寧澤儿捧,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布荚坞,位于F島的核電站,受9級特大地震影響纯命,放射性物質(zhì)發(fā)生泄漏西剥。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一亿汞、第九天 我趴在偏房一處隱蔽的房頂上張望瞭空。 院中可真熱鬧,春花似錦疗我、人聲如沸咆畏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽旧找。三九已至,卻和暖如春麦牺,著一層夾襖步出監(jiān)牢的瞬間钮蛛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工剖膳, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留魏颓,地道東北人。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓吱晒,卻偏偏與公主長得像甸饱,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子仑濒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容