首页 > 解决方案 > 如何为中间图像命名或标记?

问题描述

我使用 docker 来构建我的 Java 应用程序,我使用多阶段构建,每次运行 docker 命令来构建 docker 时都会遇到一些问题,创建带有标签和名称的新中间图像,none我需要调用中间容器的可能性.

那是我的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
...

在每个运行docker build命令之后,我有很多none图像:

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

如何将none中间图像的名称替换为my_image_name

标签: javadocker

解决方案


您可以使用docker build -t image_name 标记图像。但中间图像将<none>作为图像名称。

<none>图像是作为多阶段构建的一部分形成的。如果您的 dockerfile 中有多个 FROM,则构建映像将创建多个映像以及<none>主映像使用的中间映像。

例如

码头文件:

$ 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"]

在上面的 dockerfile 中,我使用 ubuntu:18.04 作为中间图像,在上面的COPY命令中再次使用它。这里COPY --from=compile-image /root/helloworld .

所以当我通过命令使用上面的 dockerfile 构建图像时

$ sudo docker build -t sample_image .

上面的命令将创建两个图像,如下所示。

$ 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>是中间图像。这再次在 dockerfile 内部使用。

现在您的问题的解决方案是创建两个 docker 文件并分别使用它们。

比如上面的dockerfile可以分成两个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

并执行 $ sudo docker build -t compile_image .

通过再次更改 docker 文件

码头文件2:

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

并执行:$ sudo docker build -t runtime_image .

输出不会有中间图像。因此没有<none>图像。

$ 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

仅供参考:在第二个 docker 文件中,我以前在COPY命令中使用了构建图像compile_image

COPY --from=compile_image /root/helloworld .

这里 compile_image 就像一个中间图像。

如果您观察以前的图像,则与 compile_image 占用的内存<none>相同,并且 sample_image 和 runtime_image 相同。使用多阶段构建的主要目的是减小图像的大小。


推荐阅读