[英]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
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
链接了这两个操作,并在服务之前简单地运行构建。
"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"]
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.