编译 + 部署
通常我们需要在一个指定的目标环境进行代码的编译,比如nodejs12
、golang:1.15
等环境
那么我们就可以编写 dockerfile 来实现
前提:一个 dockerfile 有且只有一个基础镜像,比如在生产环境运行时,其实不需要大而全的 nodejs 包,只需要一个小而美的即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
FROM node:12.19.0 AS build WORKDIR /code
COPY . .
RUN yarn RUN yarn build
RUN rm -rf node_modules RUN yarn install --production
FROM node:12.19.0-alpine
WORKDIR /code
COPY --from=build /code/dist /code/server COPY --from=build /code/node_modules /code/node_modules
CMD ["node", "server"]
|
动态的dockerfile
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ARG VERSION
FROM source1:$VERSION AS source1-build FROM source2:$VERSION AS source2-build FROM source3:$VERSION AS source3-build
FROM final:alpine
WORKDIR /code
ARG VERSION RUN echo $VERSION
COPY --from=source1-build /code/dist /code/source1-build COPY --from=source2-build /code/dist /code/source2-build COPY --from=source3-build /code/dist /code/source3-build
|
通过该dockerfile,制作最终的镜像命令
1 2
| version=1.0.0 docker build --build-arg VERSION=${version} -f dockerfile -t source:$version .
|