How to manage Docker images using Vagrant

How to manage Docker images using Vagrant

vagrant_docker_manage

Here in this article we will try to install vagrant and use it to manage docker images.

Test Environment

CentOS 7 machine

What is Vagrant

Vagrant enables users to create and configure lightweight, reproducible, and portable development environments.

Procedure

Step1: Install Vagrant

Here we will install Vagrant tool using the RPM package which can be used to manage docker images.

sudo wget https://releases.hashicorp.com/vagrant/1.9.6/vagrant_1.9.6_x86_64.rpm
sudo yum -y install vagrant_1.9.6_x86_64.rpm

Step2: Download the docker image

Now let’s download a box provided by docker provider using the below command.

sudo vagrant box add tknerr/baseimage-ubuntu-14.04 --provider=docker

Output:

==> 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

Here we will initialize the docker image to prepare the Vagrantfile as shown below.

sudo vagrant init tknerr/baseimage-ubuntu-14.04

Output:

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 and Start Docker CE Service

This step is required to run the docker container.

sudo 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
sudo 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
sudo systemctl start docker

Step5: Start Docker Image

It’s time to start up the box that we downloaded using the below command.

sudo vagrant up --provider=docker

Output:

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

sudo docker ps

Output:

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

We can try to ssh to the docker container using “vagrant ssh” and validate its hostname and distribution as shown below.

sudo 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..