'How to give name or tag for intermediate image?

I used docker to build my Java application and I using the multi-stage building and I have some problems every time when I run the docker command for building the docker creates new intermediate image with tag and name none and I need the possibility to called intermediate containers.

That is my dockerfile:

FROM jdk8_201-ubuntu16.04 as java_build
RUN apt-get update && \
    apt-get install -y dos2unix

ARG MVN_USER
ARG MVN_PASS
ARG GIT_BRANCH
ARG BUILD_ID
ARG COMMIT_ID
WORKDIR /tmp/app

COPY pom.xml /maven-build/pom.xml
COPY /.mvn/settings.xml /maven-build/settings.xml
COPY mvnw ./mvnw
COPY mvnw.cmd ./mvnw.cmd
COPY /.mvn ./.mvn

RUN chmod +x ./mvnw && \
./mvnw -s /maven-build/settings.xml -B -f /maven-build/pom.xml dependency:resolve dependency:resolve-plugins dependency:go-offline

COPY ./ ./

FROM ubuntu
...

and after each running docker build command I had a many none images:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              30e2325fcf15        18 hours ago        1.68GB
<none>              <none>              30e2325fcf16        18 hours ago        1.68GB
<none>              <none>              30e2325fcf14        18 hours ago        1.68GB
<none>              <none>              30e2325fcf18        18 hours ago        1.68GB
<none>              <none>              30e2325fcf13        18 hours ago        1.68GB

How can I replace none name of intermediate images to my_image_name?



Solution 1:[1]

You can use the --target keyword to build just a specific stage:

docker build --target compile-image --tag compile-image:0.0.1 .

Solution 2:[2]

This Docker forum post suggest to use labels as follows:

For anyone that is interested in something similar, I’ve found a workaround for now.

Using the LABEL command in my dockerfile I added a label to the build stage I’m interested in, then filter for it later:

FROM node as builder-stage LABEL builder=true

FROM node as app-stage LABEL builder=false

Now to filter, sort newest first, cut out the date, and take the top row: docker images --filter “label=builder=true” --format ‘{{.CreatedAt}}\t{{.ID}}’ | sort -nr | head -n 1 | cut -f2

Note that it is important to remove the label in the next build stage.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mierzwid
Solution 2 TheDiveO