Skip to content

Latest commit

 

History

History
68 lines (49 loc) · 2.14 KB

File metadata and controls

68 lines (49 loc) · 2.14 KB

Base Image Example - Docker Ubuntu

This example uses packer to build a docker image based on the ubuntu:xenial docker image.

The docker-ubuntu.pkr.hcl file is the packer file that is used by packer to build the image.

The packer.required_plugins section outlines what plugins to use to build the image.

In this example the docker plugin version 0.0.7 and greater will be used to build the image.

packer {
  required_plugins {
    docker = {
      version = ">= 0.0.7"
      source  = "github.com/hashicorp/docker"
    }
  }
}

The source section is used to configure the build plugin's configuration. In this example docker is the builder plugin that is being used.

The code below creates a new build source named docker.ubuntu that configures the docker build plugin to use the ubuntu:xenial image as a base for the new image.

The commit=true statements tells the docker plugin to create containter locally on the user's machine.

source "docker" "ubuntu" {
  image  = "ubuntu:xenial"
  commit = true
}

The source "docker" "ubuntu" statement tells packer to use the docker build type and label the source as docker.ubuntu.

The build section defines what should be done with the container after it is launch.

Normally this is when steps like provisioning the container happen but in this example there is no provisioner setup.

build {
  name = "learn-packer"
  sources = [
    "source.docker.ubuntu"
  ]
}

How to build the image

Run the commands below to build the image:

  1. packer init .
    • This command will download all the plugins listed in the required_plugins sections
  2. packer build docker-ubuntu.pkr.hcl
    • This command will build the image
  3. docker images
    • Verify the image was created by using the docker images command to list the images and verify the newly created image is in the list

Note: Packer doesn't the manage the images so you will have to remove them manually using the docker rmi IMAGE_ID command.

Other Useful Commands

  1. packer fmt .
    • Formats the .hcl file
  2. packer validate .
    • Checks the .hcl file for any invalid configs