displayname
displayname

Reputation: 168

How to delete first image after finish package in dockerfile?

My Dockerfile is;

FROM openjdk:8-jdk-alpine3.7 AS builder
RUN java -version

COPY . /usr/src/myapp/
WORKDIR /usr/src/myapp/
RUN apk --no-cache add maven && mvn --version
RUN mvn package

FROM openjdk:8-jre-alpine3.7
WORKDIR /root/
COPY --from=builder /usr/src/myapp/target/app-0.0.1-SNAPSHOT.jar .
ENTRYPOINT ["java","-jar","./app-0.0.1-SNAPSHOT.jar"]

It creates 2 image after build. First one is package image. Second image is my real image for java project. Is there a way delete first one after end its purpose?

Upvotes: 1

Views: 804

Answers (2)

Sheikh Sameen
Sheikh Sameen

Reputation: 11

Use the docker images command with the -a flag to locate the ID of the images you want to remove.

This will show you every image, including intermediate image layers. When you’ve located the images you want to delete, you can pass their ID or tag to docker rmi:

List:

$docker images -a

Remove:

$docker rmi image_id

Upvotes: 0

Neo Anderson
Neo Anderson

Reputation: 6340

There is no magic argument that can be passed to your docker build command to achieve this.

One downside of deleting the image created in your build-stage is that you might increase your build time(openjdk:8-jdk-alpine3.7 won't be deleted and therefore you won't pull it every time, but all the cached layers built on top of it will be rebuilt every time)

If you want to automate the process, be able to identify these helper images and eventually delete them in one shot, you can add a specific LABEL during the build. Here is an example:

The Dockerfile

FROM openjdk:8-jdk-alpine3.7 AS builder
LABEL stage=auto-clean
RUN echo "imagine building the .war"

FROM openjdk:8-jre-alpine3.7
RUN echo "imagine copying and running the .war"

...creates 2 images when built:

docker image ls | head -3
REPOSITORY                         TAG                          IMAGE ID            CREATED             SIZE
my-awesome-jre                     latest                       a95b17254ce8        3 seconds ago       82MB
<none>                             <none>                       75bac7f33564        4 seconds ago       102MB

...and another one for each new build:

docker image ls | head -4
REPOSITORY                         TAG                          IMAGE ID            CREATED             SIZE
<none>                             <none>                       a1b67d521a69        3 seconds ago       102MB
my-awesome-jre                     latest                       a95b17254ce8        53 seconds ago      82MB
<none>                             <none>                       75bac7f33564        54 seconds ago      102MB

You can filter all those build-stage images using the LABEL that you assigned to them:

docker image ls --filter LABEL=stage=auto-clean
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
<none>              <none>              a1b67d521a69        About a minute ago   102MB
<none>              <none>              75bac7f33564        2 minutes ago        102MB

...and delete them all in one shot:

docker image prune --filter LABEL=stage=auto-clean

...to enjoy a cleaner local registry:

docker image ls | head -3
REPOSITORY                         TAG                          IMAGE ID            CREATED             SIZE
my-awesome-jre                     latest                       a95b17254ce8        4 minutes ago       82MB
goplayground                       latest                       68badb576c2a        23 hours ago        7.68MB

Upvotes: 1

Related Questions