Not too long ago I wrote about containerizing a bunch of web applications and putting them behind an NGINX reverse proxy. This is because I’ve been exploring the possibility of taking all my personal applications and turning them into Docker containers for easy maintenance and portability. I currently use Digital Ocean and if I had to guess, I’m going to be using it for a lot longer as it is a great service. So what does it take to get containerized applications on Digital Ocean or any other remote machine?
We’re going to take a look at creating and defining a remote machine in Docker and deploying containers on it.
While I’m going to be making many Digital Ocean references in this example, you don’t need to use it. You essentially only need a server you can SSH into.
To keep things generic, we need a standard VPS with SSH, not a pre-provisioned VPS with Docker. Not all server providers offer Docker instances, so we’re not going to depend on it. To be successful the remote server needs to have a privileged account that doesn’t require a password. For this example, the root user will be fine, but in production you should probably disable the root user and create a different account accessible via a public and private key combination.
A locally running instance of Docker is a requirement to be successful. With a VPS up and running, the local Docker client must connect to the machine and provision it. This can be done by executing the following:
docker-machine create --driver generic --generic-ip-address xxx.xxx.xxx.xxx --generic-ssh-key /path/to/private/key digitalocean
The above command will use the Generic driver for Docker which depends on SSH. Provide the IP address of your remote server and the path to the SSH private key that you’ll be using. There are other possible flags, but for this example we should be fine. The
digitalocean name is what I wish to call it in my list of Docker machines.
Once the command finishes, you can view it in your list of machines via the following command:
Although we’ve added the server, a Digital Ocean VPS in my case, it is not the default machine. By default, any Docker commands will be executed locally, or on my Mac in my case. Execute the following to choose a new default:
eval $(docker-machine env digitalocean)
From here on out, any container you deploy will be deployed to the remote instance. You can use the same command to switch machines, but if you wish to switch back to your local machine, execute the following:
eval $(docker-machine env -u)
It is very easy to manage remote Docker hosts like this because you don’t need to first SSH into your remote machine.
You just saw how to connect your local Docker client to a virtual private server or any other remote machine that allows for SSH connections. In my scenario I’m using a Digital Ocean VPS, but it really doesn’t matter what you use. Being able to use your local Docker client is an added convenience that removes the need to first SSH into the remote machine.
Stay up to date on the latest in web, mobile, and game development, plus receive exclusive content by subscribing to The Polyglot Developer newsletter.