博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Docker] Build a Simple Node.js Web Server with Docker
阅读量:4628 次
发布时间:2019-06-09

本文共 2418 字,大约阅读时间需要 8 分钟。

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(200, {
"Content-Type": "text/plain"}); response.end("Hello World\n");});// Listen on port 8000, IP defaults to 127.0.0.1server.listen(8000);// Put a friendly message on the terminalconsole.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 8000

 

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-nodeCOPY index.js .EXPOSE 8000CMD 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 8000:8000 myserver

 

转载于:https://www.cnblogs.com/Answer1215/p/6067279.html

你可能感兴趣的文章
关于DWG文件转换成PDF
查看>>
Jerry眼中的SAP客户数据模型
查看>>
c++, 派生类的构造函数和析构函数 , [ 以及operator=不能被继承 or Not的探讨]
查看>>
CAS (10) —— JBoss EAP 6.4下部署CAS时出现错误exception.message=Error decoding flow execution的解决办法...
查看>>
[面试]future模式
查看>>
Beta冲刺 (1/7)
查看>>
cap理论与分布式事务的解决方案
查看>>
[PHP] JQuery+Layer实现添加删除自定义标签代码
查看>>
Linux下查看和添加环境变量
查看>>
Ubuntu18.04安装英伟达显卡驱动
查看>>
Winform开发中常见界面的DevExpress处理操作
查看>>
IOS_多线程_ASI_AFN_UIWebView
查看>>
主要的约瑟夫环问题
查看>>
自定义View步骤学习笔记
查看>>
ab压力测试
查看>>
第11章 AOF持久化
查看>>
2009年3月
查看>>
03 Django REST Framework 视图和路由
查看>>
Confluence 6 配置服务器基础地址
查看>>
Android通过ksoap2调用.net(c#)的webservice
查看>>