Packer to Create Docker

It is simple to create Docker from Packer. In this example I will show you how to create open JDK 8 installed Docker image.

First you have to create a packer.json file:

{
  "builders": [
    {
      "name": "docker",
      "type": "docker",
      "image": "centos:7",
      "commit": true
    }
  ],

  "provisioners": [
    {
      "type": "ansible",
      "playbook_file": "playbook.yaml"
    }
  ],

  "post-processors": [
    {
      "type": "docker-tag",
      "repository": "ojitha/java8",
      "tag": "0.0.1"
    }
  ]
}

Here the playbook.yaml which will provision the docker instance:

---
- hosts: all
  tasks:
    - name: message
      debug: msg="Container - {{ inventory_hostname }} running {{ ansible_distribution }}"
    - name: install the latest version of open jdk
      yum:
        name: java-1.8.0-openjdk-devel
        state: latest

Now run the following command to create the docker image:

packer build packer.json

after image has created, use the following command to run the container interactively:

docker run -it ojitha/java8:0.0.1

Install Oracle JDK 8

First you need to download the oracle jdk 8 to the docker image, scripts/download.sh :

#!/bin/bash
yum -y install wget
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2Ftechnetwork%2Fjava%2Fjavase%2Fdownloads%2Fjdk8-downloads-2133151.html; oraclelicense=accept-securebackup-cookie;" "http://download.oracle.com/otn-pub/java/jdk/8u171-b11/512cd62ec5174c3487ac17c61aaa89e8/jdk-8u171-linux-x64.rpm" -O jdk-8u171-linux-x64.rpm

Now you need to run this in the packer.jason provisioner

{
  "builders": [
    {
      "name": "docker",
      "type": "docker",
      "image": "centos:7",
      "commit": true
    }
  ],

  "provisioners": [
    {
      "type": "shell",
      "script": "scripts/download.sh"
    },
    {
      "type": "ansible",
      "playbook_file": "playbook.yaml"
    }
  ],

  "post-processors": [
    {
      "type": "docker-tag",
      "repository": "ojitha/java8",
      "tag": "0.0.1"
    }
  ]
}

Here the playbook.yaml where you can install oracle JDK 8 from rpm:

- hosts: all
  tasks:
    - name: message
      debug: msg="Container - {{ inventory_hostname }} running {{ ansible_distribution }}"
    - name: install the latest version of oracle jdk 8
      yum:
        name: /jdk-8u171-linux-x64.rpm
        state: present

Now you have to run the same commands to create to image and run the container. Here are some basic docker commands to remember:

To check the java version, run the following command

docker run --rm ojitha/java8:0.0.1 /usr/bin/javac -version

Above command will remove the container after display the version.

you can run the container as dev

docker run -idt --name dev --hostname=javahost  ojitha/java8:0.0.1 /bin/bash

To find the stats of the dev container: This is an important command.

docker stats dev

To connect to the container

docker attach dev

To list see the pids, type top command. As a result of the above command you will find the /bin/bash as the PID=1. That means if you exit from the bash container will stop.

To start the container again:

docker start dev

You can run the following command to find the java version

docker exec dev /usr/bin/java -version

you can find the container file to hosting computer folder as follow

docker cp dev:/etc/hostname hostname.txt

to remove all containers

docker rm $(docker ps -aq)

To run the container with dd command:

docker run -idt --name dev --hostname=javahost  ojitha/java8:0.0.1 dd if=/dev/zero of=/dev/null

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python