Notes
  • Index
  • Assorted articles, tools and links
    • Raspberry Pi
  • Terminal Extravaganza
    • kitty
    • zsh
    • vim
    • git
    • ssh
  • Web Dev
    • Ruby on Rails
      • JSON:API
      • Database
      • Active Storage
      • Testing
    • Postgres
    • Mongo
  • System Administration
    • New server setup
    • systemd
    • launchd
  • DevOps
    • Ansible
    • Packer
    • Docker
    • Terraform
Powered by GitBook
On this page
  • Use YAML instead of JSON
  • Docker images with Packer
  • Provisioning with Ansible

Was this helpful?

  1. DevOps

Packer

Packer is a tool to generate various kinds of deployable software images on all kinds of platforms, such as VirtualBox, AWS or GCP. The most useful (to me) portion of the software is generating Docker images using a more consistent format, integration with existing provisioning tools such as Ansible and smaller resulting images due to not keeping intermediary layers in the final image (although this can be alleviated with multi-stage builds with vanilla Docker).

Likewise, using Packer means not using Docker's image building component - Packer treats Docker as just another container runtime environment instead of a full suite of container utilities. This lets you hedge your bets slightly in case another hip and cool container engine comes along in a few years (MegaDocker or something like that).

Use YAML instead of JSON

As of version 1.4.2 (Aug 2019), Packer supports only JSON as its configuration format. HSL2 format support is reportedly in the works, but until it lands you might want to use a simple Ruby oneliner to write your configuration in YAML and then port it to JSON:

ruby -r json -r yaml -e 'puts(YAML.load($stdin).to_json)'

You can use a rudimentary {M,R}akefile, a Bash script or another task runner of your choice to simplify this.

yacker.sh
#!/bin/sh

# Usage: ./yacker.sh [yaml_configuration.yml] [packer_params]
# e.g.: ./yacker.sh packer.yml validate

YAML_FILE=$1
shift
PACKER_PARAMS=$*

CONVERTER="ruby -r json -r yaml -e 'puts(YAML.load(\$stdin).to_json)'"

eval "cat $YAML_FILE | $CONVERTER | packer $PACKER_PARAMS -"

This lets you write pretty YAML and convert it to ugly JSON on the fly. This also means that from here on (at least until HSL2 support is released) the examples in this article will be in YAML, not JSON, as it is much easier to write.

Docker images with Packer

Packer makes writing Dockerfiles much easier - it (almost) completely removes them from the equation.

builders:
- type: docker
  image: ubuntu
  commit: true
  changes:
    # You MUST specify an entrypoint and a command, otherwise this will
    # trip you up.
    - 'ENTRYPOINT ["/bin/bash"]'
    - 'CMD ["-c", "/usr/games/sl"]'
provisioners:
- type: shell
  inline:
  - apt-get update
  - apt-get install -y sl
post-processors:
- type: docker-tag
  repository: sample-image
  tag: latest

Note the following parts:

  • explicitly defining your ENTRYPOINT and CMD seems tedious, but ensures you won't inherit an unwanted entrypoint from the base image

  • you can use Packer's provisioning system to run arbitrary commands on the image you're building. This is shell in this example, but it could be anything - even (spoiler alert) Ansible!

  • you specifically instruct which tag to use in the configuration file, but you can also pull this from somewhere else (ENV variable, someplace else) using Packer variables

Building this configuration will leave you with a tagged image that you can then push to your favorite Docker registry. For bonus points, this could also be used to build a, say, Vagrant image with the same provisioners, so you don't have to re-do this if you want to target a different platform.

Provisioning with Ansible

TODO: write about this

PreviousAnsibleNextDocker

Last updated 5 years ago

Was this helpful?

This code is released under . Please use it in any way that you see fit, or don't at all!

Let's say you want to in your terminal using Docker. A simple Packer configuration to accomplis such an image might look like this:

CC0
look at pretty trains