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

最新文章

  1. [CentOS 7] 安装nginx
  2. 【转】如何保护自己的QQ号
  3. redmine中创建项目与跟踪标签(原创)
  4. MVVM开源框架Knot.js 教程1 - CBS初步
  5. R2D2 and Droid Army(多棵线段树)
  6. grunt构建前端自动化的开发环境
  7. Android 线程Thread的2种实现方法
  8. CentOS6.7下安装MySQL
  9. 【Android应用开发】 推送原理解析 极光推送使用详解 (零基础精通推送)
  10. 什么是IaaS, PaaS和SaaS及其区别
  11. Nginx之——日志按日期分割的实现(基于CentOS操作系统)
  12. xgboost中XGBClassifier()参数详解
  13. Android笔记(四):RecyclerView
  14. 如何检查显卡类型,DirectX和OpenGL的版本
  15. eclipse svn 冲突解决
  16. CEF3开发者系列之CefEnableHighDPISupport详解
  17. Linux环境进程间通信(二): 信号--转载
  18. mysql 添加字段,修改字段的用法
  19. AT2165 Median Pyramid Hard
  20. 007.ASP.NET MVC控制器依赖注入

热门文章

  1. Ansible学习记录二:命令
  2. Linux 时区的修改
  3. LightOJ 1291 Real Life Traffic
  4. android 自己定义View之SubmitView
  5. hdu5400Arithmetic Sequence
  6. 用C#生成随机中文汉字验证码的基本原理
  7. C# 向DataTable中插入数据或伪造DataTable
  8. DG动态性能视图详解
  9. Qt之模型/视图(自己定义button)
  10. WebSocket兼容到低版本浏览器