简体   繁体   English

使用 docker-compose.yml 文件中的卷进行代码同步但无法正常工作

[英]Use volumes in docker-compose.yml file for code syncing but can't work properly

index.js index.js

const express = require('express')
const router = require('./routes')

const app = express()

app.use(router)

app.listen(3000, () => {
   console.log('Server is up on port 3000')
})

routes.js路由.js

const express = require('express')
const todos = require('./_data')

const router = new express.Router()

router.get('/', (req, res) => {
  res.send('Todo APP!!!')
})

router.get('/todos', (req, res) => {
 res.send(todos)
})

module.exports = router

Dockerfile Dockerfile

FROM node:alpine
WORKDIR /app
COPY package.json . 
RUN npm install 
COPY . .
CMD ["npm", "run", "start"]

docker-compose docker-compose

  version: '3'
  services:
   node_backend_api:
    build: .
    ports: 
     - "3000:3000"
    volumes:
     - "/app/node_modules"
     - ".:/app"
    environment:
     - CHOKIDAR_USEPOLLING=true 

Use volumes in docker-compose.yml file for source code syncing.使用 docker-compose.yml 文件中的卷进行源代码同步。 When I change source code from my local project it will also change inside docker container.当我从本地项目更改源代码时,它也会在 docker 容器内更改。 It works fine when I hit command "docker-compose up" but it can not reflect changes in web browser when I want to see the output.当我点击命令“docker-compose up”时它工作正常,但当我想查看 output 时,它不能反映 web 浏览器中的变化。

So first, remove: COPY. .所以首先,删除: COPY. . COPY. . from your dockerfile .从您的dockerfile You don't want to copy the files from host to container, you want to use volumes.您不想将文件从主机复制到容器,您想使用卷。

Then, remove - "/app/node_modules" from your docker-compose.yml file There is no point in doing this (btw why are you even doing this? please read the documentation: https://docs.docker.com/compose/compose-file/compose-file-v3/#volumes ).然后,从您的docker-compose.yml文件中删除- "/app/node_modules"这样做没有意义(顺便说一句,您为什么还要这样做?请阅读文档: https://docs.Z05B6053C41A2130AFDZBFC3B1/composeA2130AFDZBFC3B1/ -file/compose-file-v3/#volumes )。

Finally re-up your stack while building it:最后在构建它时重新建立你的堆栈:

docker-compose up --build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM