如何下载基础 docker 镜像以创建 hello world docker 镜像?

Pru*_*ala 0 image download docker

我在 docker 中处于非常初级的位置。我正在尝试创建需要一些基本图像的 helloworld docker 图像。我在 Intranet 网络中工作。那么,我怎样才能获得基本图像呢??。我正在使用 Redhat linux 7。

tha*_*tah 5

Docker 会自动下载您在 Dockerfile 中指定的基础镜像。因此,例如,一个非常简单的“hello-world”图像可能是;

Dockerfile 的内容:

FROM alpine
CMD echo "hello-world"
Run Code Online (Sandbox Code Playgroud)

从 Dockerfile 构建映像,并将其命名为“hello”:

docker build -t hello .
Run Code Online (Sandbox Code Playgroud)

你会看到 Docker 拉取alpine镜像,如果它还没有,然后构建镜像;

Sending build context to Docker daemon 2.048 kB
Step 1 : FROM alpine
 ---> 70c557e50ed6
Step 2 : CMD echo "hello world"
 ---> Running in 94fcb9b89aaa
 ---> 6462a272b1d6
Removing intermediate container 94fcb9b89aaa
Successfully built 6462a272b1d6
Run Code Online (Sandbox Code Playgroud)

然后你可以从镜像运行一个容器(这个镜像只会运行,完成后直接退出)

$ docker run hello
hello world
Run Code Online (Sandbox Code Playgroud)

您可以将任何镜像用作FROM您自己镜像的“基础” ( ) 镜像,因此您在 Docker Hub 上找到的任何镜像都可以用作基础。

但是,一般情况下,建议使用“官方”图像作为您自己图像的基础。有各种可用的“发行版”,例如 Ubuntu、Debian、Alpine(如果您需要轻量级映像)、CentOS 等。基本映像不必与主机的发行版匹配(因此您可以使用“ubuntu”即使您的主机上运行了 Red Hat,也可以为您的图像提供服务)

你可以在这里找到官方图片;https://hub.docker.com/explore/

有关更高级的“hello world”图像,请参阅此处的文档;https://docs.docker.com/engine/userguide/containers/dockerizing/

以及编写 Dockerfile 的最佳实践;https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/