简体   繁体   English

Docker 即使设置了 -L,Nodemon 也不会在更改时重新加载

[英]Docker Nodemon not reloading on changes even though -L is set

Hi I'm trying to dockerize an app im currently working on.嗨,我正在尝试对我目前正在开发的应用程序进行 dockerize。 It uses nodejs and mariadb.它使用 nodejs 和 mariadb。 I have some difficulties with figuring out how to make nodemon work.我在弄清楚如何使 nodemon 工作时遇到了一些困难。

I tried using --legacy-watch or -L which is the short form but it didn't change the result.我尝试使用 --legacy-watch 或 -L 这是简短的形式,但它并没有改变结果。

NPM installs all dependecies correct i even get the nodemon text but it doesn't restart the server when i make changes. NPM 正确安装了所有依赖项,我什至得到了 nodemon 文本,但是当我进行更改时它不会重新启动服务器。

Would be gal if anyone could help如果有人能帮忙,我会很高兴

package.json: package.json:

{
  "name": "nodejs_mariadb_docker_test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node src/index.js",
    "dev": "nodemon -L src/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.2",
    "mariadb": "^2.5.5",
    "nodemon": "^2.0.15"
  }
}

Dockerfile for nodejs:用于nodejs的Dockerfile:

# Specifies the image of your engine
FROM node:16.13.2

# The working directory inside your container
WORKDIR /app

# Get the package.json first to install dependencies
COPY package.json /app

# This will install those dependencies
RUN npm install

# Copy the rest of the app to the working directory
COPY . /app

# Run the container
CMD ["npm", "run", "dev"]

and the docker compose file:和 docker 撰写文件:

version: "3"
services: 
  node:
    build: .
    container_name: express-api
    ports:
      - "80:8000"
    depends_on: 
      - mysql

  mysql:
    image: mariadb:latest
    ports:
      - "3306:3306"
    environment: 
      MYSQL_ROOT_PASSWORD: "password"
    volumes:
      - mysqldata:/var/lib/mysql
      - ./mysql-dump:/docker-entrypoint-initdb.d
volumes:
    mysqldata: {}

Having the same Dockerfile be used for development and for a final self-contained image is hard.将相同的 Dockerfile 用于开发和最终的独立映像很难。 During development you want the container to react to changes you make on the host file system.在开发过程中,您希望容器对您在主机文件系统上所做的更改做出反应。 For the final image you want it to be self-contained and have the code inside the image.对于最终图像,您希望它是独立的并且在图像中包含代码。

A thing you can do is to have a Dockerfile with 2 different ways to build the image and then use the target specification on the build in your docker-compose file to point to the kind of image you want.您可以做的一件事是使用 Dockerfile 以 2 种不同的方式构建映像,然后使用 docker-compose 文件中构建的target规范来指向您想要的图像类型。

If you change your Dockerfile to have both kinds of images like this如果您将 Dockerfile 更改为具有这样的两种图像

# Specifies the image of your engine. Label it as 'development' for use in the docker-compose file
FROM node:16.13.2 as development
# The working directory inside your container
WORKDIR /app
# When starting in development, we install packages and then run nodemon
RUN ["/bin/sh", "-c", "npm install && npm run dev"]

# This is the part of the Dockerfile that's used when we build the final image
# Specifies the image of your engine
FROM node:16.13.2 as final
# The working directory inside your container
WORKDIR /app
# Get the package.json first to install dependencies
COPY package.json /app
# This will install those dependencies
RUN npm install
# Copy the rest of the app to the working directory
COPY . /app
# Run the container
CMD ["npm", "run", "start"]

Then you can change your docker-compose file to map the current directory to /app and to ask for the 'development' version of the image like this然后您可以将您的 docker-compose 文件更改为 map 当前目录到 /app 并要求像这样的图像的“开发”版本

version: "3"
services: 
  node:
    build: 
      context: .
      target: development
    container_name: express-api
    volumes:
      - .:/app
    ports:
      - "80:8000"
    depends_on: 
      - mysql

  mysql:
    image: mariadb:latest
    ports:
      - "3306:3306"
    environment: 
      MYSQL_ROOT_PASSWORD: "password"
    volumes:
      - mysqldata:/var/lib/mysql
      - ./mysql-dump:/docker-entrypoint-initdb.d
volumes:
    mysqldata: {}

Now you should be able to change files on the host file system and have the container pick them up.现在您应该能够更改主机文件系统上的文件并让容器拾取它们。

When you're done and you want to create the final, self-contained image, you change the docker-compose file to the same as it is now (I've only shown the part of the file for the node image. The database part is the same as before).当你完成并想要创建最终的、独立的图像时,将 docker-compose 文件更改为与现在相同的文件(我只显示了节点图像的文件部分。数据库部分是和以前一样)。

version: "3"
services: 
  node:
    build: .
    container_name: express-api
    ports:
      - "80:8000"
    depends_on: 
      - mysql

Now there's no 'target', so docker-compose will run through the whole Dockerfile and build the 'final' version of the image.现在没有“目标”,因此 docker-compose 将贯穿整个 Dockerfile 并构建图像的“最终”版本。 Everything in the 'development' part of the Dockerfile will be run when you build the image, but it won't affect the final image at all. Dockerfile 的“开发”部分中的所有内容都将在您构建映像时运行,但它根本不会影响最终映像。

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

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