Skip to main content

Building a Docker Image using Packer and Jenkins

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 will be taking on the role of a DevOps engineer who manages a Packer file that is used to create a Docker image for one of the team's applications. You have created a new Packer file and committed it to GitHub. Now you want to automate the build process. The team has a Jenkins server that is used for running tests against the code. You have decided to create a Jenkins job to help manage the build process.

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.

Building a Docker Image using Packer and Jenkins

Introduction

In this hands-on lab, you will be taking on the role of a DevOps engineer who manages a Packer file that is used to create a Docker image for one of the team's applications. You have created a new Packer file and committed it to GitHub. Now you want to automate the build process. The team has a Jenkins server that is used for running tests against the code. You have decided to create a Jenkins job to help manage the build process.

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.

Open Port 8080 on the Firewall

  1. From your terminal, open port 8080.
    firewall-cmd --zone=public --permanent --add-port=8080/tcp
  2. Reload the firewall.
    firewall-cmd --reload
  3. Open your web browser.
  4. In the address bar, enter the following: http://<IP_ADDRESS>:8080
  5. You should see the Jenkins login screen.

Create the Jenkins Build Job

  1. On the Jenkins login screen, copy the admin password file path to your clipboard.
  2. Switch to your terminal, and expand the contents of the file to get the password.
    cat /var/lib/jenkins/secrets/initialAdminPassword
  3. Copy the output of this command to your clipboard.
  4. Switch back to your web browser, and paste the admin password into the password field on the Jenkins login screen.
  5. Click Continue.
  6. On the Getting Started page, choose Install suggested plugins.
  7. When the plugins have finished installing, click Continue as admin.
  8. Click Save and Finish.
  9. Click Start using Jenkins.
  10. On the Jenkins dashboard screen, click create new jobs.
  11. Under Enter an item name, type "BuildAppImage".
  12. Select Freestyle project, and click OK.
  13. Under the General tab, select This project is parameterized.
  14. Click Add Parameter > String Parameter.
  15. In the String Parameter menu, configure the following:
    • Name: repo_name
    • Default Value: la/express
  16. Click Apply.
  17. Scroll down to the Source Code Management header, and select Git.
  18. For Repository URL, enter the following: https://github.com/linuxacademy/content-lpic-ot-701-packer-docker.git
  19. Scroll down the page to the Build header, and click Add build step > Execute shell.
  20. In the Command box, enter the following:
    /usr/local/bin/packer build -var "repository=${repo_name}" -var "tag=${BUILD_NUMBER}" packer.json
  21. Click Save.

Run the Build Job

  1. On the project overview page, click Build with Parameters, then Build.
  2. Under Build History, click #1.
  3. Click Console Output in the left sidebar.
  4. Switch to your terminal application.
  5. Verify that the la/express and node images were successfully created.
    docker images

Conclusion

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