错误发生在使用Docker Compose运行TypeScript React应用程序时。

huangapple go评论73阅读模式
英文:

Error when running a typescript react app with docker compose

问题

Sure, here's the translated content you requested:

我想运行一个带有Node.js后端和create-react-app前端的TypeScript monorepo。
首先,我尝试在容器中运行前端。应用程序在本地正常运行。

我想在主机上更改代码,并使容器中的代码自动更新。

当我运行 docker-compose up -d 时,我看到下面的错误。


> frontend@0.1.0 start
> react-scripts start

无法找到所需的文件。
  名称: index.js
  查找位置: /app/src   
``` 文件夹结构

repo

  • backend
  • frontend
    • Dockerfile
    • build
    • public
    • src
      • index.tsx
  • docker-compose.yaml

FROM node
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

version: "3.8"
services:
frontend:
build: ./frontend
ports:
- '3000:3000'
volumes:
- './frontend/src:/app/src'
stdin_open: true
tty: true
volumes:
data:
logs:

version: "3.8"
services:
frontend:
build: ./frontend
ports:
- '3000:3000'
volumes:
- './frontend:/app'
- /app/node_modules
stdin_open: true
tty: true
``` 运行 docker-compose up -d 后,容器日志显示以下错误

ENOENT: 找不到文件或目录,打开 'app/package.json'
``` 注意: 奇怪的是,如果在Dockerfile中删除工作目录,则容器会运行,但会将所有内容安装在顶级目录而不是/app目录中。当我完全删除卷时,容器也正常运行(但仍然是一个不可接受的问题)。

编辑2: 我觉得问题与项目文件夹结构和路径有关。如果我将docker-compose文件放在`frontend`目录中,容器会提供React应用程序,并且本地代码更改会自动在容器中更新。

<details>
<summary>英文:</summary>

I want to run a typescript monorepo with a nodejs backend and a create-react-app frontend.
First i am trying to get the frontend running in a container. The application runs locally fine.

I want to change code in the host machine and have the code in the container update automatically.

When I run `docker-compose up -d` I see the error below.

> frontend@0.1.0 start
> react-scripts start

Could not find a required file.
Name: index.js
Searched in: /app/src

folder structure

repo

  • backend
  • frontend
    • Dockerfile
    • build
    • public
    • src
      • index.tsx
  • docker-compose.yaml

DockerFile

FROM node
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

docker-compose.yaml

version: "3.8"
services:
frontend:
builds: ./frontend
ports:
- '3000:3000'
volumes:
- './frontend/src:/app/src'
stdin_open: true
tty: true
volumes:
data:
logs:


##### edit1: docker-compose.yaml after trying suggested solution

version: "3.8"
services:
frontend:
builds: ./frontend
ports:
- '3000:3000'
volumes:
- './frontend:/app'
- /app/node_modules
stdin_open: true
tty: true

error after running `docker-compose up -d` the container logs show

ENOENT: no such file or directory, open 'app/package.json'


note:  Strangely, removing the workdir in the Dockerfile runs the container but installs everything in the top level and not in the /app directory. When I remove volumes entirely the container runs fine as well (still a deal breaker).

##### edit2: I feel the issue has something to do with the project folder structure and paths. If i place the docker-compose file in the `frontend` directory the container serves the react app and local code changes auto update in the container.

</details>


# 答案1
**得分**: 3

假设您的 `tsconfig.json` 文件位于 `frontend` 文件夹中,它在容器中不存在,因此将缺少关键部分,如

```lang-json
"compilerOptions": {
  "jsx": "react-jsx"
}

更典型的方法是挂载整个应用程序目录,并创建匿名卷以避免覆盖 node_modules

volumes:
  - ./frontend:/app
  - /app/node_modules # 匿名卷挂载
英文:

Assuming your tsconfig.json file is in frontend, it won't be present in your container so you'll be missing crucial parts like

&quot;compilerOptions&quot;: {
  &quot;jsx&quot;: &quot;react-jsx&quot;
}

A more typical approach is to volume mount the entire application directory and create an anonymous volume mount so as to not clobber node_modules

volumes:
  - ./frontend:/app
  - /app/node_modules # anonymous volume mount

huangapple
  • 本文由 发表于 2023年5月10日 11:25:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定