【问题标题】:running react and node in a single docker file without docker-compose.yml file在没有 docker-compose.yml 文件的单个 docker 文件中运行 react 和 node
【发布时间】:2019-07-20 00:47:20
【问题描述】:

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

movielisting
   Dockerfile
   client
     package.json
     package.lock.json
     ... other create-react-app folders like src..
   server
     index.js

我通过 npm run start - 客户端文件夹和 nodemon index.js - 服务器文件夹启动这个项目。我所有的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 中编写一些代码来为构建 index.html 文件提供服务

还有如何运行 Dockerfile 命令来运行应用程序。

任何帮助表示赞赏!!!

【问题讨论】:

    标签: node.js reactjs docker


    【解决方案1】:

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

    对于前端应用程序,需要构建它以便将其作为静态文件提供服务

    对于后端应用程序,它需要在容器内运行并公开暴露,以便前端可以从浏览器访问它,因为使用 localhost:4000 最终会调用用户的本地主机而不是容器的本地主机

    最后,您需要使用像 supervisor 这样的东西作为服务管理器,以便在单个容器中运行多个服务。您可能需要检查following

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

    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 我已经更新了我的答案,请检查一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-08
    • 2023-01-07
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    相关资源
    最近更新 更多