Node.js 在没有docker-compose.yml文件的单个docker文件中运行react和node

Node.js 在没有docker-compose.yml文件的单个docker文件中运行react和node,node.js,reactjs,docker,Node.js,Reactjs,Docker,我有一个使用reactjs的示例项目,下面是文件夹结构 movielisting Dockerfile client package.json package.lock.json ... other create-react-app folders like src.. server index.js 我通过npm运行start-client文件夹和nodemon index.js-server文件夹启动这个项目。我所有的api都写在服

我有一个使用reactjs的示例项目,下面是文件夹结构

movielisting
   Dockerfile
   client
     package.json
     package.lock.json
     ... other create-react-app folders like src..
   server
     index.js
我通过npm运行start-client文件夹和nodemon index.js-server文件夹启动这个项目。我所有的api都写在服务器文件夹中。我的客户端运行在端口3000,服务器运行在端口4000,我在客户端的package.json中添加了代理,如下所示

"proxy": "http://localhost:4000"
所以,我试图在Dockerfile中实现的是,我希望通过运行这个Dockerfile来启动应用程序

1) i need to create the build of client by running npm run build
2) copy to the workdir inside the container
3) copy all server folder
4) npm install for the server folder
5) proxy the application
我怎么做这个?我是否需要在nodejs中编写一些代码来提供buildindex.html文件

另外,如何运行Dockerfile命令来运行应用程序


感谢任何帮助

以便将前端和后端构建为单个映像。您需要执行以下操作:

FROM node:10.15.1-alpine

COPY . /home/movielisting
#Prepare the client
WORKDIR /home/movielisting/client
RUN npm install --no-cache && npm run build && npm install -g serve

#Prepare the server
WORKDIR /home/movielisting/server
RUN npm install --no-cache

EXPOSE 4000 

CMD [ "npm", "start", "index.js" ]
对于前端应用程序,需要将其构建为静态文件

对于后端应用程序,它需要在容器内运行并公开,以便前端可以通过浏览器访问它,因为使用
localhost:4000
将最终调用用户的localhost,而不是容器的localhost

最后,为了在单个容器中运行多个服务,您需要使用类似supervisor的工具作为服务管理器。您可能需要检查

例如,您可以检查以下内容:

FROM node:10.15.1-alpine

COPY . /home/movielisting
#Prepare the client
WORKDIR /home/movielisting/client
RUN npm install --no-cache && npm run build && npm install -g serve

#Prepare the server
WORKDIR /home/movielisting/server
RUN npm install --no-cache

EXPOSE 4000 

CMD [ "npm", "start", "index.js" ]
您可能需要检查以下链接:


@DILEEPTHOMAS我已经更新了我的答案,请检查一下