如何使单个React Docker构建/映像在所有环境中运行? [英] How to make a single React Docker build/image to run through all environments?

查看:268
本文介绍了如何使单个React Docker构建/映像在所有环境中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单个Docker容器中部署React应用程序,以便能够在OpenShift平台上运行dev,preprod和prod,而我只能在该平台上推送标记的docker映像.为此,我有:

i'm trying to deploy a React application in a single Docker container able to run through dev, preprod and prod on an OpenShift platform on which i can only push tagged docker images. In order to do this i have:

  • 使用Github上托管的create-react-app生成的React应用程序
  • 项目根目录下的Dockerfile,其中包含docker构建/运行过程
  • 包含连续集成检查和部署方法的.gitlab-ci.yml文件
  • 可访问的OpenShift平台

我可以做什么:

我可以轻松地生成将在docker映像构建阶段中添加的生产版本'/build',并且可以在生产上下文中运行,或者我可以在运行开始时进行构建(但仅编写它感觉很不好).

I can easily generate a production build '/build' that will be added in the docker image build phase and will run with a production context or i can build at run start (but just writing it feels bad).

问题:

此生成的图像无法在所有环境中运行,并且我不想为每个环境创建特定的版本.

This generated image isn't able to run through all environment, and i don't want to have a specific build for each environment.

我想要什么:

我希望能够生成一个可以在所有环境中运行的docker映像,而无需安装依赖项或仅在需要RUN时进行构建.

I want to be able to generate a single docker image that will run through all environments without having to install dependencies or build when only RUN is needed.

这是我的Dockerfile:

here is my Dockerfile:

FROM nginx:1.15.5-alpine
RUN addgroup --system app \
    && adduser --uid 1001 --system --ingroup app app \
    && rm -rf /etc/nginx/conf.d/default.conf \
    && apk add --update nodejs nodejs-npm \
    && mkdir /apptmp
COPY . /apptmp
RUN chmod 777 /apptmp
COPY config/default.conf /etc/nginx/conf.d/
COPY config/buildWithEnv.sh .

RUN touch /var/run/nginx.pid && \
  chown -R app:app /var/run/nginx.pid && \
  chown -R app:app /var/cache/nginx && \
  chown -R app:app /usr/share/nginx/html
EXPOSE 8080
USER 1001
CMD sh buildWithEnv.sh

这是脚本buildWithEnv.sh

And here is the script buildWithEnv.sh

#!/bin/bash

echo "===> Changin directory: /apptmp ..."
cd /apptmp

echo "===> Installing dependencies: npm install ..."
npm install

echo "===> Building application: npm run build ..."
npm run build

echo "===> Copying to exposed html folder ... "
rm -rf /usr/share/nginx/html/*
cp -r build/* /usr/share/nginx/html/

echo "===> Launching http server ... "
nginx -g 'daemon off;'

推荐答案

基本上:您将占位符用于静态资源中所有特定于环境的部分.然后,您配置nginx以在运行时将其替换为特定于环境的值.

Basically: You use placeholders for all environment specific parts in your static resources. Then you configure nginx to replace those at runtime with the environment specific values.

这篇关于如何使单个React Docker构建/映像在所有环境中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆