简体   繁体   English

如何通过在 docker-compose.yaml 中传递 env_file 从 dockerfile 容器重建 create-react-app

[英]How to rebuild create-react-app from a dockerfile container by passing env_file in docker-compose.yaml

I will try to be as descriptive as I can but I have an react app built with creat-react-app from this app I build a docker image.我会尽量做到描述性,但我有一个用 creat-react-app 构建的 react 应用程序,我构建了一个 docker 镜像。

dockerfile

FROM node:17-alpine

# set working directory
WORKDIR /app

# install app dependencies
#copies package.json and package-lock.json to Docker environment
COPY package.json ./

RUN apk add --no-cache --virtual .gyp python3 make g++ \
    && yarn install \
    && apk del .gyp

# Copies everything over to Docker environment
COPY . ./
RUN yarn build-docker

#install serve package
RUN yarn global add serve

EXPOSE 3000 5000
ENTRYPOINT ["serve", "-s", "build"]

Which works fine locally with the .env file included in the project.使用项目中包含的.env文件在本地运行良好。 For our production and development we use docker-compose and include the env_file in our docker-compose.yaml对于我们的生产和开发,我们使用env_file docker-compose并将env_file包含在我们docker-compose.yaml

docker-compse.yaml sample docker-compse.yaml 示例

app:
    container_name: app
    image: {image_source}
    restart: always
    env_file:
      - env/app.env

api:
    container_name: api
    image: {image_source}
    restart: always
    env_file:
      - env/global.env
      - env/db.env

The api works fine since it's using ENTRYPOINT ["npm", "start"] and reload the .env file every time it start, but since the app is built before the docker-compose up -d , api 工作正常,因为它使用ENTRYPOINT ["npm", "start"]并在每次启动时重新加载.env文件,但由于app是在.env docker-compose up -d之前构建的,

Is their a way to rebuild using yarn build-docker with docker-compose so my new build get the right .env file?他们是一种使用yarn build-docker .env docker-compose重建的方法,以便我的新构建获得正确的.env文件吗?

I finally found out how to solve the issue.我终于找到了解决问题的方法。

Instead of building the app within the container I chained both action in package.json and simply run the build before the serve.我没有在容器中构建应用程序,而是在package.json链接了这两个操作,并在服务之前简单地运行构建。

package.json包.json

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "build-docker": "react-scripts --openssl-legacy-provider build",
    "build-start": "react-scripts --openssl-legacy-provider build && serve -s build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

"build-start": "react-scripts --openssl-legacy-provider build && serve -s build"

And in dockerfile i removed the yarn build-docker and changed the entrypoint to ENTRYPOINT ["npm", "run", "build-start"]在 dockerfile 中,我删除了yarn build-docker ENTRYPOINT ["npm", "run", "build-start"]并将入口点更改为ENTRYPOINT ["npm", "run", "build-start"]

dockerfile文件

FROM node:17-alpine

# set working directory
WORKDIR /app

# install app dependencies
#copies package.json and package-lock.json to Docker environment
COPY package.json ./

RUN apk add --no-cache --virtual .gyp python3 make g++ \
    && yarn install \
    && apk del .gyp

# Copies everything over to Docker environment
COPY . ./

##### REMOVED THE FIRST BUILD 
#RUN yarn build-docker 

#install serve package
RUN yarn global add serve

EXPOSE 3000 5000
##### CHANGED THE ENTRYPOINT WITH THE NEW SCRIPT
ENTRYPOINT ["npm", "run", "build-start"]

So everytime it goes up with docker-compose the .env is used during the build.因此,每次使用.env -compose 时, .env在构建期间使用.env

Hope this could help someone!希望这可以帮助某人!

暂无
暂无

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

相关问题 如何从env_file获取docker-compose的端口? - How to get port of docker-compose from env_file? 如何在运行时读取通过 docker-compose.yaml 文件传递​​的反应应用程序中的环境变量? - How to read environment variables in react app passed through docker-compose.yaml file during run time? 适用于不同环境的Dockerfile和docker-compose.yaml - Dockerfile and docker-compose.yaml for different environments Dockerfile 可以用 docker-compose.yaml 替换吗? - Is Dockerfile replacable with docker-compose.yaml? 使用新的 env_file 更新 in-docker-compose 容器 - Update in-docker-compose container with new env_file Docker compose:如何定义指向容器内而非物理服务器内某些文件的env_file - Docker compose : how to define env_file pointing to some file inside the container and not the physical server Docker compose is using the env which was built by Dockerfile, instead of using env_file located in docker-compose.yml directory because of webpack - Docker compose is using the env which was built by Dockerfile, instead of using env_file located in docker-compose.yml directory because of webpack 如何使用 Heroku 中的 docker-compose.yaml 文件部署应用程序 - How to deploy application with docker-compose.yaml file in Heroku 使用 env_file 参数指定的 Docker 组合环境变量在 dockerfile 中不起作用 - Docker compose environment variables specified with env_file parameter not working inside the dockerfile 读取容器的特定 Docker env_file - Reading a specific Docker env_file for a container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM