brance
brance

Reputation: 1178

Multistage build image not working, while normal build does

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?

Upvotes: 4

Views: 2569

Answers (1)

David Caiazzo
David Caiazzo

Reputation: 36

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"]

Upvotes: 0

Related Questions