Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically delete intermediate stage Docker containers from my system?

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
like image 727
Kimmo Hintikka Avatar asked Feb 25 '18 12:02

Kimmo Hintikka


People also ask

How do I remove intermediate docker images?

You can use docker image prune -f after building images. This command will remove any untagged docker images from your machine. What if we want to remove only selected untagged images? Use filter for this command docker image prune command.

How do you automatically delete a container when it exits?

If you know when you're creating a container that you won't want to keep it around once you're done, you can run docker run --rm to automatically delete it when it exits: Run and Remove: docker run --rm image_name.

How do I force all docker containers to delete?

Stop and remove all containers The following command is convenient if you want to stop/remove all the containers, including the running container. In this command, docker ps -a -q is used to display a list of IDs of all Docker containers, and docker rm is used to delete them.

How do I clean my docker system?

To clean these things up, you can use docker network prune to clean up networks which aren't used by any containers. By default, you are prompted to continue. To bypass the prompt, use the -f or --force flag. Other filtering expressions are available.


1 Answers

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

like image 134
trigun117 Avatar answered Sep 28 '22 19:09

trigun117