create docker swarm

In this tutorial I will show how easy it is to create a Docker Swarm.

Precondition

Instructions

# create a new cluster identifier
$ docker-machine create -d virtualbox local
$ eval "$(docker-machine env local)"

# generate discovery token
$ docker run --rm swarm create

Copy the resulting value for <TOKEN-ID>. The following commands will create a Swarm cluster.

# create swarm master
$ docker-machine create -d virtualbox --swarm --swarm-discovery token://<TOKEN-ID> --swarm-master swarm-manager

# create swarm nodes
$ docker-machine create -d virtualbox --swarm --swarm-discovery token://<TOKEN-ID> swarm-node-01
$ docker-machine create -d virtualbox --swarm --swarm-discovery token://<TOKEN-ID> swarm-node-02
$ docker-machine create -d virtualbox --swarm --swarm-discovery token://<TOKEN-ID> swarm-node-03

# delete unused cluser identifier vm
$ docker-machine rm local

# pointing shell to swarm master
$ eval $(docker-machine env --swarm swarm-manager)

# show information
$ docker-machine ls
$ docker info

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?

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

 

VirtualBox forward ports below 1024

The VirtualBox online-manual writes “Forwarding host ports < 1024 impossible…”. This tutorial shows that it is still possible. I know that there are several reasons not to do so, but there are also reasons where it is necessary.

Conditions

  • VirtualBox with VM configured (not running)
  • You know the root password

Example for Ubuntu

# become root
$ sudo su -

# start VirtualBox Manager
$ virtualbox

Now you probably need to import the VM. Attention! The acces rights will be changed!

Port Forwarding

Configure or just check the port forwarding. To do so, you can use the graphical port forwarding editor or even the command line (VBoxManage).

VirtualBox port forwarding editor

At the end you can start the VM as root.

Create Windows 10 Vagrant Base Box

In the first part I have shown how to create the Windows 10 VirtualBox VM. This time I will show you how to create a Vagrant Base Box.

Preconditions

Important Windows 10 Settings

Turn off and disable UAC (here you will find different ways)

disable windows 10 uac

Enable Remote Desktop

remote desktop settings

Configure WinRM on Windows

open the Command Prompt as Admin

> winrm quickconfig -q
> winrm set winrm/config/winrs @{MaxMemoryPerShellMB="300"}
> winrm set winrm/config @{MaxTimeoutms="1800000"}
> winrm set winrm/config/service @{AllowUnencrypted="true"}
> winrm set winrm/config/service/auth @{Basic="true"}
> sc config WinRM start=auto

Optional Settings for Windows

open the PowerShell as Admin

# remove all of the metro apps
> Get-AppXPackage -AllUsers | Remove-AppXPackage

# remove log files
> Get-Childitem "C:\Windows\Logs\dosvc" | Remove-Item -Verbose

# disables the system restore feature
> Disable-ComputerRestore c:

# disable hibernation
> powercfg -h off

# allow Powershell scripts to provision
> Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Okay,… that is all. Now shutdown windows…

Create Vagrant BaseBox

# goto default directory
$ cd VirtualBox\ VMs/

# create base box from VM
$ vagrant package --base Win10x64 --output Win10x64.box

# add box
$ vagrant box add lupin/windows10 Win10x64.box

# check vagrant boxes
$ vagrant box list

Create and run test project

# create project folder
$ mkdir ~/test_project && cd ~/test_project

# initializes to be a Vagrant environment
$ vagrant init lupin/windows10

# edit Vagrantfile
$ vim Vagrantfile

# start VM
$ vagrant up

# start rdp client
$ vagrant rdp
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|

  config.vm.box = "lupin/windows10"
  config.vm.guest = :windows
  config.vm.communicator = "winrm"
  config.winrm.username = "vagrant"
  config.winrm.password = "vagrant"
  config.vm.boot_timeout = 600
  config.vm.network :forwarded_port, guest: 3389, host: 3389
  config.vm.network :forwarded_port, guest: 5985, host: 5985, id: "winrm", auto_correct: true

  config.vm.provider "virtualbox" do |vb|
    # vb.gui = true
    vb.memory = "2048"
    vb.cpus = 2
    vb.name = "Windows_Vagrant"
  end

end

Values for username and password should match your needs!