Using Packer to Create a Docker Image

1 hour
  • 3 Learning Objectives

About this Hands-on Lab

In this hands-on lab, you are a DevOps engineer working for ABC Solutions. For the last month, your team has been working to complete a dev project for a customer. They are ready to deploy the app to AWS, but the client has come back with a last-minute request. They would like to have the app Dockerized.

You have been given the login info for their Docker server. You will use Packer to build the Docker image and then create a container using the image.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a Packer File

Create a packer.json file with the following contents:

{
  "variables": {
    "repository": "la/express",
    "tag": "1.0"
  },
  "builders": [
    {
      "type": "docker",
      "author": "Travis Thomsen",
      "image": "node",
      "commit": true,
      "changes": [
        "EXPOSE 3000"
      ]
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "apt-get update -y && apt-get install curl -y",
        "mkdir -p /var/code",
        "cd /root",
        "curl -L https://github.com/linuxacademy/content-nodejs-hello-world/archive/v1.0.tar.gz -o code.tar.gz",
        "tar zxvf code.tar.gz -C /var/code --strip-components=1",
        "cd /var/code",
        "npm install"
      ]
    }
  ],
  "post-processors": [
    {
      "type": "docker-tag",
      "repository": "{{user `repository`}}",
      "tag": "{{user `tag`}}"
    }
  ]
}
Build a Docker Image
  1. Build the Docker image using the Packer file:

    packer build --var 'repository=la/express' --var 'tag=0.0.1' packer.json
  2. Verify the image was created:

    docker images
Create a Docker Container
  1. Build the Docker container:

    docker run -dt -p 80:3000 la/express:0.0.1 node /var/code/bin/www
  2. Verify the Docker container started:

    docker ps
  3. Verify everything is working correctly:

    curl localhost

Additional Resources

  1. In the root directory, create a file called packerfile.json, and configure the following:

    • Create a variable called repository. The default values should be la/express.
    • Create a variable called tag. The default value should be 1.0.
    • The file should use the Docker builder.
    • The type should be docker.
    • Set the author to your name.
    • Use the node image.
    • Set commit to true.
    • Expose port 3000.
  2. Create an inline shell provisioner. The provisioner will need to execute an apt-get update and install curl:

    • Create a directory call code in /var.
    • Use curl to download the application .tar file to root.
      curl -L https://github.com/linuxacademy/content-nodejs-hello-world/archive/v1.0.tar.gz -o code.tar.gz
    • Untar the file to /var/code. tar zxvf code.tar.gz -C /var/code --strip-components=1
    • Go to /var/code and execute an npm install.
  3. Create a docker-tag post-processor:

    • Set the repository to use the repository variable.
    • Set the tag to use the tag variable.
  4. Validate the packerfile.json file.

  5. Execute packer build.

  6. Start a Docker container by executing.

    docker run -dt -p 80:3000 la/express:0.0.1 node /var/code/bin/www

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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

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