当容器在启动 CRA 应用程序之前运行时,我试图在初始化时在 docker 容器中运行 bash 脚本。此 bash 脚本将环境变量复制GCP_PROJECT_ID
到.env
文件定义的docker-entrypoint.sh
文件中。我尝试使用 anEntrypoint
来运行该文件,但它不起作用。
文件
FROM node:9-alpine
RUN npm install yarn
WORKDIR /usr/app
COPY ./yarn.lock /usr/app
COPY ./package.json /usr/app
RUN yarn install
COPY . /usr/app/
RUN yarn build
FROM node:9-alpine
WORKDIR /usr/app
COPY --from=0 /usr/app/build /usr/app/build
COPY --from=0 /usr/app/package.json /usr/app/package.json
COPY .env /usr/app/.env
COPY docker-entrypoint.sh /usr/app/docker-entrypoint.sh
RUN chmod +x /usr/app/docker-entrypoint.sh
RUN yarn global add serve
RUN npm prune --production
ARG GCP_PROJECT_ID=xxxxx # SAMPLE ENVIRONMENT VARIABLES
ENV GCP_PROJECT_ID=$GCP_PROJECT_ID
ENTRYPOINT [ "/usr/app/docker-entrypoint.sh" ]
CMD [ "serve", "build" ]
docker-entrypoint.sh
#!/bin/sh
printf "\n" >> /usr/app/.env
printf "REACT_APP_GCP_PROJECT_ID=$GCP_PROJECT_ID" >> /usr/app/.env
我可以验证环境变量确实存在,即 runningdocker run -it --entrypoint sh <IMAGE NAME>
并且echo $GCP_PROJECT_ID
确实 print xxxxx
。
如何在 docker 中启动我的 CRA 应用程序之前运行 bash 脚本?