Using Local Docker Images With Minikube – 使用Minikube的本地Docker镜像

最后修改: 2022年 8月 30日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll deploy Docker containers to Kubernetes and see how we can use local images for these containers. We’ll use Minikube to run the Kubernetes cluster.

在本教程中,我们将把Docker容器部署到Kubernetes,并看看我们如何为这些容器使用本地镜像。我们将使用Minikube>来运行Kubernetes集群。

2. Dockerfile

2.Dockerfile

First of all, we need a Dockerfile to be able to create local Docker images. This should be simple because we’ll focus on the Minikube commands.

首先,我们需要一个Dockerfile,以便能够创建本地Docker镜像。这应该很简单,因为我们将专注于Minikube命令。

Let’s create a Dockerfile with just an echo command that prints a message:

让我们创建一个Dockerfile,只用一个echo命令来打印一条信息。

FROM alpine 

CMD ["echo", "Hello World"]

3. docker-env Command

3.docker-env命令

For the first approach, we need to make sure that Docker CLI is installed. This is a tool for managing Docker resources, such as images and containers.

对于第一种方法,我们需要确保安装Docker CLI>。这是一个用于管理Docker资源的工具,例如图像和容器。

By default, it uses the Docker Engine on our machine, but we can change that easily. We’ll use this and point our Docker CLI to the Docker Engine inside Minikube.

默认情况下,它使用我们机器上的Docker引擎,但我们可以很容易地改变它。我们将使用这个,把我们的Docker CLI指向Minikube内部的Docker引擎。

Let’s check this prerequisite and see if the Docker CLI is working:

让我们检查一下这个先决条件,看看Docker CLI是否在工作。

$ docker version

The output should be similar to this:

输出应该与此类似。

Client: Docker Engine - Community
 Version:           19.03.12
 ...

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  ...

Let’s proceed with the next steps. We can configure this CLI to use the Docker Engine inside Minikube. This way, we’ll be able to list the images available in Minikube and even build images inside it.

让我们继续接下来的步骤。我们可以配置这个CLI来使用Minikube内的Docker引擎。这样,我们将能够列出Minikube中可用的图像,甚至在里面构建图像。

Let’s see the steps required to configure the Docker CLI:

让我们看看配置Docker CLI所需的步骤

$ minikube docker-env

We can see the list of commands here:

我们可以看到这里的命令列表。

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://172.22.238.61:2376"
export DOCKER_CERT_PATH="C:\Users\Baeldung\.minikube\certs"
export MINIKUBE_ACTIVE_DOCKERD="minikube"

# To point your shell to minikube's docker-daemon, run:
# eval $(minikube -p minikube docker-env)

Let’s execute the command from the last line as it will do the configuration for us:

让我们执行最后一行的命令,因为它将为我们进行配置:

$ eval $(minikube -p minikube docker-env)

Now, we can use the Docker CLI to investigate the Docker environment inside Minikube.

现在,我们可以使用Docker CLI来调查Minikube内部的Docker环境。

Let’s list the available images with the minikube image ls command:

让我们用minikube image ls命令列出可用的图像:

$ minikube image ls --format table

This prints a table with the images:

这将打印出一个包含图像的表格。

|-----------------------------------------|---------|---------------|--------|
|                  Image                  |   Tag   |   Image ID    |  Size  |
|-----------------------------------------|---------|---------------|--------|
| docker.io/kubernetesui/dashboard        | <none>  | 1042d9e0d8fcc | 246MB  |
| docker.io/kubernetesui/metrics-scraper  | <none>  | 115053965e86b | 43.8MB |
| k8s.gcr.io/etcd                         | 3.5.3-0 | aebe758cef4cd | 299MB  |
| k8s.gcr.io/pause                        | 3.7     | 221177c6082a8 | 711kB  |
| k8s.gcr.io/coredns/coredns              | v1.8.6  | a4ca41631cc7a | 46.8MB |
| k8s.gcr.io/kube-controller-manager      | v1.24.3 | 586c112956dfc | 119MB  |
| k8s.gcr.io/kube-scheduler               | v1.24.3 | 3a5aa3a515f5d | 51MB   |
| k8s.gcr.io/kube-proxy                   | v1.24.3 | 2ae1ba6417cbc | 110MB  |
| k8s.gcr.io/pause                        | 3.6     | 6270bb605e12e | 683kB  |
| gcr.io/k8s-minikube/storage-provisioner | v5      | 6e38f40d628db | 31.5MB |
| k8s.gcr.io/echoserver                   | 1.4     | a90209bb39e3d | 140MB  |
| k8s.gcr.io/kube-apiserver               | v1.24.3 | d521dd763e2e3 | 130MB  |
|-----------------------------------------|---------|---------------|--------|

If we compare it with the output of the docker image ls command, we see that both show the same list. This means that our Docker CLI is correctly configured.

如果我们将其与docker image ls命令的输出进行比较,我们看到两者都显示了相同的列表。这意味着我们的Docker CLI是正确配置的。

Let’s use our Dockerfile and build an image from it:

让我们使用我们的Dockerfile从它建立一个图像

$ docker build -t first-image -f ./Dockerfile .

Now that it’s available within Minikube, we can create a pod that uses this image:

现在,它在Minikube内可用,我们可以创建一个使用这个图像的pod。

$ kubectl run first-container --image=first-image --image-pull-policy=Never --restart=Never

Let’s check the logs of this pod:

让我们检查一下这个Bean荚的日志。

$ kubectl logs first-container

We can see the expected “Hello World” message. Everything works correctly. Let’s close the terminal to make sure our Docker CLI is not connected to Minikube for the next example.

我们可以看到预期的 “Hello World “信息。一切运作正常。让我们关闭终端,以确保我们的Docker CLI在下一个例子中没有连接到Minikube。

4. Minikube Image Load Command

4.Minikube图像加载命令

Let’s see another approach for using local images. This time, we’ll build the Docker image outside of Minikube on our machine and load it into Minikube. Let’s build the image:

让我们看看另一种使用本地镜像的方法。这一次,我们将在我们的机器上建立Minikube之外的Docker镜像,并将其加载到Minikube。

$ docker build -t second-image -f ./Dockerfile .

Now the image exists, but it’s not yet available in Minikube. Let’s load it:

现在图像已经存在,但在Minikube中还不能使用。让我们加载它

$ minikube image load second-image

Let’s list the images and check if it’s available:

让我们列出图像并检查是否可用。

$ minikube image ls --format table

We can see the new image in the list. This means that we can create the pod:

我们可以在列表中看到新的图像。这意味着我们可以创建Bean荚。

$ kubectl run second-container --image=second-image --image-pull-policy=Never --restart=Never

The container starts successfully. Let’s check the logs:

容器成功启动。让我们检查一下日志。

$ kubectl logs second-container

We can see it prints the correct message.

我们可以看到它打印了正确的信息。

5. Minikube Image Build Command

5.Minikube图像构建命令

In the previous example, we loaded a pre-built Docker image to Minikube. However, we can also build our images inside Minikube.

在前面的例子中,我们加载了一个预先构建的Docker镜像到Minikube。然而,我们也可以在Minikube内部构建我们的图像。

Let’s use the same Dockerfile and build a new Docker image:

让我们使用相同的Dockerfile,构建一个新的Docker镜像。

$ minikube image build -t third-image -f ./Dockerfile .

Now that the image is available in Minikube, we can start a container with it:

现在图像在Minikube中是可用的,我们可以用它启动一个容器。

$ kubectl run third-container --image=third-image --image-pull-policy=Never --restart=Never

Let’s check the logs to make sure it’s working:

让我们检查一下日志,以确保它在工作:

$ kubectl logs third-container

It prints the “Hello World” message as expected.

它如期打印出 “Hello World “消息。

6. Conclusion

6.结论

In this article, we used three different ways to run local Docker images in Minikube.

在这篇文章中,我们用三种不同的方式在Minikube中运行本地Docker镜像。

Firstly, we configured our Docker CLI to connect to the Docker Engine inside Minikube. Then, we saw two commands to load a pre-built image and build an image directly in Minikube.

首先,我们配置了我们的Docker CLI以连接到Minikube内的Docker引擎。然后,我们看到了两个命令,以加载一个预构建的图像和直接在Minikube中构建一个图像。