dongrong9053 2018-08-02 14:17
浏览 48

多阶段构建映像不起作用,而普通构建却起作用

I have a Dockerfile that builds a golang project (that listens to the Twitter stream and lists the tweets by some filter) from the latest golang docker image, right now 1.10.3, like so:

FROM golang:1.10.3
COPY . /destination/
WORKDIR /destination/
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
CMD ["./main"]

and when I run the image that is created by the docker build command, it runs without a problem. The problem is that the image is about 900MB in size.

When I try to build the docker with the multistage build, like so:

FROM golang:1.10.3-alpine3.7 as builder
...
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
FROM busybox
COPY --from=builder /go/src/github.com/mygithubname/ /
WORKDIR /path-to-the-main-file/
CMD ["./main"]

The docker image is built successfully and it runs, but it just doesn't show me tweets when they are being posted, and it also doesn't show any errors, that is the most confusing part. Even when I ssh into the running container and ping google it shows everything as it should be.

Anyone has some hints how I could debug this problem, or to get some more logs out of the running Docker image?

  • 写回答

1条回答 默认 最新

  • drfqfuhej48511519 2018-08-02 18:50
    关注

    Few things

    1) Did the container exit? If you do a docker ps does it seem to be running.

    2) Did you check docker log {container id}

    3) Does /go/src/github.com/mygithubname/ actually reflect the location of the build in the first stage of the docker container? Sample docker file where the copy reflects the work dir of the build environment:

    FROM golang AS build-env
    ADD . /src/project
    WORKDIR /src/project
    RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
    
    # final stage
    FROM busybox
    WORKDIR /app
    COPY --from=build-env /src/project/ .
    CMD ["./main"]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看