简体   繁体   English

为 react-app 运行 docker 容器时出现问题,其中 package.json 在子文件夹中

[英]Trouble running a docker container for react-app where package.json is in a subfolder

I am trying to create a dockerfile for a project that has the following folder structure:我正在尝试为具有以下文件夹结构的项目创建一个 dockerfile:

HDWD-project
|_ client
|    |_ package.json
|_ server
     |_ package.json

Client is a react-app and I am just working with this at the moment, before including server which is the backend.客户端是一个反应应用程序,我目前正在使用它,然后包括作为后端的服务器。

I am having real trouble figuring out the logic of the dockerfile and have googled furiouly for the last two days.我在弄清楚 dockerfile 的逻辑时遇到了真正的麻烦,并且在过去的两天里疯狂地用谷歌搜索。 All the examples are too easy.所有的例子都太简单了。

I just can't seem to get react-app to start in the container, and get varying error messages.我似乎无法让 react-app 在容器中启动,并收到各种错误消息。 But I need to know the dockerfile is fine before I proceed.但在我继续之前,我需要知道 dockerfile 没问题。

FROM node:latest
WORKDIR HDWD-project
COPY ./client/package.json .
RUN npm install
COPY . .
RUN cd client
CMD ["npm", "start"]

Going forward I have a script that can start both the server and the client, but I'm just trying to get my head around docker and getting the client frontend to run fine.展望未来,我有一个脚本可以同时启动服务器和客户端,但我只是想让我的头脑围绕 docker 并让客户端前端正常运行。

Would anyone be able to correct me on where the issue in this config is and explain it?任何人都可以纠正我此配置中的问题所在并进行解释吗?

This is a docker file for the frontend( client in your case).这是前端的 docker 文件(在您的情况下为client )。 You can make a dockerfile under your client folder and build the image with docker build -t image-name:tag-name.您可以在您的client文件夹下制作一个dockerfile并使用docker build -t image-name:tag-name.

# Pull the latest node image from dockerhub
FROM node:latest
# Create app directory 
WORKDIR /usr/src/app
# Copy package.json and package-lock.json to the workdir
COPY package*.json ./ 
# Install the dependencies
RUN npm install
# Bundle app source
COPY . .
# Run the app in docker
CMD ["npm", "start"]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM