Every hosting company now provides a way for you to auto-create docker server using a one-click template they create. While this is an excellent way to set up your servers fast, I like to create my own using the base image for the operating system and then install Docker manually. I know this is insane, why anyone would spend more time on something that can be done quickly and fast!. To answer your question, let me show you how I'll create a swarm cluster with 3 servers, then feel free to decide if it is a waste of time or not.
I'll assume that you already installed Docker Desktop on your machine, which will install docker engine and other tools
docker-machine too. Also, I'll assume that you have created 3 servers on DigitalOcean "Referral link".
The reason that I am not going to talk about how to create the three servers is that DigitalOcean makes it so simple to create three identical one, if you still don't know how you can check DigitalOcean Documentations.
Looking for new opportunities
Before I dive more into the subject, Right now, I am looking for a new opportunities there for if you are looking for someone to join your team or you have something you want some help with feel free to reach out via email zaher[@]zah.me.
Once we got all the servers up and running, we need to update them and install all the required libraries, but using
docker-machine such thing is as simple as one, two, three. Let me explains:
First, we need to ask
docker-machine to create a new docker server for us on every server we created, using the generic driver. Moreover, yes, I know that DigitalOcean has it's own driver but using the
generic driver can be used with any hosting company.
From your local terminal, run the following command:
docker-machine create \ --driver=generic \ --generic-ip-address=IP_ADDRESS \ --generic-ssh-user=USERNAME \ --generic-ssh-key=ABSOLUTE_PATH_TO_SSH_KEY \ --generic-ssh-port=PORT \ MACHINE_NAME
As you can see, I have put the generic command that you can use everywhere, all you have to do is to replace them with
the values that you want, but the
MACHINE_NAME is what you want to call your machine and can be anything, for us I'll
also assume they are
master-01, master-02, master-03, but remember they need to be unique for your local docker machine.
Once you execute the command, you will get an output similar to:
Running pre-create checks... Creating machine... (master-01) Importing SSH key... Waiting for machine to be running, this may take a few minutes... Detecting operating system of created instance... Waiting for SSH to be available... Detecting the provisioner... Provisioning with ubuntu(systemd)... Installing Docker... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Checking connection to Docker... Docker is up and running! To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env master-01
Moreover, just by that, we have set up a docker server and installed Docker on it, without even ssh to the server by the way.
Now by running
$ docker-machine env master-01 you will get a message like this one:
Run this command to configure your shell: eval $(docker-machine env master-01)
Which says, run
eval $(docker-machine env master-01) and your local docker commands will be executed on
You can ssh to one of the servers and verify that
docker was installed successfully there. Then exit and run the
eval command to make sure you are on the correct place.
Setting up Docker Swarm
Now that we have installed
docker on all our machines, we can start building our swarm and join them together.
Let us verify that you have all your servers controlled by
docker-machine by running the following command:
And you will get an output similar to this one:
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS master-01 * generic Running tcp://126.96.36.199:2376 v18.09.6 master-02 - generic Running tcp://188.8.131.52:2376 v18.09.6 master-03 - generic Running tcp://184.108.40.206:2376 v18.09.6
Since we have run the
eval command on
master-01 we got an asterisk to indicate that it is the active server.
To initiate the swarm, I run the following command:
docker swarm init --advertise-addr 220.127.116.11
As you can see I am using the IP address for my first master machine to advertise for the swarm. And, the result is:
Swarm initialized: current node (ehu5r8rt5j5po5leawg17vp34) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-0fwczwq4eubfz7nrh6eqwcm019u1a1bvvj8j380hu1vkh1dlhs-bfa8riw3m0tu4ccc4ieurwv9i 18.104.22.168:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
However, since the plan is to create a swarm of 3 managers, am not going to use the worker token they just provided, but I'll
docker swarm join-token manager and use the output on both
Now that I am nearly done, let's see our swarm node list, we can do that by running
docker node ls and the result I got is:
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION ehu5r8rt5j5po5leawg17vp34 * master-01 Ready Active Leader 18.09.6 phxuewignzclp04thf6d9ybyy master-02 Ready Active Reachable 18.09.6 z371k5n0f7vxehx78in0uuw7m master-03 Ready Active Reachable 18.09.6
As you can see, I have the managers up and ready to be used in my infrastructure.
I barely touched how you can use
docker-machine to manage your docker servers, but you can look at the documentation
to read more about it, and see what else you can do with it.
Just a note to not confuse anyone, you can use
docker-machine for single docker server as well, so think that it is only
to manage your swarm. My article is to show you powerful it is.