[英]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.js和Node.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.