在 Dockerfile 中使用哪个 docker 基础镜像?

Which docker base image to use in the Dockerfile?

我有一个 Web 应用程序,它包含两个项目:

我现在需要 docker 使用 docker 调整我的应用程序的大小,但我不确定 docker 文件中的第一行(指的是used environment I guess, source).

我现在需要做的是为两个项目制作单独的 docker 图片,但由于我对此很陌生,所以我无法弄清楚这两个项目的第一行应该是什么Dockerfiles(在两个项目中)。

我在 Windows 10 OS 开发项目,我有节点版本 v8.11.1 和 expressjs 版本 4.16.3.

我尝试使用我找到的一些版本(如 node:8.11.1-alpine),但我收到警告:`

SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host.

这让我觉得我不应该只关心节点版本,而应该关心 OS。所以不确定现在要使用哪个基础图像。

node:8.11.1-alpine 是一个完全正确的 tag for a Node image. This particular one is based on Alpine Linux - 一个轻量级的 Linux 发行版,由于占用空间小,因此经常在构建 Docker 图像时使用。

如果您不确定应该选择哪个基础映像,请阅读 the documentation at DockerHub。它列出了所有当前支持的标签并描述了 Node 图像的不同风格('Image Variants' 部分)。

引用:

Image Variants

The node images come in many flavors, each designed for a specific use case.

node:<version>

This is the defacto image. If you are unsure about what your needs are, you probably want to use this one. It is designed to be used both as a throw away container (mount your source code and start the container to start your app), as well as the base to build other images off of. This tag is based off of buildpack-deps. buildpack-deps is designed for the average user of docker who has many images on their system. It, by design, has a large number of extremely common Debian packages. This reduces the number of packages that images that derive from it need to install, thus reducing the overall size of all images on your system.

node:<version>-alpine

This image is based on the popular Alpine Linux project, available in the alpine official image. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general.

This variant is highly recommended when final image size being as small as possible is desired. The main caveat to note is that it does use musl libc instead of glibc and friends, so certain software might run into issues depending on the depth of their libc requirements. However, most software doesn't have an issue with this, so this variant is usually a very safe choice. See this Hacker News comment thread for more discussion of the issues that might arise and some pro/con comparisons of using Alpine-based images.

To minimize image size, it's uncommon for additional related tools (such as git or bash) to be included in Alpine-based images. Using this image as a base, add the things you need in your own Dockerfile (see the alpine image description for examples of how to install packages if you are unfamiliar).

node:<version>-onbuild

The ONBUILD image variants are deprecated, and their usage is discouraged. For more details, see docker-library/official-images#2076.

While the onbuild variant is really useful for "getting off the ground running" (zero to Dockerized in a short period of time), it's not recommended for long-term usage within a project due to the lack of control over when the ONBUILD triggers fire (see also docker/docker#5714, docker/docker#8240, docker/docker#11917).

Once you've got a handle on how your project functions within Docker, you'll probably want to adjust your Dockerfile to inherit from a non-onbuild variant and copy the commands from the onbuild variant Dockerfile (moving the ONBUILD lines to the end and removing the ONBUILD keywords) into your own file so that you have tighter control over them and more transparency for yourself and others looking at your Dockerfile as to what it does. This also makes it easier to add additional requirements as time goes on (such as installing more packages before performing the previously-ONBUILD steps).

node:<version>-slim

This image does not contain the common packages contained in the default tag and only contains the minimal packages needed to run node. Unless you are working in an environment where only the node image will be deployed and you have space constraints, we highly recommend using the default image of this repository.