我希望每次运行时都能重新构建应用程序,因为我在入口点使用了环境变量。在这一点上我不能运行Dockerfile。所以我想先将源代码复制到docker映像,然后运行npm install。之后,我想用环境变量构建它。他们总是不同的。在构建时,我希望将构建文件复制到nginx html目录中。所以基本上我想每次运行docker映像时都构建react应用。
# build environment
FROM node:10.15.3 as builder
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
ADD . /usr/src/app
COPY ./entrypoint.sh /
RUN chmod u+x /entrypoint.sh
RUN rm /usr/src/app/src/settings/settings.js
RUN npm install
ENTRYPOINT ["/bin/sh","/entrypoint.sh"]
# production environment
FROM nginx:1.16.0-alpine
RUN rm -rf /etc/nginx/conf.d
ADD conf /etc/nginx
COPY --from=builder /usr/src/app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
Entrypoint.sh
#!/usr/bin/env bash
echo "
export const settings = {
apiDomain: '${API_DOMAIN}',
features:{
customersAndTasks: ${CUSTOMERSANDTASKS},
memberEvents: ${MEMBEREVENTS}
},
version: "0.1.1"
};
" > /usr/src/app/src/settings/settings.js
npm run build /usr/src/app