docker-compose 不能 运行 在 node_modules 已经存在的情况下做出反应

docker-compose cant run react without node_modules already existing

我正在使用 docker/docker-compose 启动我的 React (ts) 应用程序并 运行ning。 我当前使用的脚本在 1 个条件下完美运行。我已经在目录中做了 'npm install',而不是在 docker.

我希望能够从 github 克隆我的项目,然后 docker-compose up,它比

现在我首先必须 运行 'npm install',然后 'docker-compose up' 才能让它正常工作。

我试过只使用 RUN npm install react-scripts -g,这有点管用。但是我得到了打字稿和所有其他包的错误。

我想要发生的是。当我克隆我的存储库并使用 docker-compose up 时。我的整个项目 运行s。它还应该在我的反应文件夹中创建一个 node_modules 文件夹,我可以在我的 IDE 中看到它。这是为了让我的 IDE 知道包裹中的代码并且不会一直对我大喊大叫。

我不知道如何让它工作,我已经为此苦苦挣扎了几个小时,在网上找不到如何修复它。希望有人能帮助我 :D

我的结构看起来像这样:

Docker 文件:

FROM node:16.14.2

WORKDIR /usr/src/app

COPY ./package*.json ./

RUN npm install

CMD npm start --host 0.0.0.0 --port 3000 --disableHostCheck true

docker-compose.yml:

version: '3'

services:
    frontend:
        build:
            context: ./apps/frontend
            dockerfile: ./Dockerfile
        volumes:
            - ./apps/frontend:/usr/src/app
        ports:
            - 3000:3000
        environment:
            - CHOKIDAR_USEPOLLING=true
            - API_BASE_URL=host.docker.internal:8080/api
        extra_hosts:
            - "host.docker.internal:host-gateway"

我得到的错误是:

frontend_1  | > spa@0.1.0 start
frontend_1  | > react-scripts start "0.0.0.0" "3000"
frontend_1  | 
frontend_1  | sh: 1: react-scripts: not found
cyldiscordbot_frontend_1 exited with code 127

package.json(如果您需要,请提供,但在这里):

{
  "name": "spa",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@cylbot/cyldiscordbotlanguage": "^2.0.3",
    "@emotion/core": "^11.0.0",
    "@emotion/react": "^11.7.1",
    "@emotion/styled": "^11.6.0",
    "@mui/icons-material": "^5.3.1",
    "@mui/material": "^5.5.0",
    "@testing-library/user-event": "^13.2.1",
    "@types/node": "^17.0.9",
    "@types/react": "^17.0.38",
    "@types/react-dom": "^17.0.11",
    "axios": "^0.25.0",
    "emotion-theming": "^11.0.0",
    "enzyme": "^3.11.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.6",
    "react-router-dom": "^6.2.1",
    "react-scripts": "5.0.0",
    "redux": "^4.1.2",
    "redux-saga": "^1.1.3",
    "redux-thunk": "^2.4.1",
    "styled-components": "^5.3.3",
    "typescript": "^4.5.4",
    "web-vitals": "^2.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:coverage": "jest --coverage",
    "storybook": "start-storybook -p 6006 -s public",
    "build-storybook": "build-storybook -s public"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ],
    "overrides": [
      {
        "files": [
          "**/*.stories.*"
        ],
        "rules": {
          "import/no-anonymous-default-export": "off"
        }
      }
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@material-ui/core": "^4.12.3",
    "@storybook/addon-actions": "^6.4.19",
    "@storybook/addon-essentials": "^6.4.19",
    "@storybook/addon-interactions": "^6.4.19",
    "@storybook/addon-links": "^6.4.19",
    "@storybook/builder-webpack5": "^6.4.19",
    "@storybook/manager-webpack5": "^6.4.19",
    "@storybook/node-logger": "^6.4.19",
    "@storybook/preset-create-react-app": "^4.0.1",
    "@storybook/react": "^6.4.19",
    "@storybook/testing-library": "^0.0.9",
    "@testing-library/jest-dom": "^5.16.2",
    "@testing-library/react": "^12.1.4",
    "@testing-library/react-hooks": "^7.0.2",
    "@types/jest": "^27.4.1",
    "@types/styled-components": "^5.1.20",
    "jest": "^27.5.1",
    "react-test-renderer": "^17.0.2",
    "ts-jest": "^27.1.3",
    "webpack": "^5.70.0"
  }
}

我可以提供很多关于这个项目的信息,所以如果需要更多信息,请尽管询问! :D

我明白了。我问了一个朋友,他帮我解决了。 我需要将 Dockerfile 更改为:

FROM node:16.14.2

WORKDIR /usr/src/app

RUN npm install -g react-scripts

RUN chown -Rh node:node /usr/src/app

USER node

EXPOSE 3000

CMD [ "sh", "-c", "npm install && npm run start" ]

您的 Dockerfile 缺少应用程序代码;它只复制 package.json 文件。这给你一个不完整的图像,你不能只是 运行。您漏掉了一行:

COPY ./ ./

我会把它放在 RUN npm install 行之后,以便更快地重建图像。

由于您的映像不完整,在您当前的设置中,您必须从主机注入代码,这会隐藏映像构建的 node_modules 目录。这就是为什么您还必须 运行 npm install 在主机上。如果您 COPY 将应用程序代码放入图像中,则不需要 volumes: 块。

version: '3.8'
services:
    frontend:
        build: ./apps/frontend  # short form with default dockerfile:
        ports:
            - 3000:3000
        environment:
            - API_BASE_URL=host.docker.internal:8080/api
        extra_hosts:
            - "host.docker.internal:host-gateway"
        # no volumes: or polling environment variable