How to manage Docker images using Vagrant and integrate with Test kitchen

How to manage Docker images using Vagrant and integrate with Test kitchen


Purpose – To install vagrant and use it to manage docker images

Pre-requisites

CentOS 7 machine

Procedure

Step1: Install Vagrant which will be used for managing docker images


[root@desktop1 software]# wget https://releases.hashicorp.com/vagrant/1.9.6/vagrant_1.9.6_x86_64.rpm
--2017-07-07 01:43:31-- https://releases.hashicorp.com/vagrant/1.9.6/vagrant_1.9.6_x86_64.rpm
Resolving releases.hashicorp.com (releases.hashicorp.com)... 151.101.41.183, 2a04:4e42:a::439
Connecting to releases.hashicorp.com (releases.hashicorp.com)|151.101.41.183|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 78783791 (75M) [application/x-redhat-package-manager]
Saving to: ‘vagrant_1.9.6_x86_64.rpm’


100%[===========================================================================================================>] 78,783,791 26.4MB/s in 2.9s


2017-07-07 01:43:35 (26.4 MB/s) - ‘vagrant_1.9.6_x86_64.rpm’ saved [78783791/78783791]


[root@desktop1 software]# yum -y install vagrant_1.9.6_x86_64.rpm
Loaded plugins: langpacks, product-id
Examining vagrant_1.9.6_x86_64.rpm: 1:vagrant-1.9.6-1.x86_64
Marking vagrant_1.9.6_x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package vagrant.x86_64 1:1.9.6-1 will be installed
--> Finished Dependency Resolution


Dependencies Resolved


=====================================================================================================================================================
Package Arch Version Repository Size
=====================================================================================================================================================
Installing:
vagrant x86_64 1:1.9.6-1 /vagrant_1.9.6_x86_64 190 M


Transaction Summary
=====================================================================================================================================================
Install 1 Package


Total size: 190 M
Installed size: 190 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:vagrant-1.9.6-1.x86_64 1/1
Verifying : 1:vagrant-1.9.6-1.x86_64 1/1


Installed:
vagrant.x86_64 1:1.9.6-1


Complete!

Step2: Download the docker image that we would like to manage with vagrant


[root@desktop1 vagrantfiles]# vagrant box add tknerr/baseimage-ubuntu-14.04 --provider=docker
==> box: Loading metadata for box 'tknerr/baseimage-ubuntu-14.04'
box: URL: https://vagrantcloud.com/tknerr/baseimage-ubuntu-14.04
==> box: Adding box 'tknerr/baseimage-ubuntu-14.04' (v1.0.0) for provider: docker
box: Downloading: https://app.vagrantup.com/tknerr/boxes/baseimage-ubuntu-14.04/versions/1.0.0/providers/docker.box
==> box: Successfully added box 'tknerr/baseimage-ubuntu-14.04' (v1.0.0) for 'docker'!

Step3: Initialize the docker image to prepare the Vagrantfile


[root@desktop1 vagrantfiles]# vagrant init tknerr/baseimage-ubuntu-14.04
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Step4: Install docker community edition and start the docker service


This step is required to run the docker container

[root@desktop1 software]# yum -y install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm
[root@desktop1 software]# yum -y install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm
[root@desktop1 software]# docker --version
Docker version 17.03.2-ce, build f5ec1e2
[root@desktop1 vagrantfiles]# systemctl start docker

Step5: Now Start up the docker image that was downloaded earlier using vagrant


[root@desktop1 vagrantfiles]# vagrant up --provider=docker
Bringing machine 'default' up with 'docker' provider...
==> default: Creating the container...
default: Name: vagrantfiles_default_1499410674
default: Image: tknerr/baseimage-ubuntu:14.04
default: Volume: /root/stack/middleware/vagrantfiles:/vagrant
default: Port: 127.0.0.1:2222:22
default:
default: Container created: 564bf7b6a1e94609
==> default: Starting container...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!

Step6: Validate the Container that got generated


[root@desktop1 vagrantfiles]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
564bf7b6a1e9 tknerr/baseimage-ubuntu:14.04 "/bin/sh -c '/usr/..." 20 seconds ago Up 19 seconds 127.0.0.1:2222->22/tcp vagrant


[root@desktop1 vagrantfiles]# vagrant ssh
vagrant@564bf7b6a1e9:~$ hostname
564bf7b6a1e9
vagrant@564bf7b6a1e9:~$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
NAME="Ubuntu"
VERSION="14.04.5 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.5 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

Hope you enjoyed reading this article. Thank you.