Building a Docker Image with Packer

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

Packer allows us to create machine images using a number of builders — including Docker images! Built _without_ the use of a Dockerfile, using Packer with Docker requires the use of simple scripts and some metadata changes to get everything up and running. In this lab, we’ll do just that by creating a Docker image to deploy a Node.js application.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Refactor the Dockerfile

Packer provisions Docker images using the same provisioners as any other image. That said, not all steps in the build will work one-to-one.

Additionally, the follow Dockerfile instructions can be provided as metadata in the build itself:

  • USER
  • ENTRYPOINT
  • WORKDIR
  • EXPOSE
  • CMD

These will run as intended with a container launched from the image.

We’ll refactor our template to use the file and shell provisioners to create our script.

Write the Docker Builder Block

Write the builder block for the Docker builder, and add the relevant post-processors.

Test the Build
  1. Validate the template.
  2. Test the build.

Additional Resources

You are a DevOps Engineer in charge of automating all builds. The team in charge of the to-do list application Forethought has been using Dockerfiles to generate their images thus far, but you wish to bring this process under your team using the standardized tool for image creation, Packer.

The application has been provided at /home/cloud_user/packer/forethought. Take the existing setup and reconstruct it to use Packer.

FROM node:10-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
RUN npm config set registry http://registry.npmjs.org/
RUN npm install
COPY --chown=node:node . .
USER node
EXPOSE 8080
CMD [ "node", "index.js" ]

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?