Docker Machine – Create VM

Working with Docker-machine

When working with containerized applications, important to be able to easily deploy them in the cloud, not only running them locally using Docker for Mac/Windows or from a Linux box locally.

The tool to be able to create a remote VM easily and manage those containers is called docker- machine.

Create a docker machine

 – Azure

This will create a virtual machine on Azure and install docker engine on it.

#!/usr/bin/env bash
set -e
MACHINE_NAME="VIRTUAL MACHINE NAME"
RESOURCE_GROUP="RESOURCE GROUP NAME"
SUBSCRIPTION="YOUR AZURE SUBSCRIPTION ID"
AZURE_LOCATION="eastus"
AZURE_VNET_NAME="VNET NAME"
docker-machine create --driver azure 
                      --azure-availability-set="MACHINE_NAME-as" 
                      --azure-subscription-id="${SUBSCRIPTION}" 
                      --azure-location "${AZURE_LOCATION}" 
                      --azure-open-port 80 
                      --azure-open-port 443 
                      --azure-size "${AZURE_MACHINE_SIZE}" 
                      --azure-subnet "${AZURE_VNET_NAME}-subnet" 
                      --azure-vnet "${AZURE_VNET_NAME}" 
                      --azure-resource-group "${RESOURCE_GROUP}" 
                        ${MACHINE_NAME}

VERY IMPORTANT:

docker-machine rm <machine-name>

This command will DELETE the Azure virtual machine and all related resources from your subscription! Use it with care.

 – VirtualBox

This will create a virtual machine on your local VirtualBox instance and install docker engine on it.

#!/usr/bin/env bash
set -e
MACHINE_NAME="MACHINE NAME"
docker-machine create --driver virtualbox ${MACHINE_NAME}

Generic

This will use the public identification key in order to connect to an existing machine (virtual or not) and install docker engine on it.

NOTE: This driver does not yet allow you to restart/shutdown the system.

#!/usr/bin/env bash
set -e
MACHINE_IP="MACHINE IP"
MACHINE_NAME="MACHINE NAME"
SSH_USER="MACHINE USERNAME"
SSH_PUBLIC_KEY="MACHINE USERNAME PUBLIC KEY PATH"
# If you did an ssh-copy-id to the machine: ~/.ssh/id_rsa
docker-machine create --driver generic 
--generic-ip-address=${MACHINE_IP} 
--generic-ssh-key ${SSH_PUBLIC_KEY}
--generic-ssh-user ${SSH_USER}
${MACHINE_NAME}

What esle is in – docker machine

Get a list of available machines:

docker-machine ls

NOTE: We’ll suppose the docker-machine is created, reachable and named demo-machine

Here is a simple docker-compose.yml example:

version: '2'
services:
web:
image: dockercloud/hello-world:latest
ports:
- "80:80"

Deploy containers locally

Start this hello world docker-compose locally:

docker-compose up -d

You can curl it locally and see the content of the index.html page:

curl localhost:80

Now stop and delete the created container of the locally started docker-compose:

docker-compose down

Deploy containers to a remote host

Let’s do the same thing but on the remote demo-machine.
Change the local docker environment variables to the demo-machine ones:

eval $(docker-machine env demo-machine)

You’re now targeting demo-machine when using any docker commands. To validate which docker-machine you point to, use this command:

$ docker-machine active
demo-machine

Start the same hello world docker-compose defined locally:

docker-compose up -d

Try to curl locally:

$ curl localhost:80
curl: (7) Failed to connect to localhost port 80: Connection refused

The docker commands are not run locally, they are run on the docker-machine we just configured! You must use the IP of the docker-machine itself as follow:

curl $(docker-machine ip demo-machine):80

SSH

Once the machine is created, it’s really easy to ssh into it because the SSH certificates are generated on the machine and kept locally:

docker-machine ssh demo-machine

Copy files to/from the machine

You can use SCP command to send/receive files to/from the machine.

Here is how to copy ~/localfile.txt into the home folder of the demo-machine

docker-machine scp ~/localfile.txt demo-machine:~/

Here is how to copy ~/remote.txt from the home folder of the demo-machine to the local home folder:

docker-machine scp demo-machine:~/removefile.txt ~/

Cleaning up the local docker environment

If you want to go back to your local instance:

eval $(docker-machine env -u)

To validate:

$ docker-machine active
No active host found

Needs Cleaning

 

docker volume list // volumes

docker container list
docker container list -a // Show switched off containers

docker ps
docker ps -a

docker create -v /db_datka –name db_datka postgres /bin/true

docker container run -d –volumes-from db_datka –name db2 postgres

docker exec -it db2 /bin/bash
docker start db2
docker start db_datka
docker exec -it db2 /bin/bash

 

docker container run -d db_datka
docker start -d db_datka
docker start db_datka
docker container run -d –volumes-from db_datka –name db2 postgres
docker start db2

 

docker volume create –name db_datka
docker volume list
docker start db2

 

docker container run -d -v db_datka:/var/lib/data postgres

docker container run -it –rm -p 80:8080 python

docker image list
docker container run -it –name my_container_ubuntu ubuntu:16.04 bash
docker container run -it –rm -p 90:8080 python

docker container run -it –volumes-from db_datka –rm -p 90:8080 python
docker container run -it –volumes-from db_datka –name my_container_ubuntu ubuntu:16.04 bash
docker rm my_container_ubuntu
docker container run -it –volumes-from db_datka –name my_container_ubuntu ubuntu:16.04 bash
docker rm my_container_ubuntu
docker container run -it –volumes-from db_datka:/var/www/db_datka –name my_container_ubuntu ubuntu:16.04 bash
docker container run -it –volumes-from db_datka –name my_container_ubuntu ubuntu:16.04 bash

clea

docker container run -d –name bd -e MYSQL_ROOT_PASSWORD=minha mysql

docker container run -d -p 91:80 –name appatest –link bd tutum/apache-php

docker exec -it appatest /bin/bash
docker container exec -it appatest ping db
docker container exec -it appatest ping bd
docker exec -it appatest /bin/bash
docker exec -it appatest ping bd
docker network create –driver bridge isolated_nw
docker network list
docker container run -itd –net isolated_nw alpine sh

docker container exec -it stoic_shaw /bin/bash
docker exec -it stoic_shaw /bin/bash
docker exec -it 1d5ded2e565f /bin/bash
docker exec -it 1d5ded2e565f /sh
docker exec -it 1d5ded2e565f sh
docker network inspect isolated_nw
which docker-compose
base=https://github.com/docker/machine/releases/download/v0.16.0 &&\n curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&\n sudo mv /tmp/docker-machine /usr/local/bin/docker-machine &&\n chmod +x /usr/local/bin/docker-machine
docker-machine version
docker-machine use
base=https://raw.githubusercontent.com/docker/machine/v0.16.0
for i in docker-machine-prompt-bash docker-machine-wrapper.bash docker-machine.bash\ndo\nsudo wget “$base/contrib/completion/bash/${i}” -P /etc/bash_completion.d\ndone
sudo vim ~/.zshrc
base=https://raw.githubusercontent.com/docker/machine/v0.16.0\nfor i in docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash\ndo\n sudo wget “$base/contrib/completion/bash/${i}” -P /etc/bash_completion.d\ndone
man docker-manifest
docker-machine version
docker-machine
docker-machine ls
docker-machine create –driver=virtualbox vbox-test
docker-engine ls
docker-engine list
docker-machine ls
docker-machine ss
docker-machine version
docker-machine ssh vbox-test
docker-machine stop vbox-test
docker-machine create –driver=virutalbox –virtualbox-disk-size 30000 teste-virtualbox
docker-machine create –driver=virtualbox –virtualbox-disk-size 30000 teste-virtualbox
docker-machine env teste-virtualbox
docker machine ls
docker-machine ls
cd .docker/machine/machines/teste-virtualbox/

cd teste-virtualbox

eval $(docker-machine env teste-virtualbox)
docker-machine ls
docker-machine ssh teste-virtualbox
docker-machine stop teste-virtualbox
docker machine rm teste-virtualbox

docker system prune
docker-machine ls
docker-machine start teste-virtualbox
docker-machine env
eval $(docker-machine env teste-virtualbox)
docker run busybox echo hello world

docker-machine stop teste-virtualbox

Was this article helpful?

Related Articles

Leave A Comment?