Skip to main content

Creating a Dockerfile

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Beginner

In this hands-on lab, you are part of a development team that is working on a next-gen product for XYZ Company. The team needs you to Dockerize the application. To do this, you need to create a Dockerfile that will copy the code to /var/node where the application will live.

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.

Creating a Dockerfile

Introduction

In this hands-on lab, we will learn how to Dockerize an application. Specifically, we will create a Dockerfile that will copy the application code to a specified location, and build a Docker image.

Setting Up the Environment

  1. Open your terminal application, and log in to the environment using the credentials provided on the lab instructions page. (Remember to replace <PUBLIC_IP_ADDRESS> with the actual public IP address.)
    ssh cloud_user@<PUBLIC_IP_ADDRESS>
  2. Type yes at the prompt.
  3. Enter your password at the prompt.

Clone the Application from GitHub

  1. Clone the application source code from GitHub.
    git clone https://github.com/linuxacademy/content-express-demo-app.git

Build the Docker Image

  1. Create a new Dockerfile.
    vi Dockerfile
    • In the vi text editor, specify the base image.
      FROM node
    • Create the directory where the code will be copied.
      RUN mkdir -p /var/node
    • Add the application source code to /var/node.
      ADD content-express-demo-app/ /var/node
    • Set the working directory to /var/node.
      WORKDIR /var/node
    • Add the following command:
      RUN npm install
    • Specify the command that the container will run when it comes online.
      CMD bin/www
    • Press ESC and type :wq to save and exit the vi text editor.
    • Build the Docker image.
      docker build -t la/app-node -f Dockerfile .
    • Verify that the Docker image was created.
      docker images

Conclusion

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