Add remote CentOS to docker-machine

This tutorial shows how to add a remote CentOS to your local docker-machine.

Conditions

Preparation

The first step is to install docker on CentOS 7.

# update packages
$ yum update

# change into repository directory
$ cd /etc /yum.repos.d/

# add the docker repo
$ tee docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF

# install docker
$ yum install docker-engine

# start docker
$ service docker start

# check version
$ docker --version && docker images

Second step is to setup firewall rules on CentOS 7.

# change firewalld
$ firewall-cmd --permanent --zone=public --add-port=2376/tcp
$ firewall-cmd --reload

# check rules
$ firewall-cmd --list-all

The last step on CentOS 7 is to share ssh public key.

# create ssh key (local)
$ ssh-keygen -t rsa -b 4096

Add CentOS 7 server to docker-machine

# list docker-machines
$ docker-machine ls

# add remote to local docker-machine
$ docker-machine create -d generic --generic-ip-address <IP | Domain> --generic-ssh-key ~/.ssh/id_rsa --generic-ssh-user <user> myServer

# pointing shell
$ eval $(docker-machine env myServer)

# list docker-machines
$ docker-machine ls
...
NAME        ACTIVE   DRIVER       STATE     URL                SWARM   DOCKER    ERRORS
myServer      *      generic      Running   tcp://<ip>:2376    v1.11.1

Vagrant Plugin Recommendations

Today a few Vagrant Plugin Recommendations.

Preconditions

Preparation

Create a simple Vagrant BaseBox (as here) and if you like use the following Vagrantfile.

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "lupin/centos7"
  # config.vm.network "public_network"
  config.vm.synced_folder ".", "/vagrant", disabled: true
  # config.vm.synced_folder "./shared", "/vagrant"

  config.vm.provider "virtualbox" do |vb|
    vb.cpus = "2"
    vb.memory = "2048"
  end

  if Vagrant.has_plugin?("vagrant-hostsupdater")
    config.vm.network :private_network, ip: "10.0.0.10"
    config.hostsupdater.aliases = ["example.test"]
  end

  config.vm.provision "shell", inline: <<-SHELL
    sudo yum update -y
    sudo yum install -y vim docker
    sudo yum clean all
    sudo systemctl enable docker
    sudo systemctl start docker
    sudo setenforce 0
  SHELL

  config.vm.provision "docker" do |d|
    d.run "nginx", args: "-p 8080:80"
  end

  config.vm.provision "shell", inline: "sudo hostname -I"
end

1. vagrant-camera

# install camera plugin
$ vagrant plugin install vagrant-camera

# start VM via Vagrant
$ vagrant up

# do screenshot on current target folder
$ vagrant camera -s .

# show all pictures (Mac OS X)
$ open screenshot_default_*.png

2. vagrant-vbguest

# start VM via Vagrant
$ vagrant up

# ssh into VM (optional)
$ vagrant ssh

# check current version (optional)
$ VBoxControl --version
...
5.0.20r106931

# exit from VM (optional)
$ exit

# install vbguest plugin
$ vagrant plugin install vagrant-vbguest

# reload VM
$ vagrant reload

# ssh into VM (optional)
$ vagrant ssh

# list all downloaded (optional)
$ ls -l /opt
...
VBoxGuestAdditions-5.0.14
VBoxGuestAdditions-5.0.20

3. vagrant-hostsupdater

# install hostsupdater plugin
$ vagrant plugin install vagrant-hostsupdater

# check content of local hosts (optional)
$ cd /etc && cat hosts

# start VM
$ vagrant up

# or reload if already running
$ vagrant reload --provision

# check content of local hosts (optional)
$ cd /etc && cat hosts

# check status
$ vagrant ssh -c 'curl -v example.test:8080'

# stop VM
$ vagrant halt

# check content of local hosts (optional)
$ cd /etc && cat hosts

 

Create desktop environments on the fly

In this tutorial we will create desktop environments via docker on the fly. This environments could be used for development and/or testing purposes. For example you could expand it with Selenium-Grid nodes or provide manual testers all they need.

Preconditions

Steps

# create new VM
$ docker-machine create -d virtualbox xserver

# ssh into VM
$ docker-machine ssh xserver

# create Dockerfile
$ vi Dockerfile
FROM centos:centos7

MAINTAINER Lupin3000

RUN yum update -y
RUN yum install -y epel-release
RUN yum install -y x2goserver x2goserver-xsession
RUN yum groupinstall -y Xfce
RUN yum install -y firefox

RUN /usr/bin/ssh-keygen -t rsa -f /etc /ssh/ssh_host_rsa_key -N ''
RUN /usr/bin/ssh-keygen -t ecdsa -f /etc /ssh/ssh_host_ecdsa_key -N ''
RUN /usr/bin/ssh-keygen -t ed25519 -f /etc /ssh/ssh_host_ed25519_key -N ''

RUN adduser testuser
RUN echo 'testuser:test123' | chpasswd
RUN echo 'root:test123' | chpasswd

EXPOSE 22

CMD ["/usr/sbin/sshd", "-D"]

* Note: the space after etc is because of the security settings of my provider!

# build Docker image from Dockerfile
$ docker build -t centos7/xserver .

# run Docker container from image
$ docker run -p 2222:22 -d --name centos7-xserver centos7/xserver

Connect with x2go client

The following example shows the client configuration. Important are values ​​for host (192.168.99.100), port (2222) and session type (XFCE).

x2go client settings

Now it’s up to you to add more users, tools etc. – or to integrate everything into a build process.

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/

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