【问题标题】:How do I create a dockerized development environment for ReactJS如何为 ReactJS 创建一个 dockerized 开发环境
【发布时间】:2019-09-25 08:49:23
【问题描述】:

我已经通过包含实时服务器开发了一个反应应用程序并部署了它。作为代理服务器,我使用 nginx,它还为后端和前端提供一些静态和媒体文件。

特别是为了测试静态和媒体文件的服务,我希望有一个 dockerized 本地测试环境。

这是我的问题: 在我的测试环境中,我不想每次更改代码时都创建一个新版本(npm run build - 用于 create-react-app)。理想情况下,我将能够通过绑定安装进行热重载。

这意味着我必须通过 nginx 为开发服务器提供服务。这有可能吗?我为这个问题苦苦挣扎了好几天,寻找一种传统的方法来解决这个问题。

【问题讨论】:

    标签: reactjs docker nginx


    【解决方案1】:

    其实还不错,只需使用这样的 nginx 配置:

    events {}
    
    # assuming you want to serve your app on localhost:8080
    # and assuming your webpack dev server runs on port 3000
    http {
        include /etc/nginx/mime.types;
        server {
            # assuming you want to serve the app on localhost:8080
            listen 8080;
            client_max_body_size 50m;
    
            # webpack dev server
            location / {
                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $host;
                # use your port for your webpack dev server
                proxy_pass http://host.docker.internal:3000/;
            }
    
            # this is specifically needed for hot reload with webpack dev server
            location /sockjs-node {
                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $host;
    
                # 'host.docker.internal' is a docker dns record for your host machine's localhost,
                # and '3000' should be the port of your webpack dev server
                proxy_pass http://host.docker.internal:3000;
    
                proxy_redirect off;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
            }
       }
    
        access_log /etc/nginx/access.log;
        error_log /etc/nginx/error.log debug;
    }
    
    

    并使用如下所示的./nginx.sh 运行 nginx:

    #!/usr/bin/env bash
    
    # path to your app's build directory
    APP_BUILD_DIR=/path/to/my-app/build
    
    # path of dir containing your nginx config
    NGINX_CONF_DIR=$(pwd)
    
    # name of your nginx conf file, relative to NGINX_CONF_DIR
    NGINX_CONF_FILE=nginx.conf
    
    # port at which nginx is serving your app
    PORT=8080
    
    # docker run docs: https://docs.docker.com/engine/reference/run/
    # this will run an nginx container named 'nginx' as a daemon,
    # and will mount NGINX_CONF_DIR in the container
    docker run -d --name nginx \
      -v=$NGINX_CONF_DIR:/etc/nginx \
      -v=$APP_BUILD_DIR:/opt/base/my-app \
      -p=$PORT:$PORT nginx \
      nginx -c /etc/nginx/$NGINX_CONF_FILE -g "daemon off;"
    
    

    该目录中还有一个类似于this one 的mime.types 文件。

    所以你的目录结构应该是这样的:

    dir/
    -- nginx.sh
    -- nginx.conf
    -- mime.types
    -- ...
    

    我假设你在你的机器上运行你的 webpack 开发服务器,而不是在 docker 容器中?以我的经验,我会诚实地推荐。 npm(或yarn)做得很好,以至于我发现不需要在本地容器中运行我的 React 应用程序,即使我通过 nginx 容器为它们提供服务。

    奖励:如果你想运行相同的应用程序,这里有一个 nginx 配置,但提供静态包而不是 webpack 开发服务器:

    events {}
    
    http {
        include /etc/nginx/mime.types;
        server {
            # assuming you want to serve the app on localhost:8080
            listen 8080;
            client_max_body_size 50m;
    
            location / {
                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $host;
                root /opt/base/my-app;
                try_files $uri /index.html;
            }
    
            # matches things like http://localhost:8080/build/static/js/2.d63c51de.chunk.js
            location /build/static/ {
                alias /opt/base/my-app/static/;
                try_files $uri $uri/;
            }
       }
    
        access_log /etc/nginx/access.log;
        error_log /etc/nginx/error.log debug;
    }
    

    参考:this SO post

    【讨论】:

    • 嘿,非常感谢您抽出宝贵时间!我将对此进行测试并在我工作后立即接受答案:) 谢谢。现在,通过在容器中运行开发服务器,我有了一个可行的解决方案。这里的优点是,我可以使用 docker dns 并将开发服务器定义为 nginx 中的上游服务器。按照您的建议,不在容器中运行节点服务器有什么好处吗?
    • 我喜欢在容器外部本地构建前端。这似乎更容易。对于需要与其他服务(如数据库)通信的节点服务器(除了 webpack 开发服务器),我可以看到使用 docker 运行它的优势,因为它的网络优势。 docker的美妙之处在于它真的取决于你。但是你会注意到proxy_pass http://host.docker.internal:3000;? host.docker.internal 位是您真正的 localhost 的 docker dns 记录。我希望这可以为您解决一些问题。
    【解决方案2】:

    使用 HMR 对开发服务器进行 Docker 化并非易事。

    即使是预配置的项目(使用 HMR 和 docker-ready),如果不进行额外调整,一开始也可能无法运行。 F.e. HMR 需要 localhost 路径,而您可以让应用程序在另一个 IP/端口上运行(CORS 问题)。在构建过程中,一些调整可能是动态的硬补丁包。

    在尝试设置您的梦想配置之前尝试运行准备好的项目。您始终可以为不同的部分(在不同的端口上)运行不同的服务器。

    我将从 apollo-universal-starter-kit 开始 - 它包含 node.js api/backend 部分,但可以与外部部分一起使用(可配置)。

    您可以在 Docker Hub 上搜索其他 react-HMR-ready 解决方案。尝试,获得灵感和知识……玩得开心。

    【讨论】:

      【解决方案3】:

      对于从本地文件系统热部署到 docker,请指定以下选项。以下是使用 powershell 在 Windows 10 上运行的 docker:

      Set the environment variable
      -e CHOKIDAR_USEPOLLING=true
      
      Mount the volume of local machine into work directory in the container
      -v ${pwd}:/<workdir in container>
       
      E.g. : docker run -it --rm -p 5000:3000 -v /app/node_modules -v ${pwd}:/app -e CHOKIDAR_USEPOLLING=true <image id>
      

      【讨论】:

        猜你喜欢
        • 2011-10-24
        • 2011-07-26
        • 2015-06-27
        • 2022-09-25
        • 2019-05-03
        • 2020-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多