Docker机器教程

112 阅读5分钟

Docker机器教程

通常情况下,我们创建的服务会在容器中运行,然后通过本地主机访问。Docker机器带来了一种管理和访问这些服务的新方式。

它允许开发者在虚拟主机上创建docker引擎,并通过指定的端口将其公开。

在本教程中,我将带领你了解docker-machine的概念,并讨论它如何暴露其在虚拟机中运行的服务。

要求

  • 对docker有基本了解。
  • 在你的本地机器上安装Docker引擎。
  • 在你的开发环境中安装虚拟机。在本教程中,我们将使用VirtualBox。

目的

本教程的目的是向你介绍docker机器的概念。

首先,我将告诉你如何在Ubuntu上安装和运行docker机器。然后,我们将继续配置和管理多个远程Docker主机。

开始使用Docker-machine

一个Docker机器运行在虚拟主机、本地开发环境、云端,甚至是某个服务器上。

我们知道,Docker守护程序利用docker知识完成了创建和执行容器的所有工作。

在这个正常的设置过程中,你可以使用docker命令行工具管理你的容器和图像。

随着docker-machine的引入,你可以轻松地设置尽可能多的容器,将它们部署到虚拟主机,如VirtualBox。然后这个主机暴露出一个IP地址,正如我们在一分钟内看到的那样,并为你提供一个环境来管理这些容器。

这种docker-machine的优点是,它允许一个简单的交互式环境来管理几个容器,这是我们仅仅使用docker无法实现的。

Docker Machine与Docker Engine的区别

每当你听到docker ,它通常指的是docker engine 。当你运行如下图所示的命令时,你只是与docker引擎的命令行界面进行交互。

docker run <image>

现在,想象一下,你有多个docker化的Java应用程序的情况。要独立管理这些应用程序是很难的。这就是docker机器出现的地方。

正如前面所讨论的,docker-machine是用来配置和管理这些docker化主机的。它的工作原理是,这个机器被安装在本地机器上。然后它被用来在虚拟化环境中安装docker引擎。

安装Docker Machine

# run this command to check your installed version
docker --version

通过运行以下命令,继续并下载docker Machine。

# this installs the docker machine for Linux
base=https://github.com/docker/machine/releases/download/v0.16.0 \
  && curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine \
  && sudo mv /tmp/docker-machine /usr/local/bin/docker-machine \
  && chmod +x /usr/local/bin/docker-machine

输出。

# when you run the above command, you're supposed to see something like this
# you notice it moves the docker-machine to a new directory
# then sets the access mode to allow for execution

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   633  100   633    0     0    511      0  0:00:01  0:00:01 --:--:--   511
100 26.8M  100 26.8M    0     0   374k      0  0:01:13  0:01:13 --:--:--  318k

为了检查你是否已经成功安装了docker-machine,请在终端上运行以下命令。

docker-machine --version

输出示例。

# note that this version is at the time of this writing
docker-machine version 0.16.0, build 702c267f

使用docker-machine来运行docker容器

现在我们来看看docker-machine是如何用来管理docker容器的。首先运行这个命令来列出你的docker机器。

 $ docker-machine ls
 # no output since we've not 
 # created any machine.
 
 NAME   ACTIVE   DRIVER   STATE   URL   SWARM   DOCKER   ERRORS
 

让我们继续,通过执行下面的命令创建一个机器。

docker-machine create --driver VirtualBox default
# you notice we've included a driver flag passed the name of our machine

输出。

# the result while installing the docker-machine
Running pre-create checks...
Creating machine...
#... some output goes here
Waiting for the machine to be running, may take a few minutes...
# here the OS you're currently using for installation is being detected
Detecting operating system of created instance...
# ssh connection setup
Waiting for SSH to be available...
# detecting the provisioner
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
# here you notice we're connected to virtual machine
# a remote machine hence certs are being generated and 
# configured
Copying certs to the remote machine...
# docker configuration process
Setting Docker configuration on the remote daemon...
#... installation complete
Docker is up and running!
#...

现在我们已经成功创建了一个docker-machine,让我们继续通过运行下面的命令检查这个机器是否存在。

  docker-machine ls
 # this command lists down the list of machines available

输出。


NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default   -        virtualbox   Running   tcp://192.168.99.102:2376           v19.03.12  

让我们为我们的虚拟机设置环境命令,如下所示。

# a command to create an env command
docker-machine env default

输出。

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.102:2376"
export DOCKER_CERT_PATH="/home/jumamiller/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell: 
# eval $(docker-machine env default)

最后,让我们通过运行上一步输出中给出的命令,将我们的shell连接到我们的虚拟环境。

# configuring the shell
eval $(docker-machine env default)

运行容器并试验机器命令

现在我们已经把一切都准备好了,让我们继续,通过运行以下命令来验证我们的安装。

# donwloads docker-machine and prints hello John doe
docker run busybox echo hello John doe

输出。

# it downloads busybox and prints hello John doe
# if the image is not locally available, you should it is pulled
b71f96345d44: Pull complete 
Digest: sha256:0f354ec1728d9ff32edcd7d1b8bbdfc798277ad36120dc3dc683be44524c8b60
#...
Hello John Doe

接下来,通过运行以下命令获得VirtualBox的IP地址。

# run this command to get the IP address of our default docker-machine
docker-machine IP default

IP地址 输出。

# It's important to note that this may vary from your local machine
192.168.99.102

然后,我们可以通过运行下面的命令,继续运行Nginx容器。

# note that if you don't have this image, it will be pulled!
docker run -d -p 8000:80 nginx

预期的输出。

#...pulling image
33847f680f63: Pull complete 
dbb907d5159d: Pull complete 
8a268f30c42a: Pull complete 
b10cf527a02d: Pull complete 
c90b090c213b: Pull complete 
1f41b2f2bf94: Pull complete 
Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90
Status: Downloaded newer image for nginx:latest
d74c42d4e75403d4e4bd0031b293df6bbe0040e1a94a2ebdf1960caa6698b808

现在我们可以通过使用CuRL在上面定义的8000端口上打出服务器,如下图所示。

curl $(docker-machine IP default):8000

输出。

<!-- note that this an output on the bash -->
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
 ...
    
</style>
...
</body>
</html>

这就是使用docker机器来创建和管理多个使用虚拟机和docker机器的主机的简单和容易。

总结

部署Docker-ready云服务器不会比使用Docker Machine容易得多。无论你是想快速测试一个正在开发的容器,还是想建立一个按需扩展的集群,其简单性和易用性都将帮助你节省时间和金钱。

在本教程中,我们已经讨论了docker机器的关键概念。我们已经看到我们如何提供这个工具来管理多个docker化的应用程序。