将Docker中间层保留为多级构建

时间:2018-02-22 17:56:07

标签: docker docker-compose

我试图拥有一个开发容器和一个"生产"从单个Dockerfile构建的容器,它已经"工作"但是我在构建之后无法访问dev容器(多级中介被缓存,但没有以有用的方式标记)。

Dockerfile是如此:

# See https://github.com/facebook/flow/issues/3649 why here
# is a separate one for a flow using image ... :(
FROM node:8.9.4-slim AS graphql-dev
WORKDIR /graphql-api
ENV PATH /graphql-api/node_modules/.bin:$PATH
RUN apt update && apt install -y libelf1
ADD ./.babelrc /graphql-api/
ADD ./.eslintignore /graphql-api/
ADD ./.eslintrc /graphql-api/
ADD ./.flowconfig /graphql-api/
ADD ./.npmrc /graphql-api/
ADD ./*.json5 /graphql-api/
ADD ./lib/ /graphql-api/lib
ADD ./package.json /graphql-api/
ADD ./schema/ /graphql-api/schema
ADD ./yarn.lock /graphql-api/
RUN yarn install --production --silent && npm install --silent
CMD ["npm", "run", "lint-flow-test"]

# Cleans node_modules etc, see github.com/tj/node-prune
# this container contains no node, etc (golang:latest)
FROM golang:latest AS graphql-cleaner
WORKDIR /graphql-api
ENV PATH /graphql-api/node_modules/.bin:$PATH
COPY --from=graphql-dev graphql-api .
RUN go get github.com/tj/node-prune/cmd/node-prune
RUN node-prune

# Minimal end-container (Alpine )
FROM node:8.9.4-alpine
WORKDIR /graphql-api
ENV PATH /graphql-api/node_modules/.bin:$PATH
COPY --from=graphql-cleaner graphql-api .
EXPOSE 3000
CMD ["npm", "start"]

理想情况下,我可以使用graphql-dev启动docker-compose.yml和最终容器,如下所示:

version: '3'
services:
  graphql-dev:
    image: graphql-dev
    build: ./Dockerfile
    volumes:
      - ./lib:/graphql-api/lib
      - ./schema:/graphql-api/schema
  graphql-prod:
    image: graphql
    build: ./Dockerfile

最后两个步骤是"缩小"最终构建(为我们节省超过250Mb)并不是真正需要的,除了在生产构建中。

  • 如果我将dockerfile解压缩为两个......不知怎的Dockerfile.prodDockerfile.dev那么我必须管理它们之间的依赖关系,因为我无法强制prod始终构建{{ 1}}(我可以吗?)
  • 如果我能以某种方式在docker-compose.yml文件中的构建中指定dev我可以做到,有一些问题,但在target下指定target在我的yml文件中产生错误:
  

错误:撰写文件' ./ docker-compose.yml'无效是因为:
    services.graphql-dev.build包含不受支持的选项:' target'

0 个答案:

没有答案