What is Docker machine?
Docker Machine is an official Docker tool that can help us install Docker on remote machines, or install VMS directly on a virtual machine host and install Docker in a virtual machine. We can also manage these VMS and Docker through the Docker-machine command. Here is a picture from the official Docker machine document, very image Oh!
This article will showcase the main usage scenarios for Docker machine through a series of demos.
Installing Docker Machine
Before installing Docker machine, install Docker locally.
The installation of the Docker machine is simple, and it is possible to download the executable file locally in Ubuntu directly.
$ 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 version information that is displayed for the author after installation:
Installing Docker on a remote host
What if we have more than one Ubuntu host that needs to install Docker? is not a login up through the apt-get command to install it? Of course not, with docker-machine command we can easily install Docker on the remote host.
Pre-conditions
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
2. You do not need to enter a password to set sudo for this user
3. Add the local user's SSH public key to the target host
For example, we want to add a user named Nick to the remote host and join the sudo group:
$ sudo adduser nick$ sudo usermod-a-g sudo nick
Then set the sudo action without entering the password:
$ sudo visudo
Add the following line to the end of the document and save the file:
Nick all= (All:all) Nopasswd:all
Finally, the local user's SSH public key is added to the target host:
$ ssh-copy-id-i ~/.ssh/id_rsa.pub [email protected]
The primary purpose of these steps is to obtain sufficient permissions to operate the target host remotely.
Installation commands
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 created to have a virtual host and install Docker, because the target host already exists in this example, so only Docker is installed. -D is a shorthand form for--driver, which is used primarily to specify what driver to use to create the target host. Docker Machine supports the creation of hosts on cloud servers, which is achieved by using different drivers. In this case, you can use generic. The next three parameters that start with--generic are the target host for the specified operation and the account used. The last parameter, Krdevdb, is the name of the virtual machine, which Docker machines uses to set the name of the target host.
All right, that's easy! After a short wait, Docker was successfully installed on the target machine:
Check the installation results
We can view the list of currently manageable hosts through the LS command of Docker machine:
The KRDEVDB host is just the host where we installed Docker, and the last column shows the installed Docker version: V17.05.0-ce.
Then execute the eval $ (docker-machine env KRDEVDB) command to operate the Docker daemon on the remote host via the local client. Execute the Docker version command to see:
Note that the client and server versions in are different, which also means that we are using the local client to connect to the remote server.
Installing a virtual machine with Docker on the local host
In practice, we typically install virtual machine management software such as VSphere on a physical machine and call it virtual machine host. Then use the VSphere tool to install the virtual machine. Next we'll show you how to install a virtual machine with Docker on a local virtual machine host that has VSphere installed. Directly on the 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
The VMware product VSphere is installed on our virtual machine host, so we need to provide a driver for the Docker machine so that we can install the new VM on it.
--VMWAREVSPHERE-VCENTER=XXX.XXX.XXX.XXX \
--vmwarevsphere-username=root \
--vmwarevsphere-password=12345678 \
The above three lines specify the IP address, user name, 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 that are consumed by the newly created virtual machine.
Testvm
The last parameter is the name of the new virtual machine.
Soon the creation of the virtual machine is complete. Look at the VSphere client first:
A virtual machine named TESTVM is already running.
Then execute the docker-machine ls command to see:
The TESTVM is already visible, and its DRIVER is displayed as Vmwarevsphere.
Is that all right?
It seems to be wrong! Yes, what is the most important thing when we manually create a virtual machine? Is the image of the virtual machine installed! But here we do not specify the relevant things, so what exactly did Docker-machine install to us a system? When installing a virtual machine using the Vmwarevsphere driver, we cannot specify a VM image that we like (perhaps Docker machines is not ready). The default is to use a virtual machine image called Boot2docker, which is very small and only dozens of megabytes, so the installation will be quick.
Managing the remote Docker Client server model
Docker has been running in both client and server mode, except that the original version was started with the same binary file Docker to start the server-side daemon and client. In recent releases, the server executable has been separated from the client's executable file. To view the executable file in the/usr/bin directory:
Where Dockerd is the executable file that executes the server-side task. We usually perform native docker tasks by using Docker as a client command to send tasks to the server side of this machine.
Connect a remote server using a local client
So can local clients connect and send tasks to the remote Docker server side? Of course, it is possible, but we set it up a little bit more trouble manually. But it doesn't matter, Docker machine is ready for us! Let's look at how to run the container on the KRDEVDB host with a local Docker client:
$ docker-machine env KRDEVDB
The output of this command can be used as a command to set some environment variables that Docker clients use, allowing the native Docker client to communicate with the remote Docker server. Follow the instructions above to execute the command:
$ eval $ (docker-machine env KRDEVDB)
Well, in the current command-line terminal, the next Docker command that runs is the Docker daemon on the remote host KRDEVDB. To differentiate the native Docker daemon operation, we restarted a new command-line terminal and then executed the Docker PS command separately:
It is obvious from this that the local host and the remote host are running separate containers.
Manage Remote Docker daemon
In addition to running basic Docker commands, Docker machine can also manage remote Docker hosts. For example, we can start, close, and restart the remote Docker daemon by starting, stop, and restart commands, respectively. The situation here is slightly more complicated, and only the drivers that support these commands can perform the related operations. For example, we close Krdevdb and TESTVM separately:
The previous prompt generic driver does not support the Stop command. The TESTVM was installed through the Vmwarevsphere driver, so the stop was executed successfully.
For remote management, SSH support is essential! Docker machine, of course, has done its job responsibly:
$ docker-machine SSH Krdevdb
Execute the above command. Note that this command will not prompt you for a password, but will not allow you to configure SSH keys or anything, as Docker machine privately dirty the dirty work.
Summarize
The purpose of Docker machine is to simplify the installation and remote management of Docker. As we can see from the content of this article, Docker Machine does offer a lot of convenience for us to use and manage Docker. As far as improvements are concerned, Docker machine now installs the latest version of Docker, and I think it would be nice if I could support the installation of the Docker version.
About Docker Machine