Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Docker Machine?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

What is Docker Machine?

Docker Machine is a tool officially provided by Docker, which can help us install Docker on remote machines, or install virtual machines directly on virtual machine host and install Docker in virtual machines. We can also manage these virtual machines and Docker through the docker-machine command. The following is a picture from the official document of Docker Machine, very vivid!

This article will demonstrate the main usage scenarios of Docker Machine through a series of demo.

Install Docker Machine

Please install Docker locally before installing Docker Machine.

The installation of Docker Machine is very simple, just download the executable file locally in Ubuntu.

$curl-L https://github.com/docker/machine/releases/download/v0.12.0/docker-machine-`uname-s`-`uname-m` > / tmp/docker-machine$ chmod + x / tmp/docker-machine$ sudo mv / tmp/docker-machine / usr/local/bin/docker-machine

Where v0.12.0 is the latest version. Of course, Docker Machine is an open source project, and you can choose to install a different version or compile it yourself. The following figure shows the version information displayed by the author after installation:

Install Docker on a remote host

What if we have multiple Ubuntu hosts that need to install Docker? Do you log in one by one and install it through the apt-get command? Of course not, we can easily install Docker on a remote host with the docker-machine command.

prerequisite

We need to do some preparatory work before using docker-machine for remote installation:

1. Create a user on the target host and join the sudo group

two。 You do not need to enter a password to set the sudo operation for this user

3. Add the ssh public key of the local user to the target host

For example, we want to add a user named nick on the remote host and join the sudo group:

$sudo adduser nick$ sudo usermod-a-G sudo nick

Then set the sudo operation without entering a password:

$sudo visudo

Add the following line to the end of the document and save the file:

Nick ALL= (ALL:ALL) NOPASSWD: ALL

Finally, add the ssh public key of the local user to the target host:

$ssh-copy-id-I ~ / .ssh/id_rsa.pub nick@xxx.xxx.xxx.xxx

The main purpose of these steps is to obtain sufficient permissions to operate the target host remotely.

Installation command

Run the following command locally:

$docker-machine create-d generic\-- generic-ip-address=xxx.xxx.xxx.xxx\-- generic-ssh-user=nick\-- generic-ssh-key ~ / .ssh/id_rsa\ krdevdb

Note that the create command was intended to create a virtual host and install Docker, because the target host in this example already exists, so only Docker is installed. -d is short for-- driver and is mainly used to specify what driver to use to create the target host. Docker Machine supports the creation of hosts on CVM, which is achieved by using different drivers. In this case, generic is fine. The next three parameters that begin with-- generic are mainly the target host for the specified operation and the account used. The last parameter, krdevdb, is the name of the virtual machine, which Docker Machine uses to set the name of the target host.

All right, it's that simple! After a brief wait, Docker is successfully installed on the target machine:

Check the installation result

We can view the list of currently manageable hosts through the ls command of Docker Machine:

The krdevdb host is the host on which we just installed Docker, and the last column shows the installed Docker version: v17.05.0-ce.

Then execute the eval $(docker-machine env krdevdb) command, and you can manipulate the Docker daemon on the remote host through the local client. Execute the docker version command to see:

Note that the Client and Server versions in the figure above are not the same, which also shows that we are using the local Client to connect to the remote Server.

Install a virtual machine with Docker on the local host

In practical use, we usually install virtual machine management software such as vSphere on the physical machine, which is called virtual machine host. Then install the virtual machine through the vSphere tool and use it. Next we will show how to install a virtual machine with Docker on host, a local virtual machine with vSphere installed. Direct command:

$docker-machine create\-driver vmwarevsphere\-vmwarevsphere-vcenter=xxx.xxx.xxx.xxx\-vmwarevsphere-username=root\-vmwarevsphere-password=12345678\-vmwarevsphere-cpu-count=1\-vmwarevsphere-memory-size=512\-vmwarevsphere-disk-size=10240\ testvm

Explain the more important parameters:

-- driver vmwarevsphere

Our virtual machine host is installed on vmware's product vSphere, so we need to provide the corresponding driver for Docker Machine so that we can install a new virtual machine on it.

-- vmwarevsphere-vcenter=xxx.xxx.xxx.xxx\

-- vmwarevsphere-username=root\

-- vmwarevsphere-password=12345678\

The above three lines specify the IP address, username, and password of the virtual machine host, respectively.

-- vmwarevsphere-cpu-count=1\

-- vmwarevsphere-memory-size=512\

-- vmwarevsphere-disk-size=10240\

The above three lines specify the cpu, memory, and disk resources occupied by the newly created virtual machine, respectively.

Testvm

The last parameter is the name of the new virtual machine.

Soon the creation of the virtual machine is complete. First, take a look at the client side of vSphere:

A virtual machine named testvm is already running.

Then execute the docker-machine ls command to see:

You can already see testvm, and its DRIVER is displayed as vmwarevsphere.

Is that it?

There seems to be something wrong. Yes, what is the most important thing when we create a virtual machine manually? It is an image to install a virtual machine. But we didn't specify anything related here, so what kind of system did docker-machine install for us? When installing virtual machines using the vmwarevsphere driver, we cannot specify our favorite virtual machine image (maybe Docker Machine is not ready yet). A virtual machine image called boot2docker is used by default, which is very small, only a few tens of megabytes, so the installation will be very fast.

Manage remote Docker

Client-server mode

Docker has always been running in both client and server mode, but the original version started both the server-side daemon and the client through the same binary file, docker. In recent versions, the server executable has been separated from the client executable. View the executable file in the / usr/bin directory:

Where dockerd is the executable file that performs server-side tasks. While we usually execute native docker tasks, we mainly send tasks to the server side of the machine through the client command docker.

Use a local client to connect to a remote server

Can the local client connect and send tasks to the remote Docker server? Of course you can, but it's a little more troublesome for us to set it up manually. But it doesn't matter, Docker Machine has done it for us! Let's take a look at how to run the container on the host krdevdb through the local Docker client:

$docker-machine env krdevdb

The output of this command can be used as a command to set some of the environment variables used by the Docker client so that the local Docker client can communicate with the remote Docker server. Follow the prompts above to execute the command:

$eval $(docker-machine env krdevdb)

Well, in the current command line terminal, the next docker command that runs operates on Docker daemon on the remote host krdevdb. To distinguish between native Docker daemon operations, we restart a new command line terminal and execute the docker ps command separately:

From the figure above, it is obvious that different containers are running on the local host and the remote host.

Manage remote Docker daemon

In addition to running basic docker commands, Docker Machine can also manage remote Docker hosts. For example, we can start, shut down, and restart remote Docker daemon with start, stop, and restart commands, respectively. The situation here is a little more complicated, and only drivers that support these commands can complete the relevant operations. For example, we shut down krdevdb and testvm respectively:

The previous prompt that the generic driver does not support the stop command. Testvm is installed through the vmwarevsphere driver, so stop is executed successfully.

SSH support is essential for remote management! Of course, Docker Machine has done his job dutifully:

$docker-machine ssh krdevdb

Just carry out the above orders. Note that this command will not prompt you for a password, let alone configure the SSH key, because Docker Machine has done all the dirty work in private.

Summary

The purpose of Docker Machine is to simplify the installation and remote management of Docker. We can also see from the content of this article that Docker Machine does bring a lot of convenience for us to use and manage Docker. As for the areas that need to be improved, now Docker Machine will install the latest version of Docker. I think it would be nice if I could support the specified version of Docker!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report