Reputation: 15520
I have Dockerfile which I have committed to a git repo. I'm trying to build the container in stages and only save the final container stage with static go binary installed in it.
However also "staging" container seems to be saved to my system. I have tried to automatically delete it by using --rm flag but without success.
Here is my Dockerfile
# Use golang alpine3.7 image for build
FROM golang:1.10.0-alpine3.7
# Maintainer information
LABEL Maintainer="Kimmo Hintikka"
# set working directory to local source
WORKDIR /go/src/github.com/HintikkaKimmo/kube-test-app
# Copy kube-test-app to currect working directory
COPY kube-test-app.go .
# build golang binary from the app source
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o kube-test-app .
# Use Alpine 3.7 as base to mach the build face
FROM alpine:3.7
# Maintainer information
LABEL Maintainer="Kimmo Hintikka"
# Install ca-certificates for HTTPS connections
RUN apk --no-cache add ca-certificates
# Set working directory
WORKDIR /root/
# Copy binary from previous stage. --from=0 means the index of the build action
COPY --from=0 /go/src/github.com/HintikkaKimmo/kube-test-app/kube-test-app .
# Run the binary
CMD [ "./kube-test-app" ]
And here is the command I used to build it.
docker build --rm github.com/hintikkakimmo/kube-test-app -t kube-test-app
This builds successfully but also creates an additional tagless TAG <none>
container which is the 1st phase of my build.
~/go/src/github.com/hintikkakimmo/kube-test-app(master*) » docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kube-test-app latest f87087bf2549 8 minutes ago 11.3MB
<none> <none> 1bb40fa05f27 8 minutes ago 397MB
golang 1.10.0-alpine3.7 85256d3905e2 7 days ago 376MB
alpine 3.7 3fd9065eaf02 6 weeks ago 4.15MB
alpine latest 3fd9065eaf02 6 weeks ago 4.15MB
My Docker version is:
~ » docker --version [email protected]@Kimmos-MacBook-Pro
Docker version 17.12.0-ce, build c97c6d6
Upvotes: 8
Views: 7424
Reputation: 117
I took a different approach. I set a label on the intermediate container:
FROM golang:1.14.2 AS builder
LABEL builder=true
When I run the build, I simply append a command that seeks and destroys images containing that label:
docker build . -t custom-api:0.0.1 && \
docker rmi `docker images --filter label=builder=true -q`
Upvotes: 5
Reputation: 659
You should build it like this:
docker build --rm -t kube-test-app .
If dockerfile is in direcory were you located or specify path to dockerfile
docker build --rm -t kube-test-app -f path/to/dockerfile .
-t
is tag, name of your builded docker image
For remove all except images and runing containers use docker system prune
For remove image docker rmi -f image_name
Upvotes: 9