doolo00026 2019-05-13 12:37
浏览 148
已采纳

如何为开发和生产使用相同的dockerfile

I have 2 Dockerfile, 1 for dev, and 1 for prod:

PROD:

FROM golang AS builder
WORKDIR /go/src/gitlab.com/company/project
COPY . .
RUN go build -o ./release/api .

FROM scratch
EXPOSE 9999
COPY --from=builder /go/src/gitlab.com/company/project/release/api .
CMD ["./api"]

and DEV:

FROM golang
WORKDIR /go/src/gitlab.com/company/PROJECT
COPY . .
RUN go build -o ./release/api .
CMD ["./release/api"]

I would like to merge those two into a single one, as maintaining 2 Dockerfile is a bad practice

The main difference is that in dev, I work on golang image, which is practical, and in prod, I work with scratch image, which reduce a lot the size of my binary.

It seems that I can use multiple stages in a Dockerfile, and specify stage name at build time:

docker build . --target=builder

But If I do this, I don't know how to conditionnaly run my app in the first stage = [ If I am in dev, run the app in the first stage, otherwise, run the app in the second stage]

How should I do it ?

  • 写回答

1条回答 默认 最新

  • dtm37893 2019-05-13 14:40
    关注

    What about something like this? I didn't test it and didn't think through your example deeply, but maybe is somewhere close to what you need/helps you finding out the final solution?

    FROM golang:alpine AS base
    WORKDIR /go/src/gitlab.com/company/project
    COPY . .
    RUN go build -o ./release/api .
    
    FROM base AS dev
    CMD ["./release/api"]
    
    FROM scratch AS prod
    EXPOSE 9999
    COPY --from=base /go/src/gitlab.com/company/project/release/api .
    CMD ["./api"]
    

    Depending on the value specified in target docker build --target=prod or docker build --target=dev, a different image will get built.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号