Skip to main content

Using Packer to Create a Docker Image

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Intermediate

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using Packer to Create a Docker Image

Introduction

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.

Setting Up the Environment

  1. Open your terminal application, and log in using the credentials provided on the lab instructions page.
    ssh cloud_user@<PUBLIC_IP_ADDRESS>
  2. Type yes at the prompt.
  3. Enter your password at the prompt.

Create a Packer File

  1. Become the root user:

    sudo su
  2. Create a Packer file:

    vi packer.json
  3. In the vi text editor, configure the following:

    {
    "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`}}"
      }
    ]
    }
  4. Press ESC and type :wq to save the changes and exit the vi text editor.

  5. Validate the Packer file.

    packer validate packer.json

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 that 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 that the Docker container started.
    docker ps
  3. Verify that everything is working correctly.
    curl localhost

Conclusion

Congratulations, you've successfully completed this hands-on lab!