Operations 11 min read

How to Deploy Docker Across Multiple Hosts with Docker‑Machine: Step‑by‑Step Guide

This tutorial explains how to use Docker‑Machine to batch‑install Docker on multiple Ubuntu hosts, covering environment preparation, SSH key setup, Docker‑CE installation via Alibaba Cloud mirrors, creating generic drivers, configuring remote daemon access, and leveraging bash completion for smoother management.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Deploy Docker Across Multiple Hosts with Docker‑Machine: Step‑by‑Step Guide

Batch installing Docker on hosts can be done efficiently with Docker‑Machine.

Docker‑Machine supports installing and configuring Docker hosts in various environments such as regular Linux OS, virtualization platforms (VirtualBox, VMware), and public clouds (AWS, Azure, GCE).

Experimental Environment Description

Operating System Version

IP

Configuration

Role

Ubuntu 18.04.4 LTS

172.16.89.101

4‑core 8 GB

machine

Ubuntu 18.04.4 LTS

172.16.89.100

4‑core 8 GB

node1

Ubuntu 18.04.4 LTS

172.16.89.99

4‑core 8 GB

node2

Official project URL: https://github.com/docker/machine/releases (latest version v0.16.2).

Installation is simple; run:

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

After installation, verify with docker-machine ls.

Since Docker‑Machine needs password‑less SSH to remote hosts, generate an SSH key on the machine node and copy it to node1 and node2:

ssh-keygen
ssh-copy-id 172.16.89.100
ssh-copy-id 172.16.89.99

Start Installation

Create Docker on node1 using the generic driver:

docker-machine create --driver generic --generic-ip-address=172.16.89.100 node1

The same command works for node2:

docker-machine create --driver generic --generic-ip-address=172.16.89.99 node2

If the default Docker‑CE image source is slow, install Docker‑CE on the nodes first using Alibaba Cloud mirrors:

# step 1: install required tools
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: add GPG key
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# step 3: add repository
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# step 4: install Docker‑CE
sudo apt-get -y update
sudo apt-get -y install docker-ce

After Docker‑CE is installed on node1 and node2, continue with Docker‑Machine creation. Verify the machines:

docker-machine ls
NAME   ACTIVE DRIVER   STATE   URL                                 SWARM DOCKER   ERRORS
node1  -      generic Running tcp://172.16.89.100:2376 v19.03.8
node2  -      generic Running tcp://172.16.89.99:2376  v19.03.8

Check the Docker daemon configuration on a node:

vim /etc/systemd/system/docker.service.d/10-machine.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock --storage-driver overlay2 --tlsverify --tlscacert /etc/docker/ca.pem --tlscert /etc/docker/server.pem --tlskey /etc/docker/server-key.pem --label provider=generic
Environment=

The -H tcp://0.0.0.0:2376 option enables remote Docker daemon access, and the --tls* flags secure the connections.

Better Experience

Install Bash completion scripts from https://github.com/docker/machine/tree/master/contrib/completion/bash and place them in /etc/bash_completion.d. Add the following to $HOME/.bashrc:

source /etc/bash_completion.d/docker-machine-prompt.bash
source /etc/bash_completion.d/docker-machine.bash
source /etc/bash_completion.d/docker-machine-wrapper.bash
PS1='[\u@\h \W$(__docker_machine_ps1 " [%s]")]\$'

Use docker-machine env node1 to display environment variables for a node, then run: eval $(docker-machine env node1) After evaluation, all Docker commands run on the current shell are executed on node1, e.g.: docker pull nginx:alpine The image is stored on node1, not on the machine node.

Docker‑Machine Subcommands

Subcommand

Effect

active

Show active node

config

View machine daemon config

create

Create a machine

env

Display node environment variables

ls

List node status

stop/start/restart

Control the OS of a node

scp

Copy files between nodes

upgrade

Upgrade Docker on a node

docker-machine scp node1:/tmp/a.txt node2:/tmp/

Summary

Using Docker‑Machine to deploy Docker in a multi‑host environment greatly improves efficiency, but the default foreign image source can be slow; for better performance, pre‑install Docker‑CE with a fast mirror (or use Ansible for large fleets) before creating machines.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Bashdocker-machinemulti-host deployment
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.