简体   繁体   English

无法使用 docker-compose 运行 React 和 Node 应用程序

[英]Unable to run React and Node application with docker-compose up

I am trying to run React.js and Node.js ( Express.js ) application into docker container.我正在尝试将React.jsNode.js ( Express.js ) 应用程序运行到 docker 容器中。 So far I have create a Dockerfile for React application and another Dockerfile for Node.js application.到目前为止,我已经为 React 应用程序创建了一个Dockerfile ,为Node.js应用程序创建了另一个Dockerfile

Dockerfile(React app) Dockerfile(反应应用程序)

FROM alpine:3.11

RUN apk add --update nodejs npm

RUN mkdir /client
WORKDIR /client

RUN npm install
COPY . /client

RUN npm install --global http-server

CMD [ "http-server"]

Dockerfile(Node app) Dockerfile(节点应用程序)

FROM alpine:3.11

RUN apk add --update nodejs npm

RUN mkdir /server
WORKDIR /server

COPY ./package.json /package.json
COPY ./package-lock.json /package-lock.json

RUN npm install

COPY . /server

CMD [ "node", "server.js"]

I have also created a docker-compose like this -我还像这样创建了一个docker-compose -

version: '3'

services:
    server:
        build: ./backend
        expose: 
            - 8080
        environment: 
            API_HOST: "http://localhost:8080"
            APP_SERVER_PORT: 8080
        ports: 
            - 8080:8080
        volumes: 
            - ./client
        command: node server.js
    client:
        build: ./build
        environment: 
            -REACT_APP_PORT: 8081
        expose: 
            - 8081
        ports: 
            - 8081:8081
        volumes: 
            - ./server
        links: 
            - server
        command: http-server

I don't understand what is wrong with my configuration when I run docker-compose up it throws an error as show below -当我运行docker-compose时,我不明白我的配置有什么问题,它会抛出一个错误,如下所示 -

Error: Error loading shared library /server/node_modules/hummus/binding/hummus.node: Exec format error
server_1  |     at Object.Module._extensions..node (internal/modules/cjs/loader.js:1021:18)
server_1  |     at Module.load (internal/modules/cjs/loader.js:811:32)
server_1  |     at Function.Module._load (internal/modules/cjs/loader.js:723:14)
server_1  |     at Module.require (internal/modules/cjs/loader.js:848:19)
server_1  |     at require (internal/modules/cjs/helpers.js:74:18)
server_1  |     at Object.<anonymous> (/server/node_modules/hummus/hummus.js:5:31)
server_1  |     at Module._compile (internal/modules/cjs/loader.js:955:30)
server_1  |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
server_1  |     at Module.load (internal/modules/cjs/loader.js:811:32)
server_1  |     at Function.Module._load (internal/modules/cjs/loader.js:723:14)
pdf_spliter_server_1 exited with code 1
client_1  | Starting up http-server, serving ./
client_1  | Available on:
client_1  |   http://127.0.0.1:8080
client_1  |   http://172.26.0.3:8080
client_1  | Hit CTRL-C to stop the server

When you have a volumes: declaration that names a host path, the entire contents of that host path replace whatever was built into the image.当您有一个volumes:命名主机路径的声明时,该主机路径的全部内容将替换映像中内置的任何内容。 You mention in a comment that you're on MacOS;您在评论中提到您使用的是 MacOS; in this environment, Docker runs in a hidden Linux VM, so you're mounting a MacOS node_modules directory to run in a Linux environment, which leads to the error you're getting.在此环境中,Docker 在隐藏的 Linux VM 中运行,因此您正在安装 MacOS node_modules目录以在 Linux 环境中运行,这会导致您遇到错误。

The standard Docker model is that an image contains an entire packaged application and all of its dependencies.标准的 Docker model 是一个镜像包含整个打包的应用程序及其所有依赖项。 You should be able to independently run an image without separately needing the application source code.您应该能够独立运行图像,而无需单独需要应用程序源代码。 Your Dockerfiles have lines like COPY. /server你的 Dockerfiles 有像COPY. /server COPY. /server which in fact copy the entire application tree in. COPY. /server实际上将整个应用程序树复制到其中。

This means that you can delete the volumes: lines from your docker-compose.yml file to use the code built into the image.这意味着您可以从docker-compose.yml文件中删除volumes:行以使用图像中内置的代码。 You can also remove a number of other options ( links: and expose: aren't used in modern Docker; command: just duplicates a Dockerfile setting).您还可以删除许多其他选项( links:expose:在现代 Docker 中未使用; command:仅复制 Dockerfile 设置)。 That would leave you with那会给你留下

version: '3'
services:
    server:
        build: ./backend
        environment: 
            API_HOST: "http://localhost:8080"
            APP_SERVER_PORT: 8080
        ports: 
            - 8080:8080
    client:
        build: ./build
        ports: 
            - 8081:3000

You also need to make sure you have a .dockerignore file that excludes node_modules from the Docker build context.您还需要确保您有一个从 Docker 构建上下文中排除node_modules.dockerignore文件 This will avoid overwriting the results of the RUN npm install step and reduce the overhead of running docker build .这将避免覆盖RUN npm install步骤的结果并减少运行docker build的开销。

暂无
暂无

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

相关问题 Docker + NodeJS:docker-compose up 和 docker-compose run 之间的 node_modules 不一样 - Docker + NodeJS: node_modules not the same between docker-compose up and docker-compose run Docker/Reactjs/Nodejs - 如何在 docker-compose up 后安装依赖项并运行应用程序 - Docker/Reactjs/Nodejs - How to install dependencies and run application after docker-compose up Docker 映像与 docker run 一起使用,但不与 docker-compose up 一起使用 - Docker image working with docker run but not with docker-compose up Node.JS:由于 node-sass 和 node-gyp,尝试使用 docker-compose 向上退出运行 docker 项目 - Node.JS: Trying to run docker project with docker-compose up exits due to node-sass and node-gyp docker-compose 无法在没有 node_modules 的情况下运行反应 - docker-compose cant run react without node_modules already existing 使用docker-compose run时无法调试,但是docker-compose up可以工作 - Can't debug when using `docker-compose run`, but `docker-compose up` works 使用docker-compose设置带有Redis的节点 - Setting up node with redis using docker-compose 带有 docker-compose 的 Node.js 容器无法运行 - Node.js container with docker-compose can't run 具有docker-compose的节点应用程序中的CRON不触发作业 - CRON in node application with docker-compose not firing jobs 使用docker-compose时,节点应用程序无法连接到Redis - While using docker-compose, node application fails to connect to Redis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM