docker-machine the easy way of administration

The docker-machine command offers very cool features in order to operate with Docker images/container. The first basic features I would like to present in this tutorial.

Preconditions

Preparation

# start VM
MBP:~ lupin$ docker-machine start apimock

# connect your shell to the machine
MBP:~ lupin$ docker-machine env apimock
MBP:~ lupin$ eval $(docker-machine env apimock)

# check status
MBP:~ lupin$ docker-machine ls

The output should now resemble the following example:

NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
apimock   *        virtualbox   Running   tcp://192.168.99.100:2376           v1.11.0

Start and access Docker container

docker-machine access container

… a little hint …

docker-machine xterm

… leave container without stopping …

docker-machine leave container

Copy files

docker-machine copy

How cool is that?

Vim syntax highlighting for Dockerfiles

By default Vim offers on the Mac OS X no syntax highlighting for Dockerfiles. With a few steps you can change that. At the end of this tutorial you can also install additional PlugIns!

Prepare .vimrc

# create or modify .vimrc
$ vim ~/.vimrc
set nocompatible
filetype off

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" add plugins here...
Plugin 'VundleVim/Vundle.vim'
Plugin 'docker/docker' , {'rtp': '/contrib/syntax/vim/'}

call vundle#end()
filetype plugin indent on

set ruler
syntax enable

Install and enable PlugIns

# start Vim
$ vim

# install and enable Vim PlugIns
:PluginInstall

# quit vim
:q

# show installed Vim PlugIns (optional)
$ ls -la ~/.vim/bundle/

Share Docker Images and Container

If you want to test dockerized applications, you need to export/import Docker images or Docker containers. Therefor Docker offers various options and commands. In this tutorial i show some possibilities without Docker Hub or Docker registry.

Preconditions

Docker save and load

# show Docker images
docker@apimock:~$ docker images

# save Docker image (include parent layers, tags, versions etc.)
docker@apimock:~$ docker save api_image:latest | gzip -vc > api_image.tar.gz

# download archive from apimock
MBP:~ lupin$ scp docker@<192.168.99.100>:/home/docker/api_image.tar.gz ~/Downloads/

# create new Boot2Docker VM
MBP:~ lupin$ docker-machine create -d virtualbox apimock2

# upload archive to apimock2
MBP:~ lupin$ scp ~/Downloads/api_image.tar.gz docker@<192.168.99.101>:/home/docker/

# load archive into image
docker@apimock2:~$ cat api_image.tar.gz | docker load

# show Docker images
docker@apimock2:~$ docker images

# show Docker image history
docker@apimock2:~$ docker history api_image

# create and start Docker container
docker@apimock2:~$ docker run --name api_container -d -p 8888:8888 api_image

Docker export and import

# show Docker containers
docker@apimock:~$ docker ps -a

# export Docker container (could running)
docker@apimock:~$ docker export api_container | gzip -vc > api_container.tar.gz

# download archive from apimock
MBP:~ lupin$ scp docker@<192.168.99.100>:/home/docker/api_container.tar.gz ~/Downloads/

# upload archive to apimock2
MBP:~ lupin$ scp ~/Downloads/api_container.tar.gz docker@<192.168.99.101>:/home/docker/

# stop and delete previous container
docker@apimock2:~$ docker kill api_container && docker rm api_container

# delete previous images
docker@apimock2:~$ docker rmi api_image

# import from archive
docker@apimock2:~$ cat api_container.tar.gz | docker import - api_image/import

# show Docker images
docker@apimock2:~$ docker images

# show Docker image history
docker@apimock2:~$ docker history api_image/import

# create and start Docker container (CMD needed)
docker@apimock2:~$ docker run --name api_container -d -p 8888:8888 api_image/import mock-server --address=0.0.0.0 --dir=api

Attention: Please note each output for “$ docker history…” !!!!

Create REST API mock server with Docker

This time again a tutorial with various instructions. It is a REST API services for development and testing purposes and some simple Docker instructions.

Preconditions

Note: For Mac OS X and Windows use Docker Toolbox!

Create and connect into Boot2Docker VM

# create new boot2docker vm
$ docker-machine create -d virtualbox apivm

# list created vm(s)
$ docker-machine ls

# list informations (optional)
$ docker-machine inspect apimock

# ssh into boot2docker vm
$ docker-machine ssh apivm

Create Dockerfile (inside VM)

# create new Dockerfile
$ vi Dockerfile
FROM ubuntu

# install python packages
RUN apt-get update && apt-get -y install python python-dev python-pip

# install python libraries
RUN pip install mock-server tornado==4.2

# create directory
RUN mkdir -p api

EXPOSE 8888

CMD ["mock-server","--address=0.0.0.0","--dir=api"]

Create Docker image and container (inside VM)

# create Docker image
$ docker build -t api_image .

# list Docker image(s)
$ docker images

# create and start new container from Docker image
$ docker run --name api_container -d -p 8888:8888 api_image

# list Docker container(s)
$ docker ps -a

Run application in browser

Now open a browser and call URL like: http://<192.168.99.100>:8888/__manage. You can now begin to create and use REST API resources.

Expand Boot2Docker – Part2

The configuration of the Boot2Docker VM, still has a few drawbacks. You can not permanently store, the root password must be set again, SSH keys always changing … and so on. In this part we will change that.

Preconditions

Create and connect HDD

# change to Default Machine Folder
$ cd VirtualBox\ VMs/

# create storage medium for VM
$ VBoxManage createhd --filename ./Boot2DockerVM/disk.vdi --size 8000

# attache storage medium to VM
$ VBoxManage storageattach "Boot2DockerVM" --storagectl "IDE" --port 1 --device 0 --type hdd --medium ./Boot2DockerVM/disk.vdi

# reconfigure boot settings of VM
$ VBoxManage modifyvm "Boot2DockerVM" --boot1 dvd --boot2 disk --boot3 none --boot4 none

# start vm
$ VBoxManage startvm Boot2DockerVM --type gui

Prepare disk

# format disk
$ mkfs.ext4 /dev/sda

# mark filesystem
$ tune2fs -L boot2docker-data /dev/sda

# restart VM
$ reboot

Set permanent root password

# check disk
$ df -h

# change directory
$ cd /mnt/sda/var/lib/boot2docker/

# create bootlocal.sh
$ vi bootlocal.sh

# content of bootlocal.sh (please use your own password)
echo "root:test123" | chpasswd

# change access rights
$ chmod +x bootlocal.sh

From this point, you can connect via SSH as root user.

Create Boot2Docker VirtualBox VM

Boot2Docker is perfect to test Docker Images/Container. This guide shows the fastest and easiest way to create a Boot2Docker VirtualBox VM.

Preconditions

Download Boot2Docker ISO

Download the necessary ISO Version from GitHub. In this tutorial I use the “Default Machine Folder”.

Boot2Docker Release

Prepare and start VM

# create new VirtualBox VM
$ VBoxManage createvm --name "Boot2DockerVM" --ostype Linux_64 --register
 
# configure system settings of VM
$ VBoxManage modifyvm "Boot2DockerVM" --memory 2048 --cpus 2 --acpi on --pae on --hwvirtex on --nestedpaging on
 
# configure boot settings of VM
$ VBoxManage modifyvm "Boot2DockerVM" --boot1 dvd --boot2 none --boot3 none --boot4 none 

# configure basic settings
$ VBoxManage modifyvm "Boot2DockerVM" --vram 8 --audio none --accelerate3d off --accelerate2dvideo off --usb off

# configure network settings
$ VBoxManage modifyvm "Boot2DockerVM" --nic1 nat --nic2 bridged --bridgeadapter2 <devicename>

# Example MacBook Pro
$ VBoxManage modifyvm "Boot2DockerVM" --nic1 nat --nic2 bridged --bridgeadapter2 'en0: WLAN (AirPort)'

# modify a storage controller
$ VBoxManage storagectl "Boot2DockerVM" --name "IDE" --add ide
 
# add boot2docker iso
$ VBoxManage storageattach "Boot2DockerVM" --storagectl "IDE" --port 0 --device 0 --type dvddrive --medium boot2docker.iso

# start VM
$ VBoxManage startvm Boot2DockerVM

SSH into running VM

# ssh into VM (Password: tcuser)
$ ssh docker@<ip>

If all goes well …

                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.10.3, build master : 625117e - Thu Mar 10 22:09:02 UTC 2016
Docker version 1.10.3, build 20f81dd