Docker 为什么要创建两个图像而不是一个?

Docker 为什么要创建两个图像而不是一个?,docker,Docker,请参阅下面的命令: docker build -t iansbasecontainer:v1 -f DotnetDebug.Dockerfile . 它创建一个容器,如下所示: DotnetDebug.Dockerfile如下所示: FROM microsoft/aspnetcore:2.0 AS base # Install the SSHD server RUN apt-get update \ && apt-get install -y --no-install-

请参阅下面的命令:

docker build -t iansbasecontainer:v1 -f DotnetDebug.Dockerfile .
它创建一个容器,如下所示:

DotnetDebug.Dockerfile如下所示:

FROM microsoft/aspnetcore:2.0 AS base

# Install the SSHD server
RUN apt-get update \
  && apt-get install -y --no-install-recommends openssh-server \
  && mkdir -p /run/sshd \
  && echo "root:Docker!" | chpasswd
#Copy settings file. See elsewhere to find them. 
COPY sshd_config /etc/ssh/sshd_config
COPY authorized_keys  root/.ssh/authorized_keys

# Install Visual Studio Remote Debugger
RUN apt-get install zip unzip
RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg

EXPOSE 2222
FROM iansbasecontainer:v1 AS base
WORKDIR /app

MAINTAINER Vladimir Vladimir@akopyan.me 

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src

COPY ./DebugSample .
RUN dotnet restore 

FROM build AS publish
RUN dotnet publish -c Debug -o /app

FROM base AS final
COPY --from=publish /app /app
COPY ./StartSSHAndApp.sh /app

EXPOSE 5000

CMD /app/StartSSHAndApp.sh
#If you wish to only have SSH running and start 
#your service when you start debugging
#then use just the SSH server, you don't need the script
#CMD ["/usr/sbin/sshd", "-D"]
然后运行以下命令:

docker build -t iansimageusesbasecontainer:v1 -f DebugASP.Dockerfile .
但是,会出现两幅图像:

DebugASP.Dockerfile如下所示:

FROM microsoft/aspnetcore:2.0 AS base

# Install the SSHD server
RUN apt-get update \
  && apt-get install -y --no-install-recommends openssh-server \
  && mkdir -p /run/sshd \
  && echo "root:Docker!" | chpasswd
#Copy settings file. See elsewhere to find them. 
COPY sshd_config /etc/ssh/sshd_config
COPY authorized_keys  root/.ssh/authorized_keys

# Install Visual Studio Remote Debugger
RUN apt-get install zip unzip
RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg

EXPOSE 2222
FROM iansbasecontainer:v1 AS base
WORKDIR /app

MAINTAINER Vladimir Vladimir@akopyan.me 

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src

COPY ./DebugSample .
RUN dotnet restore 

FROM build AS publish
RUN dotnet publish -c Debug -o /app

FROM base AS final
COPY --from=publish /app /app
COPY ./StartSSHAndApp.sh /app

EXPOSE 5000

CMD /app/StartSSHAndApp.sh
#If you wish to only have SSH running and start 
#your service when you start debugging
#then use just the SSH server, you don't need the script
#CMD ["/usr/sbin/sshd", "-D"]
为什么会出现两个图像?请注意,我是Docker的新手,所以这可能是一个简单的答案。我花了几个小时在谷歌上搜索

另外,为什么存储库和标记设置为:

为什么会出现两个图像?

如上所述:

使用多阶段构建时,每个阶段都会生成一个新映像。该映像存储在本地映像缓存中,并将用于后续生成(作为缓存机制的一部分)。您可以运行每个构建阶段(和/或标记阶段,如果需要)

阅读有关多阶段构建的更多信息

为什么会出现两个图像?

如上所述:

使用多阶段构建时,每个阶段都会生成一个新映像。该映像存储在本地映像缓存中,并将用于后续生成(作为缓存机制的一部分)。您可以运行每个构建阶段(和/或标记阶段,如果需要)


阅读有关多阶段构建的更多信息。

Docker为每个层生成中间(aka
)图像,这些图像稍后用于最终图像。如果执行
docker images-a
命令,您实际上可以看到它们

但你们所看到的被称为“悬空影像”。这是因为最终图像不再使用某些中间图像。在多阶段构建的情况下——前一阶段的图像不会在最终图像中使用,因此它们会悬空

悬挂的图像是无用的,并且会占用你的空间,因此建议定期清除它们(称为)。您可以使用以下命令执行此操作:

docker image prune

Docker为每个层生成中间(aka
)图像,这些图像稍后用于最终图像。如果执行
docker images-a
命令,您实际上可以看到它们

但你们所看到的被称为“悬空影像”。这是因为最终图像不再使用某些中间图像。在多阶段构建的情况下——前一阶段的图像不会在最终图像中使用,因此它们会悬空

悬挂的图像是无用的,并且会占用你的空间,因此建议定期清除它们(称为)。您可以使用以下命令执行此操作:

docker image prune