Skip to content
Advertisement

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?

Advertisement

Answer

you can use docker build -t image_name to tag the image.But intermediate images will have <none> as the image name.

<none> images are formed as part of the multistage builds. If there are multiple FROM in your dockerfile then building image will create multiple images along with <none> images which are intermediate images used by main image.

for example

dockerfile:

$ cat dockerfile
FROM ubuntu:18.04 AS compile-image
RUN apt-get update
RUN apt-get install -y --no-install-recommends gcc build-essential
WORKDIR /root
COPY . hello.c
RUN mkdir -p helloworld hello.c

FROM ubuntu:18.04 AS runtime-image
COPY --from=compile-image /root/helloworld .
CMD ["./helloworld"]

In the above dockerfile I am using ubuntu:18.04 as intermediate image which is used again in the COPY command above. HERE COPY --from=compile-image /root/helloworld .

So When I build an image using above dockerfile by command

$ sudo docker build -t sample_image .

The above command will create two images as below.

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
sample_image        latest              32b62676d8b8        About a minute ago   64.2MB
<none>              <none>              dfb370218e54        About a minute ago   282MB

<none> is an intermediate image. which is again internally used in the dockerfile.

now the solution for your question would be creating two docker files and using them separately.

For example, the above dockerfile can be divided into two dockerfiles and can be used to create separate images without any intermediate image.

dockerfile 1:

$ sudo vi dockerfile
$ sudo cat dockerfile
FROM ubuntu:18.04 AS compile-image
RUN apt-get update
RUN apt-get install -y --no-install-recommends gcc build-essential
WORKDIR /root
COPY . hello.c
RUN mkdir -p helloworld hello.c

and execute $ sudo docker build -t compile_image .

by changing docker file again

docker file 2:

$ sudo vi dockerfile
$ sudo cat dockerfile
FROM ubuntu:18.04
COPY --from=compile_image /root/helloworld .
CMD ["./helloworld"]

and execute: $ sudo docker build -t runtime_image .

the output will not have intermediate images. Hence no <none> images.

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
compile_image       latest              b580efe23dad        About a minute ago   282MB
runtime_image       latest              32b62676d8b8        3 minutes ago        64.2MB

FYI: In second docker file, I used previously build image compile_image in COPY command

COPY --from=compile_image /root/helloworld .

Here compile_image acts like an intermediate image.

If you observe previous images the memory occupied by <none> and compile_image are same and sample_image and runtime_image are the same. The main purpose of using the multistage builds is to reduce the size of an image.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement