Learn how to build a simple Node.js web server with Docker. In this lesson, we'll create a Dockerfile for a simple Node.js script, copy and build it into a Docker image, and start a container to run the web server.

We have a simple express server:

// Load the http module to create an http server.
var http = require('http'); // Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(, {"Content-Type": "text/plain"});
response.end("Hello World\n");
}); // Listen on port 8000, IP defaults to 127.0.0.1
server.listen(); // Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

Create a DockerFile:

A Docker file is a text document which provides the Docker engine with instructions on how to build the image. Every Docker file starts with the "from" keyword, followed by the name of our base image.  A base image is another Docker image from which we'll build our image. Since we're running a Node web server, we'll use the mhart/alpine-node image as our base.

FROM mhart/alpine-node

Then we say "copy" our main enter file to our image:

COPY index.js .

By default, you can think of Docker as having a firewall with no ports opened. Since we're running a web server, we'll need to open up the port the server is running on.

Let's add "expose 8000" to our Docker file.

EXPOSE 

The last line in every Docker file is typically the "cmd" or "command" keyword, followed by our executable. We'll use a simple command to start our web server, "Node index.js." This is now a valid Docker file.

CMD node index.js

Full:

FROM mhart/alpine-node
COPY index.js .
EXPOSE
CMD node index.js

Next, we'll use it to build our Docker image. From our project directory, run "Docker build -t myserver."

docker build -t myserver .

-t: to name our docker image.

.: to tell which dir to look

After executing it, you can verify the image was built by running:

docker images

We can test it by running "Docker run." We'll also specify a "-p" flag, which maps a host port to a container port.

You can think of this as port forwarding through a firewall. Let's map port 8000 on our host to port 8000 on our container. Lastly, we'll add the name of our Docker image we specified when we built our image, "myserver."

docker run -p : myserver

Github

[Docker] Build a Simple Node.js Web Server with Docker的更多相关文章

  1. Node.js Web 开发框架大全《中间件篇》

    这篇文章与大家分享优秀的 Node.js 中间件模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处 ...

  2. 利用OpenShift托管Node.js Web服务进行微信公众号开发

    最近写了一个微信的翻译机器人.用户只要关注该公众号,发送英文的消息,就能收到中文翻译的回复.有兴趣的读者可以扫描下面的二维码关注该公众号,尝试发送英文单词试试看.(有时候第一次发送单词会收到“该公众号 ...

  3. node.js Web应用框架Express.js(一)

    什么是Express.js Express 是一个简洁而灵活的 node.js Web应用框架, 提供一系列强大特性帮助你创建各种Web应用,提供丰富的HTTP工具以及来自Connect框架的中间件随 ...

  4. Node.js Web模块

    什么是Web服务器? Web服务器是处理由HTTP客户端发送的,如web浏览器的HTTP请求的软件应用程序,并返回响应于客户端网页. Web服务器通常伴随着图片,样式表和脚本的HTML文档. 大多数W ...

  5. KoaHub.js是基于 Koa.js 平台的 Node.js web 快速开发框架

    koahubjs KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架.可以直接在项目里使用 ES6/7(Generator Function, Class, A ...

  6. Node.js web快速入门 -- KoaHub.js

    介绍 KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架.可以直接在项目里使用 ES6/7(Generator Function, Class, Async & ...

  7. 《Node.js入门》CentOS 6.5下Node.js Web开发环境搭建笔记

    近期想尝试一下英特尔的基于WebRTC协同通信开发套件,所以须要在本地搭建Node.js Web的开发測试环境. 这里讲的是CentOS 下的搭建方法.使用Windows的小伙伴请參考: <No ...

  8. Node.js+Web TWAIN,实现Web文档扫描和图像上传

      目录(?)[+] 通过Dynamic Web TWAIN SDK和Node.js的组合,只需要几行代码就可以实现在浏览器中控制扫描仪,获取图像后上传到远程服务器. 原文:Document Imag ...

  9. Windows 7下Node.js Web开发环境搭建笔记

    Node.js是什么? 我们看看百科里怎么说的?JavaScript是一种运行在浏览器的脚本,它简单,轻巧,易于编辑,这种脚本通常用于浏览器的前端编程,但是一位开发者Ryan有一天发现这种前端式的脚本 ...

随机推荐

  1. 关于client浏览器界面文字内容溢出用省略号表示方法

    在实际的项目中,因为client浏览器文字内容的长度不确定性和页面布局的固定性,难免会出现文字内容超过div(或其它标签,下同)区域的情况.此时比較好的做法就是当文字超过限定的div宽度后自己主动以省 ...

  2. theme- 自定义控件属性

    今天想要在一个控件中增加自己的一条属性,具体步骤如下 1.在frameworks/base/core/res/res/values/attr中注册属性 因为我们希望增加的属性是在AutoComplet ...

  3. golang sync.Cond

    package main import ( "fmt" "sync" "time" ) func main() { wait := sync ...

  4. BZOJ2738: 矩阵乘法(整体二分)

    Description 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. Input 第一行两个数N,Q,表示矩阵大小和询问组数: 接下来N行N列一共N*N个数,表示这个矩阵: ...

  5. ArcGIS 点要素新增点

    IFeatureLayer layer = FrmMain.m_mapControl.get_Layer(0) as IFeatureLayer; IFeatureClass featureClass ...

  6. WebSocket兼容到低版本浏览器

    就目前而言,WebSocket是最好的Web通信解决方案了.但是IE从10才开始兼容它,对于目前大量IE8存在的市场,原生的WebSocket显然不太实用,我们需要低版本兼容的解决方案.于是我模拟We ...

  7. HTML基础第六讲---表格

    转自:https://i.cnblogs.com/posts?categoryid=1121494 上一讲,讲了关于<控制表格及其表项的对齐方式>,在这里我要讲讲表格及其属性 ,然后大家在 ...

  8. docker进入容器的几种方法

    一 启动进入容器指定bash 退出后容器关闭 [root@Centos-node3 ~]# docker run -it centos bash [root@83c6b25aca09 /]# 二 do ...

  9. jquery追加元素,移除DOM,jqueryDOM操作

    1.append() 方法在被选元素的结尾插入内容. 2.prepend() 方法在被选元素的开头插入内容. 3.after() 方法在被选元素之后插入内容. 4.before() 方法在被选元素之前 ...

  10. ThinkPHP5.0---URL访问

    ThinkPHP 5.0 在没有启用路由的情况下典型的URL访问规则是(采用 PATH_INFO 访问地址): http://serverName/index.php(或者其它应用入口文件)/模块/控 ...