[Docker] Build a Simple Node.js Web Server with Docker
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
[Docker] Build a Simple Node.js Web Server with Docker的更多相关文章
- Node.js Web 开发框架大全《中间件篇》
这篇文章与大家分享优秀的 Node.js 中间件模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处 ...
- 利用OpenShift托管Node.js Web服务进行微信公众号开发
最近写了一个微信的翻译机器人.用户只要关注该公众号,发送英文的消息,就能收到中文翻译的回复.有兴趣的读者可以扫描下面的二维码关注该公众号,尝试发送英文单词试试看.(有时候第一次发送单词会收到“该公众号 ...
- node.js Web应用框架Express.js(一)
什么是Express.js Express 是一个简洁而灵活的 node.js Web应用框架, 提供一系列强大特性帮助你创建各种Web应用,提供丰富的HTTP工具以及来自Connect框架的中间件随 ...
- Node.js Web模块
什么是Web服务器? Web服务器是处理由HTTP客户端发送的,如web浏览器的HTTP请求的软件应用程序,并返回响应于客户端网页. Web服务器通常伴随着图片,样式表和脚本的HTML文档. 大多数W ...
- KoaHub.js是基于 Koa.js 平台的 Node.js web 快速开发框架
koahubjs KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架.可以直接在项目里使用 ES6/7(Generator Function, Class, A ...
- Node.js web快速入门 -- KoaHub.js
介绍 KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架.可以直接在项目里使用 ES6/7(Generator Function, Class, Async & ...
- 《Node.js入门》CentOS 6.5下Node.js Web开发环境搭建笔记
近期想尝试一下英特尔的基于WebRTC协同通信开发套件,所以须要在本地搭建Node.js Web的开发測试环境. 这里讲的是CentOS 下的搭建方法.使用Windows的小伙伴请參考: <No ...
- Node.js+Web TWAIN,实现Web文档扫描和图像上传
目录(?)[+] 通过Dynamic Web TWAIN SDK和Node.js的组合,只需要几行代码就可以实现在浏览器中控制扫描仪,获取图像后上传到远程服务器. 原文:Document Imag ...
- Windows 7下Node.js Web开发环境搭建笔记
Node.js是什么? 我们看看百科里怎么说的?JavaScript是一种运行在浏览器的脚本,它简单,轻巧,易于编辑,这种脚本通常用于浏览器的前端编程,但是一位开发者Ryan有一天发现这种前端式的脚本 ...
随机推荐
- 调用中行接口针对返回报文(xml形式)做相关处理
最近在对接中行银行接口,在获取返回报文的时候遇到一些问题,现在在这里做个总结 TIP: 在返回报文之前,要对前置机的URL请求,在这期间遇到一个坑,还是通过查看日志才发现问题 在填写转账信息的时候要求 ...
- 洛谷——P1843 奶牛晒衣服
https://www.luogu.org/problem/show?pid=1843#sub 题目背景 熊大妈决定给每个牛宝宝都穿上可爱的婴儿装 . 于是 , 为牛宝宝洗晒衣 服就成了很不爽的事情. ...
- 漫话Unity(二)
三.Unity编辑器介绍 Unity是一个商业级的3d游戏引擎.一个引擎的专业程度事实上并非体如今它多么牛b 的次世代效果,说实话那些效果即便你会用也不敢用.由于没有哪个手机是次世代的. 游戏引擎的专 ...
- gullo.me 的 natvps
gullo.me 的 natvps 1. 在下面的地址中输入内网 IP,查看许可的网络资源 https://hosting.gullo.me/plugin/support_manager/knowle ...
- php课程 12-42 php中类的关键字有哪些
php课程 12-42 php中类的关键字有哪些 一.总结 一句话总结:const.final.static 1.类常量-const2.最终版本-final3.静态成员-static 1.php中类常 ...
- golang sync.RWMutex
sync.RWMutex package main import ( "fmt" "runtime" "sync" ) func click ...
- 带你走进EJB--EJB和Spring对比(转)
http://blog.csdn.net/jnqqls/article/details/17723417 通过对EJB系列的总结和学习我们已经对EJB有了基本的了解,但是为了更进一步的去深入学习EJB ...
- 获取DOM元素到页面顶部的距离,亲测有效版本(转载)
原文:https://blog.csdn.net/u013764814/article/details/83825479 干脆点(博客就应该干脆,少扯皮) DOM元素有一个属性是offsetTop,表 ...
- 【hdu 6181】Two Paths
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=6181 [题意] 让你求从1到n的次短路 [题解] 模板题; 因为点可以重复走; 则一定会有次短路. di ...
- silverlight依据json字符串动态创建实体类
1.接收json字符串: //用JsonValue转换json字符串是为了之后获得json字符串的每行数据和每一列的列名 JsonValue jv = JsonValue.Parse(json); ...