node.js - 在react和express项目中使用docker compose up后出错

标签 node.js reactjs docker

这是我的第一个 docker 项目,我在 Medium 上阅读了几篇关于如何将 docker 与现有的 React + Express Node js 项目结合使用的文章。

需要注意的是,我不想在项目的 react (客户端)端使用构建命令。

如何解决此错误并正确运行该项目?

提前致谢...

当我使用命令 docker-compose up 时抛出此错误:-

Creating waha_api_1 ... done 
Creating waha_client_1 ... done 
Attaching to waha_api_1, waha_client_1 api_1   
  | npm ERR! code ENOENT api_1  
  | npm ERR! syscall open api_1   
  | npm ERR! path /api/package.json api_1   
  | npm ERR! errno -2 api_1   
  | npm ERR! enoent ENOENT: no such file or directory, open '/api/package.json' api_1   
  | npm ERR! enoent This is related to npm not being able to find a file. api_1   
  | npm ERR! enoent api_1   
  | api_1   
  | npm ERR! A complete log of this run can be found in: api_1   
  | npm ERR!     /root/.npm/_logs/2021-05-08T12_46_07_679Z-debug.log client_1
  | npm ERR! code ENOENT client_1
  | npm ERR! syscall open client_1
  | npm ERR! path /client/package.json client_1
  | npm ERR! errno -2 client_1
  | npm ERR! enoent ENOENT: no such file or directory, open '/client/package.json' client_1
  | npm ERR! enoent This is related to npm not being able to find a file. client_1
  | npm ERR! enoent client_1
  | client_1
  | npm ERR! A complete log of this run can be found in: client_1
  | npm ERR!     /root/.npm/_logs/2021-05-08T12_47_03_120Z-debug.log client_1
  | npm ERR! code ENOENT client_1
  | npm ERR! syscall open client_1
  | npm ERR! path /client/package.json client_1
  | npm ERR! errno -2 client_1
  | npm ERR! enoent ENOENT: no such file or directory, open '/client/package.json' client_1
  | npm ERR! enoent This is related to npm not being able to find a file. client_1
  | npm ERR! enoent client_1
  | client_1
  | npm ERR! A complete log of this run can be found in: client_1
  | npm ERR!     /root/.npm/_logs/2021-05-08T12_47_04_690Z-debug.log waha_client_1 exited with code 254 client_1
  | npm ERR! code ENOENT client_1
  | npm ERR! syscall open client_1
  | npm ERR! path /client/package.json client_1
  | npm ERR! errno -2 client_1
  | npm ERR! enoent ENOENT: no such file or directory, open '/client/package.json' client_1
  | npm ERR! enoent This is related to npm not being able to find a file. client_1
  | npm ERR! enoent client_1
  | client_1
  | npm ERR! A complete log of this run can be found in: client_1
  | npm ERR!     /root/.npm/_logs/2021-05-08T12_47_06_078Z-debug.log waha_client_1 exited with code 254 client_1
  | npm ERR! code ENOENT client_1
  | npm ERR! syscall open client_1
  | npm ERR! path /client/package.json client_1
  | npm ERR! errno -2 client_1
  | npm ERR! enoent ENOENT: no such file or directory, open '/client/package.json' client_1
  | npm ERR! enoent This is related to npm not being able to find a file. client_1
  | npm ERR! enoent client_1
  | client_1
  | npm ERR! A complete log of this run can be found in: client_1
  | npm ERR!     /root/.npm/_logs/2021-05-08T12_47_07_881Z-debug.log waha_client_1 exited with code 254 client_1
  | npm ERR! code ENOENT client_1
  | npm ERR! syscall open client_1
  | npm ERR! path /client/package.json client_1
  | npm ERR! errno -2 client_1
  | npm ERR! enoent ENOENT: no such file or directory, open '/client/package.json' client_1
  | npm ERR! enoent This is related to npm not being able to find a file. client_1
  | npm ERR! enoent client_1
  | client_1
  | npm ERR! A complete log of this run can be found in: client_1
  | npm ERR!     /root/.npm/_logs/2021-05-08T12_47_09_661Z-debug.log waha_client_1 exited with code 254 client_1
  | npm ERR! code ENOENT client_1
  | npm ERR! syscall open client_1
  | npm ERR! path /client/package.json client_1
  | npm ERR! errno -2 client_1
  | npm ERR! enoent ENOENT: no such file or directory, open '/client/package.json' client_1
  | npm ERR! enoent This is related to npm not being able to find a file. client_1
  | npm ERR! enoent client_1
  | client_1
  | npm ERR! A complete log of this run can be found in: client_1
  | npm ERR!     /root/.npm/_logs/2021-05-08T12_47_12_248Z-debug.log waha_client_1 exited with code 254 client_1
  | npm ERR! code ENOENT client_1
  | npm ERR! path /client/package.json client_1
  | npm ERR! errno -2 client_1
  | npm ERR! enoent ENOENT: no such file or directory, open '/client/package.json' client_1
  | npm ERR! enoent This is related to npm not being able to find a file. client_1
  | npm ERR! enoent client_1
  | client_1
  | npm ERR! A complete log of this run can be found in: client_1
  | npm ERR!     /root/.npm/_logs/2021-05-08T12_47_16_386Z-debug.log waha_client_1 exited with code 254 waha_api_1 exited with code 254

api dockerfile:-

# Use a lighter version of Node as a parent image
FROM node:12-alpine

# Set the working directory to /api
WORKDIR /api

# copy package.json into the container at /api
COPY package.json ./api
COPY package-lock.json ./api

# install dependencies
RUN npm install

# Copy the current directory contents into the container at /api
COPY . /api/

# Make port 80 available to the world outside this container
EXPOSE 80

# Run the app when the container launches
CMD ["npm", "start"]

客户端dockerfile:-

# Use a lighter version of Node as a parent image
FROM node:12-alpine

# Set the working directory to /client
WORKDIR /client

# copy package.json into the container at /client
COPY package.json ./client
COPY package-lock.json ./client

# install dependencies
RUN npm install

# Copy the current directory contents into the container at /client
COPY . /client/

# Make port 3000 available to the world outside this container
EXPOSE 3000

# Run the app when the container launches
CMD ["npm", "start"]

docker-compose.yml:-

version: "3"

services:
    client:
        build:
            context: ./client
            dockerfile: ./Dockerfile
        image: client
        restart: always
        ports:
            - "3000:3000"
        volumes:
            - ./client:/client
            - /client/node_modules
        links:
            - api
        networks:
            - webappnetwork

    api:
        build:
            context: ./api
            dockerfile: ./Dockerfile
        image: api
        restart: always
        ports:
            - "9000:9000"
        volumes:
            - ./api:/api
            - /api/node_modules
        networks:
            - webappnetwork

networks:
    webappnetwork:
        driver: bridge

我的项目结构:-

enter image description here

最佳答案

如果您在 Dockerfile 中设置了 WORKDIR,则会将其视为当前的 pwd 就是该位置。因此,您实际上在 COPY 步骤中执行的操作是将内容分别复制到 /api/api/client/client 。有关该主题的更多信息:What is the point of WORKDIR on Dockerfile?

请参阅此处查看更正后的 Dockerfile 示例:

FROM node:12-alpine

WORKDIR /api

# Only copy to the WORKDIR (with .)
COPY package.json .
COPY package-lock.json .

RUN npm install

# Same here
COPY . .

EXPOSE 80

CMD ["npm", "start"]

另请记住,如果您的 React 应用程序是用 TypeScript 编写的,则应该有一个额外的步骤,将 TS 文件编译为 JS。

我有一个 Discord-bot Docker 容器(Typescript)的示例 HERE (请记住,这使用 .dockerignore 文件,这就是为什么我只使用 COPY . .)。它可能并不完美,但它可以完成工作。

对 Docker-compose 文件无法提供太多帮助,但有几点:

  • dockerfile: ./Dockerfile 可能是多余的,因为“Dockerfile”应该是默认的
  • 删除两者上的图像,因为它们应该链接到注册表中的图像。您可以指定 imagebuild
  • 删除链接,因为它是旧标志,不需要 ( source )
  • 省略绑定(bind),它们不是必需的,并且破坏了使用 Docker 的意义
    • 构建应用程序运行所需的文件后,应该已位于容器中。您所做的只是覆盖文件并可能破坏容器中的状态。

有关卷安装的更多说明:

卷挂载是从容器到主机系统(或虚拟卷)的挂载,它们在运行时绑定(bind),而不是在构建时绑定(bind)。

您通常只对需要在容器生命周期之外保留的数据使用卷挂载。含义 - 如果容器死亡,您仍然拥有数据。

例如,您可能有一个数据库服务器,因此您希望使用卷挂载将数据保留在容器外部,因为您不希望在容器崩溃时丢失所有数据,并且必须旋转上一个新的。当您使用卷挂载时,您只需将其再次挂载到新容器上,所有数据都会在那里。

您不装载应用程序数据,例如容器中的源代码。它被构建一次,然后存储在容器内。无需挂载数据。

因此,只需删除以下卷、链接和图像,直到它如下所示:

version: "3"

services:
    client:
        build:
            context: ./client
        restart: always
        ports:
            - "3000:3000"
        networks:
            - webappnetwork

    api:
        build:
            context: ./api
        restart: always
        ports:
            - "9000:9000"
        networks:
            - webappnetwork

networks:
    webappnetwork:
        driver: bridge

关于node.js - 在react和express项目中使用docker compose up后出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67452703/

相关文章:

angular - Docker 中的 Protractor 测试 - 在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时内未调用异步回调

javascript - 从所需模块构造新对象的模式或最佳方法?

node.js - 使用 NodeJS 的 AWS Lambda 函数中的 AWS-SDK 加载错误

javascript - 有没有更简单的方法来链接 MongoDB 结果的 promise ?

angularjs - 使用 Grunt Connect 时无法获取/获取

javascript - 如何在全新的空白页面中呈现 React Route 组件

javascript - 传递参数时 React Router 不起作用

reactjs - 内存动态样式组件会导致 "Rendered fewer hooks than expected error."

bash - 具有用于执行bash脚本的入口点的Dockerfile

spring - InfluxDB docker镜像不适用于我的Spring Boot应用程序镜像