簡體   English   中英

Docker 即使設置了 -L,Nodemon 也不會在更改時重新加載

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

嗨,我正在嘗試對我目前正在開發的應用程序進行 dockerize。 它使用 nodejs 和 mariadb。 我在弄清楚如何使 nodemon 工作時遇到了一些困難。

我嘗試使用 --legacy-watch 或 -L 這是簡短的形式,但它並沒有改變結果。

NPM 正確安裝了所有依賴項,我什至得到了 nodemon 文本,但是當我進行更改時它不會重新啟動服務器。

如果有人能幫忙,我會很高興

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"
  }
}

用於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"]

和 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: {}

將相同的 Dockerfile 用於開發和最終的獨立映像很難。 在開發過程中,您希望容器對您在主機文件系統上所做的更改做出反應。 對於最終圖像,您希望它是獨立的並且在圖像中包含代碼。

您可以做的一件事是使用 Dockerfile 以 2 種不同的方式構建映像,然后使用 docker-compose 文件中構建的target規范來指向您想要的圖像類型。

如果您將 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"]

然后您可以將您的 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: {}

現在您應該能夠更改主機文件系統上的文件並讓容器拾取它們。

當你完成並想要創建最終的、獨立的圖像時,將 docker-compose 文件更改為與現在相同的文件(我只顯示了節點圖像的文件部分。數據庫部分是和以前一樣)。

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

現在沒有“目標”,因此 docker-compose 將貫穿整個 Dockerfile 並構建圖像的“最終”版本。 Dockerfile 的“開發”部分中的所有內容都將在您構建映像時運行,但它根本不會影響最終映像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM