docker删除映像_如何为生产优化Docker映像

docker删除映像

The author selected Code.org to receive a donation as part of the Write for DOnations program.

作者选择Code.org接受捐赠,这是Write for DOnations计划的一部分。

介绍 (Introduction)

In a production environment, Docker makes it easy to create, deploy, and run applications inside of containers. Containers let developers gather applications and all their core necessities and dependencies into a single package that you can turn into a Docker image and replicate. Docker images are built from Dockerfiles. The Dockerfile is a file where you define what the image will look like, what base operating system it will have, and which commands will run inside of it.

在生产环境中, Docker使在容器内轻松创建,部署和运行应用程序变得容易。 容器使开发人员可以将应用程序及其所有核心必要性和依赖项收集到一个包中,您可以将其转变为Docker映像并进行复制。 Docker映像是从Dockerfiles构建的 。 Dockerfile是一个文件,您可以在其中定义映像的外观,映像的基本操作系统以及在其中运行的命令。

Large Docker images can lengthen the time it takes to build and send images between clusters and cloud providers. If, for example, you have a gigabyte-sized image to push every time one of your developers triggers a build, the throughput you create on your network will add up during the CI/CD process, making your application sluggish and ultimately costing you resources. Because of this, Docker images suited for production should only have the bare necessities installed.

大型Docker映像可以延长在集群和云提供商之间构建和发送映像所需的时间。 例如,如果您有一个千兆字节大小的映像,每当一位开发人员触发构建时要推送,则您在网络上创建的吞吐量将在CI / CD流程中累加,使您的应用程序变慢,最终使您浪费资源。 因此,适合生产的Docker映像仅应安装必需品。

There are several ways to decrease the size of Docker images to optimize for production. First off, these images don’t usually need build tools to run their applications, and so there’s no need to add them at all. By using a multi-stage build process, you can use intermediate images to compile and build the code, install dependencies, and package everything into the smallest size possible, then copy over the final version of your application to an empty image without build tools. Additionally, you can use an image with a tiny base, like Alpine Linux. Alpine is a suitable Linux distribution for production because it only has the bare necessities that your application needs to run.

有几种减小Docker映像大小以进行生产优化的方法。 首先,这些映像通常不需要构建工具来运行其应用程序,因此根本不需要添加它们。 通过使用多阶段构建过程 ,您可以使用中间映像来编译和构建代码,安装依赖项,并将所有内容打包到尽可能小的大小,然后在没有构建工具的情况下将应用程序的最终版本复制到空映像中。 此外,您可以使用基础很小的映像,例如Alpine Linux 。 Alpine是适合生产的Linux发行版,因为它仅具有运行您的应用程序所需的全部必需品。

In this tutorial, you’ll optimize Docker images in a few simple steps, making them smaller, faster, and better suited for production. You’ll build images for a sample Go API in several different Docker containers, starting with Ubuntu and language-specific images, then moving on to the Alpine distribution. You will also use multi-stage builds to optimize your images for production. The end goal of this tutorial is to show the size difference between using default Ubuntu images and optimized counterparts, and to show the advantage of multi-stage builds. After reading through this tutorial, you’ll be able to apply these techniques to your own projects and CI/CD pipelines.

在本教程中,您将通过几个简单的步骤优化Docker映像,使其更小,更快,更适合生产。 您将在几个不同的Docker容器中为示例Go API生成映像,从Ubuntu和特定于语言的映像开始,然后进行Alpine发行。 您还将使用多阶段构建来优化图像以进行生产。 本教程的最终目的是显示使用默认Ubuntu映像和优化镜像之间的大小差异,并展示多阶段构建的优势。 阅读完本教程后,您将能够将这些技术应用于自己的项目和CI / CD管道。

Note: This tutorial uses an API written in Go as an example. This simple API will give you a clear understanding of how you would approach optimizing Go microservices with Docker images. Even though this tutorial uses a Go API, you can apply this process to almost any programming language.

注意:本教程以Go语言编写的API为例。 这个简单的API将使您清楚地了解如何使用Docker映像来优化Go微服务。 即使本教程使用Go API,您也可以将此过程应用于几乎所有编程语言。

先决条件 (Prerequisites)

Before you start you will need:

在开始之前,您需要:

第1步-下载示例Go API (Step 1 — Downloading the Sample Go API)

Before optimizing your Docker image, you must first download the sample API that you will build your Docker images from. Using a simple Go API will showcase all the key steps of building and running an application inside a Docker container. This tutorial uses Go because it’s a compiled language like C++ or Java, but unlike them, has a very small footprint.

在优化Docker映像之前,必须首先下载用于构建Docker映像的示例API 。 使用简单的Go API将展示在Docker容器中构建和运行应用程序的所有关键步骤。 本教程使用Go,因为它是C ++Java之类的编译语言,但与它们不同,它的占用空间很小。

On your server, begin by cloning the sample Go API:

在您的服务器上,首先克隆示例Go API:

  • git clone https://github.com/do-community/mux-go-api.git

    git clone https://github.com/do-community/mux-go-api.git

Once you have cloned the project, you will have a directory named mux-go-api on your server. Move into this directory with cd:

克隆项目后,服务器上将有一个名为mux-go-api的目录。 使用cd进入此目录:

  • cd mux-go-api

    cd mux-go-api

This will be the home directory for your project. You will build your Docker images from this directory. Inside, you will find the source code for an API written in Go in the api.go file. Although this API is minimal and has only a few endpoints, it will be appropriate for simulating a production-ready API for the purposes of this tutorial.

这将是您的项目的主目录。 您将从该目录构建Docker映像。 在内部,您会在api.go文件中找到用Go编写的API的源代码。 尽管此API是最小的,并且只有几个端点,但对于本教程而言,它适合于模拟可用于生产的API。

Now that you have downloaded the sample Go API, you are ready to build a base Ubuntu Docker image, against which you can compare the later, optimized Docker images.

现在,您已经下载了示例Go API,现在可以构建基本的Ubuntu Docker映像了,您可以将其与以后的优化Docker映像进行比较。

第2步-构建基本的Ubuntu映像 (Step 2 — Building a Base Ubuntu Image)

For your first Docker image, it will be useful to see what it looks like when you start out with a base Ubuntu image. This will package your sample API in an environment similar to the software you’re already running on your Ubuntu server. Inside the image, you will install the various packages and modules you need to run your application. You will find, however, that this process creates a rather heavy Ubuntu image that will affect build time and the code readability of your Dockerfile.

对于您的第一个Docker映像,当您从基本的Ubuntu映像开始时,看看它的外观会很有用。 这会将您的示例API打包在与您已在Ubuntu服务器上运行的软件类似的环境中。 在映像内,您将安装运行应用程序所需的各种软件包和模块。 但是,您会发现此过程创建了一个相当沉重的Ubuntu映像,这将影响构建时间和Dockerfile的代码可读性。

Start by writing a Dockerfile that instructs Docker to create an Ubuntu image, install Go, and run the sample API. Make sure to create the Dockerfile in the directory of the cloned repo. If you cloned to the home directory it should be $HOME/mux-go-api.

首先编写一个Dockerfile,该文件指示Docker创建Ubuntu映像,安装Go并运行示例API。 确保在克隆的存储库目录中创建Dockerfile。 如果克隆到主目录,则应为$HOME/mux-go-api

Make a new file called Dockerfile.ubuntu. Open it up in nano or your favorite text editor:

新建一个名为Dockerfile.ubuntu文件。 在nano或您喜欢的文本编辑器中打开它:

  • nano ~/mux-go-api/Dockerfile.ubuntu

    纳米〜/ mux-go-api / Dockerfile.ubuntu

In this Dockerfile, you’ll define an Ubuntu image and install Golang. Then you’ll proceed to install the needed dependencies and build the binary. Add the following contents to Dockerfile.ubuntu:

在此Dockerfile中,您将定义Ubuntu映像并安装Golang。 然后,您将继续安装所需的依赖项并构建二进制文件。 将以下内容添加到Dockerfile.ubuntu

~/mux-go-api/Dockerfile.ubuntu
〜/ mux-go-api / Dockerfile.ubuntu
FROM ubuntu:18.04

RUN apt-get update -y \
  && apt-get install -y git gcc make golang-1.10

ENV GOROOT /usr/lib/go-1.10
ENV PATH $GOROOT/bin:$PATH
ENV GOPATH /root/go
ENV APIPATH /root/go/src/api

WORKDIR $APIPATH
COPY . .

RUN \ 
  go get -d -v \
  && go install -v \
  && go build

EXPOSE 3000
CMD ["./api"]

Starting from the top, the FROM command specifies which base operating system the image will have. Then the RUN command installs the Go language during the creation of the image. ENV sets the specific environment variables the Go compiler needs in order to work properly. WORKDIR specifies the directory where we want to copy over the code, and the COPY command takes the code from the directory where Dockerfile.ubuntu is and copies it over into the image. The final RUN command installs Go dependencies needed for the source code to compile and run the API.

从顶部开始, FROM命令指定映像将具有哪个基本操作系统。 然后, RUN命令在映像创建过程中安装Go语言。 ENV设置Go编译器需要的特定环境变量才能正常工作。 WORKDIR指定了我们要在其上复制代码的目录, COPY命令从Dockerfile.ubuntu所在的目录中Dockerfile.ubuntu代码并将其复制到映像中。 最终的RUN命令将安装Go依赖项,以供源代码编译和运行API。

Note: Using the && operators to string together RUN commands is important in optimizing Dockerfiles, because every RUN command will create a new layer, and every new layer increases the size of the final image.

注意:使用&&运算符将RUN命令串在一起对于优化Dockerfile很重要,因为每个RUN命令都会创建一个新层,并且每个新层都会增加最终映像的大小。

Save and exit the file. Now you can run the build command to create a Docker image from the Dockerfile you just made:

保存并退出文件。 现在,您可以运行build命令从刚创建的Dockerfile创建Docker映像:

  • docker build -f Dockerfile.ubuntu -t ubuntu .

    docker build -f dockerfile.ubuntu -t ubuntu。

The build command builds an image from a Dockerfile. The -f flag specifies that you want to build from the Dockerfile.ubuntu file, while -t stands for tag, meaning you’re tagging it with the name ubuntu. The final dot represents the current context where Dockerfile.ubuntu is located.

build命令从Dockerfile构建映像。 -f标志指定您要从Dockerfile.ubuntu文件构建,而-t代表tag,这意味着您正在使用ubuntu名称对其进行标记。 最后一个点表示Dockerfile.ubuntu所在的当前上下文。

This will take a while, so feel free to take a break. Once the build is done, you’ll have an Ubuntu image ready to run your API. But the final size of the image might not be ideal; anything above a few hundred MB for this API would be considered an overly large image.

这将需要一段时间,请随时休息一下。 构建完成后,您将准备好一个Ubuntu映像来运行您的API。 但是图像的最终尺寸可能并不理想。 此API超过几百MB的任何内容都将被视为过大的图像。

Run the following command to list all Docker images and find the size of your Ubuntu image:

运行以下命令以列出所有Docker映像并找到Ubuntu映像的大小:

  • docker images

    码头工人图像

You’ll see output showing the image you just created:

您将看到显示刚创建的图像的输出:


   
   
Output
REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 61b2096f6871 33 seconds ago 636MB . . .

As is highlighted in the output, this image has a size of 636MB for a basic Golang API, a number that may vary slightly from machine to machine. Over multiple builds, this large size will significantly affect deployment times and network throughput.

如输出中突出显示的那样,此图像的大小对于基本的Golang API 而言为636MB,该数字可能因机器而异。 在多个版本中,这种较大的大小将严重影响部署时间和网络吞吐量。

In this section, you built an Ubuntu image with all the needed Go tools and dependencies to run the API you cloned in Step 1. In the next section, you’ll use a pre-built, language-specific Docker image to simplify your Dockerfile and streamline the build process.

在本节中,您将使用所有必需的Go工具和依赖项来构建Ubuntu映像,以运行在步骤1中克隆的API。在下一节中,您将使用预构建的,特定于语言的Docker映像来简化Dockerfile。并简化构建过程。

第3步-建立特定于语言的基本映像 (Step 3 — Building a Language-Specific Base Image)

Pre-built images are ordinary base images that users have modified to include situation-specific tools. Users can then push these images to the Docker Hub image repository, allowing other users to use the shared image instead of having to write their own individual Dockerfiles. This is a common process in production situations, and you can find various pre-built images on Docker Hub for almost any use case. In this step, you’ll build your sample API using a Go-specific image that already has the compiler and dependencies installed.

预先构建的图像是用户已修改以包括特定于情况的工具的普通基础图像。 然后,用户可以将这些映像推送到Docker Hub映像存储库,从而允许其他用户使用共享映像,而不必编写自己的单独Dockerfile。 这是生产环境中的常见过程,您几乎可以在Docker Hub上找到各种预制的映像。 在此步骤中,您将使用Go特定的映像构建示例API,该映像已经安装了编译器和依赖项。

With pre-built base images already containing the tools you need to build and run your app, you can cut down the build time significantly. Because you’re starting with a base that has all needed tools pre-installed, you can skip adding these to your Dockerfile, making it look a lot cleaner and ultimately decreasing the build time.

借助已包含构建和运行应用程序所需工具的预构建基础映像,您可以大大减少构建时间。 因为您从一个预先安装了所有必需工具的基础开始,所以可以跳过将这些工具添加到Dockerfile中的过程,从而使其看起来更加整洁并最终减少了构建时间。

Go ahead and create another Dockerfile and name it Dockerfile.golang. Open it up in your text editor:

继续创建另一个Dockerfile并将其命名为Dockerfile.golang 。 在您的文本编辑器中打开它:

  • nano ~/mux-go-api/Dockerfile.golang

    纳米〜/ mux-go-api / Dockerfile.golang

This file will be significantly more concise than the previous one because it has all the Go-specific dependencies, tools, and compiler pre-installed.

该文件将比上一个更加简洁,因为它已预安装了所有Go特定的依赖项,工具和编译器。

Now, add the following lines:

现在,添加以下行:

~/mux-go-api/Dockerfile.golang
〜/ mux-go-api / Dockerfile.golang
FROM golang:1.10

WORKDIR /go/src/api
COPY . .

RUN \
    go get -d -v \
    && go install -v \
    && go build

EXPOSE 3000
CMD ["./api"]

Starting from the top, you’ll find that the FROM statement is now golang:1.10. This means Docker will fetch a pre-built Go image from Docker Hub that has all the needed Go tools already installed.

从顶部开始,您将发现FROM语句现在是golang: 1.10 。 这意味着Docker将从Docker Hub获取预安装的Go映像,该映像已经安装了所有必需的Go工具。

Now, once again, build the Docker image with:

现在,再次使用以下命令构建Docker映像:

  • docker build -f Dockerfile.golang -t golang .

    docker build -f dockerfile.golang -t golang。

Check the final size of the image with the following command:

使用以下命令检查图像的最终大小:

  • docker images

    码头工人图像

This will yield output similar to the following:

这将产生类似于以下内容的输出:


   
   
Output
REPOSITORY TAG IMAGE ID CREATED SIZE golang latest eaee5f524da2 40 seconds ago 744MB . . .

Even though the Dockerfile itself is more efficient and the build time is shorter, the total image size actually increased. The pre-built Golang image is around 744MB, a significant amount.

即使Dockerfile本身效率更高且构建时间较短,但总映像大小实际上还是增加了。 预先构建的Golang映像大约为744MB ,非常大。

This is the preferred way to build Docker images. It gives you a base image which the community has approved as the standard to use for the specified language, in this case Go. However, to make an image ready for production, you need to cut away parts that the running application does not need.

这是构建Docker映像的首选方法。 它为您提供了一个基本图像,该图像已被社区批准为用于指定语言(在本例中为Go)的标准。 但是,要使图像准备好生产,您需要切掉正在运行的应用程序不需要的部分。

Keep in mind that using these heavy images is fine when you are unsure about your needs. Feel free to use them both as throwaway containers as well as the base for building other images. For development or testing purposes, where you don’t need to think about sending images through the network, it’s perfectly fine to use heavy images. But if you want to optimize deployments, then you need to try your best to make your images as tiny as possible.

请记住,当您不确定自己的需求时,可以使用这些较重的图像。 随意将它们用作一次性容器以及构建其他图像的基础。 出于开发或测试目的,您无需考虑通过网络发送图像的情况,使用沉重的图像是完全可以的。 但是,如果要优化部署,则需要尽最大努力使映像尽可能小。

Now that you have tested a language-specific image, you can move on to the next step, in which you will use the lightweight Alpine Linux distribution as a base image to make your Docker image lighter.

现在,您已经测试了特定于语言的映像,可以继续进行下一步,在此步骤中,您将使用轻量级的Alpine Linux发行版作为基础映像,以减轻Docker映像的负担。

步骤4 —建立基本的高山影像 (Step 4 — Building Base Alpine Images)

One of the easiest steps to optimize your Docker images is to use smaller base images. Alpine is a lightweight Linux distribution designed for security and resource efficiency. The Alpine Docker image uses musl libc and BusyBox to stay compact, requiring no more than 8MB in a container to run. The tiny size is due to binary packages being thinned out and split, giving you more control over what you install, which keeps the environment as small and efficient as possible.

优化Docker映像的最简单步骤之一就是使用较小的基本映像。 Alpine是一种轻量级Linux发行版,旨在提高安全性和资源效率。 Alpine Docker映像使用musl libcBusyBox保持紧凑,运行一个容器所需的空间不超过8MB。 较小的尺寸是由于二进制软件包经过精简和拆分而获得的,从而使您可以更好地控制安装的内容,从而使环境尽可能地小巧高效。

The process of creating an Alpine image is similar to how you created the Ubuntu image in Step 2. First, create a new file called Dockerfile.alpine:

创建Alpine映像的过程类似于您在步骤2中创建Ubuntu映像的过程。首先,创建一个名为Dockerfile.alpine的新文件:

  • nano ~/mux-go-api/Dockerfile.alpine

    纳米〜/ mux-go-api / Dockerfile.alpine

Now add this snippet:

现在添加以下代码段:

~/mux-go-api/Dockerfile.alpine
〜/ mux-go-api / Dockerfile.alpine
FROM alpine:3.8

RUN apk add --no-cache \
    ca-certificates \
    git \
    gcc \
    musl-dev \
    openssl \
    go

ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
ENV APIPATH $GOPATH/src/api
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" "$APIPATH" && chmod -R 777 "$GOPATH"

WORKDIR $APIPATH
COPY . .

RUN \
    go get -d -v \
    && go install -v \
    && go build

EXPOSE 3000
CMD ["./api"]

Here you’re adding the apk add command to use Alpine’s package manager to install Go and all libraries it requires. As with the Ubuntu image, you need to set the environment variables as well.

在这里,您将添加apk add命令以使用Alpine的软件包管理器来安装Go及其所需的所有库。 与Ubuntu映像一样,您还需要设置环境变量。

Go ahead and build the image:

继续构建图像:

  • docker build -f Dockerfile.alpine -t alpine .

    docker build -f dockerfile.alpine -t alpine。

Once again, check the image size:

再次检查图像大小:

  • docker images

    码头工人图像

You will receive output similar to the following:

您将收到类似于以下内容的输出:


   
   
Output
REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest ee35a601158d 30 seconds ago 426MB . . .

The size has gone down to around 426MB.

大小已降至426MB左右。

The small size of the Alpine base image has reduced the final image size, but there are a few more things you can do to make it even smaller.

Alpine基本图像的小尺寸减小了最终图像的尺寸,但是您可以做更多的事情来减小它的尺寸。

Next, try using a pre-built Alpine image for Go. This will make the Dockerfile shorter, and will also cut down the size of the final image. Because the pre-built Alpine image for Go is built with Go compiled from source, its footprint is significantly smaller.

接下来,尝试使用Go的预构建Alpine图像。 这将使Dockerfile更短,并且还将缩小最终映像的大小。 由于Go的预构建Alpine映像是使用从源代码编译的Go构建的,因此其占用空间明显较小。

Start by creating a new file called Dockerfile.golang-alpine:

首先创建一个名为Dockerfile.golang-alpine的新文件:

  • nano ~/mux-go-api/Dockerfile.golang-alpine

    纳米〜/ mux-go-api / Dockerfile.golang-alpine

Add the following contents to the file:

将以下内容添加到文件中:

~/mux-go-api/Dockerfile.golang-alpine
〜/ mux-go-api / Dockerfile.golang-alpine
FROM golang:1.10-alpine3.8

RUN apk add --no-cache --update git

WORKDIR /go/src/api
COPY . .

RUN go get -d -v \
  && go install -v \
  && go build

EXPOSE 3000
CMD ["./api"]

The only differences between Dockerfile.golang-alpine and Dockerfile.alpine are the FROM command and the first RUN command. Now, the FROM command specifies a golang image with the 1.10-alpine3.8 tag, and RUN only has a command for installing Git. You need Git for the go get command to work in the second RUN command at the bottom of Dockerfile.golang-alpine.

Dockerfile.golang-alpineDockerfile.alpine之间的唯一区别是FROM命令和第一个RUN命令。 现在, FROM命令指定带有1.10-alpine3.8标签的golang映像,并且RUN仅具有用于安装Git的命令。 您需要Git才能使go get命令在Dockerfile.golang-alpine底部的第二个RUN命令中工作。

Build the image with the following command:

使用以下命令生成映像:

  • docker build -f Dockerfile.golang-alpine -t golang-alpine .

    docker build -f dockerfile.golang-alpine -t golang-alpine。

Retrieve your list of images:

检索图像列表:

  • docker images

    码头工人图像

You will receive the following output:

您将收到以下输出:


   
   
Output
REPOSITORY TAG IMAGE ID CREATED SIZE golang-alpine latest 97103a8b912b 49 seconds ago 288MB

Now the image size is down to around 288MB.

现在,图像大小已降至288MB左右。

Even though you’ve managed to cut down the size a lot, there’s one last thing you can do to get the image ready for production. It’s called a multi-stage build. By using multi-stage builds, you can use one image to build the application while using another, lighter image to package the compiled application for production, a process you will run through in the next step.

即使您已经成功地缩小了尺寸,也可以做最后一件事使图像准备好投入生产。 这称为多阶段构建。 通过使用多阶段构建,您可以使用一个映像来构建应用程序,而使用另一个较轻的映像来打包已编译的应用程序以进行生产,该过程将在下一步中执行。

步骤5 —排除具有多阶段构建的构建工具 (Step 5 — Excluding Build Tools with a Multi-Stage Build)

Ideally, images that you run in production shouldn’t have any build tools installed or dependencies that are redundant for the production application to run. You can remove these from the final Docker image by using multi-stage builds. This works by building the binary, or in other terms, the compiled Go application, in an intermediate container, then copying it over to an empty container that doesn’t have any unnecessary dependencies.

理想情况下,在生产环境中运行的映像不应安装任何构建工具或对生产环境应用程序而言多余的依赖关系。 您可以使用多阶段构建将它们从最终的Docker映像中删除。 通过在中间容器中构建二进制文件(或换句话说,已编译的Go应用程序)来工作,然后将其复制到没有任何不必要依赖项的空容器中。

Start by creating another file called Dockerfile.multistage:

首先创建另一个名为Dockerfile.multistage文件:

  • nano ~/mux-go-api/Dockerfile.multistage

    纳米〜/ mux-go-api / Dockerfile.multistage

What you’ll add here will be familiar. Start out by adding the exact same code as with Dockerfile.golang-alpine. But this time, also add a second image where you’ll copy the binary from the first image.

您将在此处添加的内容很熟悉。 首先添加与Dockerfile.golang-alpine完全相同的代码。 但是这一次,还要添加第二个图像,在该图像中,您将从第一个图像复制二进制文件。

~/mux-go-api/Dockerfile.multistage
〜/ mux-go-api / Dockerfile.multistage
FROM golang:1.10-alpine3.8 AS multistage

RUN apk add --no-cache --update git

WORKDIR /go/src/api
COPY . .

RUN go get -d -v \
  && go install -v \
  && go build

##

FROM alpine:3.8
COPY --from=multistage /go/bin/api /go/bin/
EXPOSE 3000
CMD ["/go/bin/api"]

Save and close the file. Here you have two FROM commands. The first is identical to Dockerfile.golang-alpine, except for having an additional AS multistage in the FROM command. This will give it a name of multistage, which you will then reference in the bottom part of the Dockerfile.multistage file. In the second FROM command, you’ll take a base alpine image and COPY over the compiled Go application from the multistage image into it. This process will further cut down the size of the final image, making it ready for production.

保存并关闭文件。 在这里,您有两个FROM命令。 第一个与Dockerfile.golang-alpine相同,除了在FROM命令中具有附加的AS multistage 。 这将为其命名为multistage ,您将在Dockerfile.multistage文件的底部引用该Dockerfile.multistage 。 在第二个FROM命令中,您将获取基本的alpine图像,并从multistage图像中将已编译的Go应用程序COPYCOPY 。 此过程将进一步缩小最终图像的尺寸,使其可以投入生产。

Run the build with the following command:

使用以下命令运行构建:

  • docker build -f Dockerfile.multistage -t prod .

    docker build -f dockerfile.multistage -t prod。

Check the image size now, after using a multi-stage build.

使用多阶段构建后,现在检查图像大小。

  • docker images

    码头工人图像

You will find two new images instead of only one:

您会发现两个新图像,而不是一个:


   
   
Output
REPOSITORY TAG IMAGE ID CREATED SIZE prod latest 82fc005abc40 38 seconds ago 11.3MB <none> <none> d7855c8f8280 38 seconds ago 294MB . . .

The <none> image is the multistage image built with the FROM golang:1.10-alpine3.8 AS multistage command. It’s only an intermediary used to build and compile the Go application, while the prod image in this context is the final image which only contains the compiled Go application.

<none>映像是使用FROM golang:1.10-alpine3.8 AS multistage命令构建的multistage映像。 它只是用于构建和编译Go应用程序的中介,而在此上下文中的prod映像是仅包含已编译的Go应用程序的最终映像。

From an initial 744MB, you’ve now shaved down the image size to around 11.3MB. Keeping track of a tiny image like this and sending it over the network to your production servers will be much easier than with an image of over 700MB, and will save you significant resources in the long run.

从最初的744MB减少到现在的图像大小减少到11.3MB左右。 与拥有700MB以上的映像相比,跟踪这样的微小映像并将其通过网络发送到生产服务器要容易得多,从长远来看,它将节省大量资源。

结论 (Conclusion)

In this tutorial, you optimized Docker images for production using different base Docker images and an intermediate image to compile and build the code. This way, you have packaged your sample API into the smallest size possible. You can use these techniques to improve build and deployment speed of your Docker applications and any CI/CD pipeline you may have.

在本教程中,您使用不同的基础Docker映像和一个中间映像来编译和构建代码,从而优化了Docker映像的生产环境。 这样,您已将示例API打包为尽可能小的大小。 您可以使用这些技术来提高Docker应用程序以及您可能拥有的任何CI / CD管道的构建和部署速度。

If you are interested in learning more about building applications with Docker, check out our How To Build a Node.js Application with Docker tutorial. For more conceptual information on optimizing containers, see Building Optimized Containers for Kubernetes.

如果您想了解有关使用Docker构建应用程序的更多信息,请查看我们的《 如何使用Docker构建Node.js应用程序》教程。 有关优化容器的更多概念性信息,请参阅为Kubernetes构建优化的容器

翻译自: https://www.digitalocean.com/community/tutorials/how-to-optimize-docker-images-for-production

docker删除映像

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值