Skip to main content

Adding Metadata and Labels

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Intermediate

For the last six months, the Acme Anvil Corporation has been migrating some of their bare metal infrastructure to Docker containers. After the initial implementation, you mention to the team that labels can be used for storing metadata about images and containers. This is useful for keeping track of image and container attributes like the build date and application version. Your team thinks this is a great idea, and you’ve been tasked with creating a quick demo to show how useful labels can be. You created a small weather app a few months ago while learning Node.JS, and it’s perfect for a quick demo. On your Docker workstation, you will create a Dockerfile that contains four labels: the maintainer, build date, application name, and application version. You will then build the image and push it to Docker Hub. On your Docker server, you will create a new container using the weather-app image. Once the container is running, you will update the branch of your application, rebuild the image, and push the changes to Docker Hub. On the Docker server, Watchtower will update the running container with the new version of 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.

Adding Metadata and Labels

Introduction

For the last six months, the Acme Anvil Corporation has been migrating some of their bare metal infrastructure to Docker containers. After the initial implementation, you mention to the team that labels can be used for storing metadata about images and containers. This is useful for keeping track of image and container attributes like the build date and application version.

Your team thinks this is a great idea, and you’ve been tasked with creating a quick demo to show how useful labels can be. You created a small weather app a few months ago while learning Node.JS, and it’s perfect for a quick demo. On your Docker workstation, you will create a Dockerfile that contains four labels: the maintainer, build date, application name, and application version. You will then build the image and push it to Docker Hub. On your Docker server, you will create a new container using the weather-app image. Once the container is running, you will update the branch of your application, rebuild the image, and push the changes to Docker Hub. On the Docker server, Watchtower will update the running container with the new version of the image.

Instructions

In this lab, you will deploy a container that uses labels. In order to complete this learning activity, you will need a Docker Hub account.

Log in to your Docker workstation and Docker server, and sudo to root. You will create the Dockerfile and image on the Docker workstation. The weather-app container will be run on your Docker server.

Solution

Begin by logging in to the Docker Server and Docker Workstation lab servers using the credentials provided on the hands-on lab page:

ssh cloud_user@PUBLIC_IP_ADDRESS

Become the root user:

sudo su -

Create a Dockerfile

On Docker Workstation:

  1. Create a Dockerfile:

    vi Dockerfile
  2. Use the following instructions:

    > Note: Replace EMAIL_ADDRESS with your email address.

    FROM node
    
    LABEL maintainer="EMAIL_ADDRESS"
    
    ARG BUILD_VERSION
    ARG BUILD_DATE
    ARG APPLICATION_NAME
    
    LABEL org.label-schema.build-date=$BUILD_DATE
    LABEL org.label-schema.applicaiton=$APPLICATION_NAME
    LABEL org.label-schema.version=$BUILD_VERSION
    
    RUN mkdir -p /var/node
    ADD weather-app/ /var/node/
    WORKDIR /var/node
    RUN npm install
    EXPOSE 3000
    CMD ./bin/www

Build the Docker image

  1. Log in to Docker Hub:

    docker login
  2. Build the Docker image using the following parameters:

    > Note: Be sure to replace DOCKER_USERNAME with your Docker Hub username.

    docker build -t <DOCKER_USERNAME>/weather-app --build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') 
    --build-arg APPLICATION_NAME=weather-app --build-arg BUILD_VERSION=v1.0  -f Dockerfile .
  3. Show image ID:

    docker images
  4. Use image ID to inspect:

    docker inspect IMAGE_ID

Push the image to Docker Hub

  1. Push the weather-app image to Docker Hub.

    > Note: Be sure to replace USERNAME with your Docker Hub username.

    docker push USERNAME/weather-app

Create the weather-app container

On the Docker Server:

  1. Start the weather-app container.

    > Note: Be sure to replace USERNAME with your Docker Hub username.

    docker run -d --name demo-app -p 80:3000 --restart always USERNAME/weather-app
  2. Verify that the image is running:

    docker ps

Check out version v1.1 of the weather app

On the Docker Workstation:

  1. In the weather-app directory, check out version v1.1 of the weather app.

    cd weather-app
    git checkout v1.1
    cd ../ 

Rebuild the weather-app image

  1. Rebuild and push the weather-app image.

    > Note: Be sure to replace USERNAME with your Docker Hub username.

    docker build -t USERNAME/weather-app --build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') /
    --build-arg APPLICATION_NAME=weather-app --build-arg BUILD_VERSION=v1.1  -f Dockerfile .
    docker push USERNAME/weather-app

On the Docker Server:

  1. Show image status:

    docker ps
  2. Using the container ID from the previous command, inspect the weather-app image:

    docker inspect IMAGE_ID

Conclusion

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