docker3 docker-compose.yml 文件编写

docker-compose.yml文件了 到nodeapp 顶级目录操作 文件下操作
vi docker-compose.yml

内容如下:
// 不能使用镜像的名字了 需要打包编译 把images文件下的node文件进行构建
// depends_on依赖的容器
version: "2"
services:
  db:
    image: mariadb
    environment:
      MYSQL_ROOT_PASSWORD: "123456"
      MYSQL_DATABASE: "nodeapp"
      MYSQL_USER: "guozimo"
      MYSQL_PASSWORD: "123456"
    volumes:
      - dbdata:/var/lib/mysql
  node:
    build:
      context: "./images/node"
      dockerfile: Dockerfile
    depends_on:
      - db  
  web:
    image: nginx
    ports:
      - "8080:80"  
    depends_on:
      - node   
    volumes:
      - "./images/nginx/conf.d:/etc/nginx/conf.d"  
      - "./images/node/web/public:/public" 
volumes:
  dbdata:
    driver: local  


    ////////
    vi default.conf
    下面是default.conf的内容
    静态文件的处理 动态文件的处理
    curl http://127.0.0.1:3000

    server{
      listen 80;
      server_name localhost 39.105.91.188;
      location /{
        root /public;
        index index.html;
      }
      location /api{
        proxy_pass http://127.0.0.1:3000
      }
    }  


    docker-compose up  
    注释代码:
    /**
    **/


let http =require("http");
http.createServer(function(req,res){
 res.end("3000");
}).listen(3000,function(){
    console.log('node server start at port 3000')
});

docker-compose down
docker-compose up --build 
// 修改代码后需要重新编译 重新编译node镜像


关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
 防火墙重启
 systemctl restart docker


server{
      listen 80;
      server_name localhost 39.105.91.188;
      location /{
        root /public;
        index index.html;
      }
      location /api{
        proxy_pass http://node:3000;
      }
    } 


    根目录访问:
    curl http://localhost:8080/api

    继续修改server.js
    echo index.html > index.html
 [root@guozimo public]# echo 404.html > 404.html
 
 如果docker-compose up --build
 访问curl http://locahost:8080/api 数据库访问报错
 可以尝试更新一下
 docker pull mysql
 docker pull mariadb
 再重新docker-compose up --build
 重新访问 curl http://locahost:8080/api

扫描二维码关注公众号,回复: 8708299 查看本文章

      


 

发布了308 篇原创文章 · 获赞 27 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/gwdgwd123/article/details/103957555