Deploying your application to cloud using docker-machine

Problem

As a part of my investigation of what docker is, I want to do a simple and useful thing - deploy my application in a completely convenient manner. Let’s say, I’m using Digital Ocean as a cloud provider. Because my application is too little to think about complex deployment infrastructure, I’d like to be able to deploy everything from my laptop using digitalocean token.

Solution

I’ll try to deploy everything using docker-machine, together with digitalocean cloud provider. I’ll describe in step by step how to do this.

Create new machine

As simple, as it can be - I’ll create a new digitalocean instance. Open your terminal, and type following command.

docker-machine create \
-d digitalocean \
my-application

Few explanations, -d digitalocean means, that you will use digitalocean for deployment. Out of the box, digitalocean will use exports, that we set some minutes ago.
While we’re waiting till our console end up deploying to docker, let’s open our digitalocean droplets page, and see how it’s beatiful, we are using console, and ignore any ui cloud tools.

Connecting to docker

So, our console shows following

➜ queued-rest-api git:(master) ✗ docker-machine create \
> -d digitalocean \
> my-application
Running pre-create checks...
Creating machine...
(my-application) Creating SSH key...
(my-application) Creating Digital Ocean droplet...
(my-application) Waiting for IP address to be assigned to the Droplet...
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 debian...
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 my-application

Last line is the line that you should pay attention with - it.

To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env my-application

So, by running eval $(docker-machine env my-application) we can connect to our docker instance, right ? Let’s do it. There’s no input after running this command, so, don’t worry. Think about it, as about switching contexts. You switch your docker context to another machine, for this case, in the cloud. After command above, you are working with docker, which is located in the cloud, and can potentially, run your web application.

What’s next ?

So , we have connected to docker straightly and can start executing any commands. If we have some folder with our application, we need to write instructions about how to deploy it. It’s called Dockerfile.

Running our app inside docker instance

I’m not going to write anything about Dockerfile, feel free to investigate it by yourself. Let’s suppose, you have Dockerfile. So, go to your terminal, cd to folder, where Dockerfile is present and execute following command: