1 在本地myapp文件夹下配置几个文件

  1. //app.js
  2. const koa = require("koa");
  3. const app = new koa();
  4. const router = require("koa-router")();
  5. router.get("/",async ctx=>{
  6. ctx.body = {
  7. code:200,
  8. msg:"hello aliyun"
  9. }
  10. })
  11. app.use(router.routes())
  12. app.listen(8080);
  1. //package.json
  2. {
  3. "name": "data",
  4. "version": "1.0.0",
  5. "description": "",
  6. "main": "app.js",
  7. "scripts": {
  8. "start": "node app.js"
  9. },
  10. "keywords": [],
  11. "author": "",
  12. "license": "ISC",
  13. "dependencies": {
  14. "koa": "^2.13.1",
  15. "koa-router": "^10.0.0"
  16. }
  17. }
//Dockerfile 使用这个文件构建一个镜像
# 引入Node
FROM node:latest
# 在容器内创建工作目录
RUN mkdir -p /app   
# 指定容器工作目录  
WORKDIR /app   
#将当前目录下的所有文件,都拷贝进入 image 文件的/app目录。
COPY . /app
RUN npm install --registry=https://registry.npm.taobao.org    
RUN npm install pm2 -g --registry=https://registry.npm.taobao.org 
#对外暴露的端口   
EXPOSE 8080
#程序启动脚本
CMD ["pm2-runtime", "app.js"]

//Dockerfile 使用这个文件构建一个镜像
# 引入Node
FROM node:latest
# 在容器内创建工作目录
RUN mkdir -p /app   
# 指定容器工作目录  
WORKDIR /app   
#将当前目录下的所有文件,都拷贝进入 image 文件的/app目录。
COPY . /app
RUN npm install --registry=https://registry.npm.taobao.org    
//原来的这一行删除
#对外暴露的端口   
EXPOSE 8080
#程序启动脚本
CMD ["node", "app.js"] //改为node

2 Linux上构建镜像

在Dockerfile同级路径下执行如下命令构建镜像

cd进入本地myapp文件夹

docker build -t myapp .

3 启动容器

docker run -d -p 8083:8080  myapp
docker run -d -p 8083:8080 --name myapp  myapp
1.使用myapp这个镜像去启动一个容器
2.主机器的8080端口映射到8080,8080端口也就是我们的Dockerfile对外暴露的接口。
3.-d表示后台运行。-p表示指定端口映射
docker stop xxx //停止容器
docker rm xxx //删除容器
netstat -ntlp
kill -9 11952        //后面的11952是PID