[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(200, {"Content-Type": "text/plain"}); response.end("Hello World\n"); }); // Listen on port 8000, IP defaults to 127.0.0.1 server.listen(8000); // 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 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-node COPY index.js . EXPOSE 8000 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 8000:8000 myserver
相关文章
- js面试题及答案2020_JS面试题大全
- centos 7 开启docker的2375端口
- Docker安装Sentinel
- docker 常用命令
- Docker实用工具
- Docker容器实战三:安装部署
- Docker 离线安装_docker官方中文文档
- Vue.js – 引入外部 JS 文件
- Docker-网站部署实践
- MinIO的使用(内含docker的简单使用)
- 【Docker】使用docker安装部署NextCloud私人网盘
- 基于Wechaty+Docker打造一个ChatGPT机器人
- 「 【Docker】用docker运行2048小游戏(玩转docker) 」
- 云原生之使用docker部署httpd服务
- Docker - 搭建私有云、映射数据卷、网络
- 【Docker 系列】docker 学习 二,Docker 的常用命令
- Docker - docker-compose搭建ELK服务
- 软件测试|使用docker搞定 Python环境搭建
- 干货 | 用P2P方法快速分发Docker镜像
- 使用Docker搭建Redis集群(dockerredis)
- Docker实现Redis的轻松部署(dockerredis)
- 使用 JS 操作 Redis:实现强大数据存储.(js调用redis)
- 使用JS控制Oracle数据库的更新(js控制oracle更新)
- JS实时链接Oracle让数据库访问更便捷(js实时连接oracle)