Adding Metadata and Labels

1 hour
  • 6 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a Dockerfile

Create a Dockerfile using the following instructions:

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.application=$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

Build the Docker image using the following parameters:

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.0  -f Dockerfile .
Push the Image to Docker Hub

Push the weather-app image to Docker Hub.

docker push <USERNAME>/weather-app
Create the `weather-app` Container

Start the weather-app container.

docker run -d --name demo-app -p 80:3000 --restart always <USERNAME>/weather-app
Check Out Version v1.1 of the Weather App

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

Rebuild and push the weather-app image.

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

Additional Resources

In this lab, you will deploy a container that uses labels. In order to complete this Labels, 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.

Create the Dockerfile

  1. The base image should be node.
  2. Add three arguments:
    • BUILD_VERSION
    • BUILD_DATE
    • APPLICATION_NAME
  3. Add three labels:
    • org.label-schema.build-date (will be set using the BUILD_DATE argument)
    • org.label-schema.application (will be set using the APPLICATION_NAME argument)
    • org.label-schema.version (will be set using the BUILD_VERSION argument)
  4. Using the RUN instruction, make a directory called /var/node.
  5. Use the ADD instruction to add the contents of the code directory into /var/node.
  6. Make /var/node the working directory.
  7. Execute an npm install.
  8. Expose port 3000.
  9. Set ./bin/www as the command.
  10. From the command line, log in to Docker Hub.
  11. Build your image using <USERNAME>/weather-app, and supply the following three build arguments:
    • BUILD_DATE should be set to $(date -u +'%Y-%m-%dT%H:%M:%SZ').
    • APPLICATION_NAME should be set to weather-app.
    • BUILD_VERSION should be set to v1.0.
  12. Push the image to Docker Hub.

Create the Weather App

  1. Create a Docker container called weather-app.
  2. The port mapping should be port 80 on the host, mapping to 3000 on the container.
  3. The restart policy should be set to always.
  4. Use the image you created, <USERNAME>/weather-app.
  5. Use the docker inspect command with the filter flag to filter output for {{.Config.Labels}}.

Update the Weather App to Version 1.1

  1. Change directories to /root/weather-app.
  2. Check out branch v1.1.
  3. Change directories back to /root.
  4. Rebuild your image with the following build arguments:
    • BUILD_DATE should be set to $(date -u +'%Y-%m-%dT%H:%M:%SZ').
    • APPLICATION_NAME should be set to weather-app.
    • BUILD_VERSION should be set to v1.1.
  5. Push the image to Docker Hub.

Watchtower Will Update weather-app

  1. The Watchtower interval is set to 5 seconds. After about 10 or 15 seconds, check to see if weather-app has been updated by executing docker ps.
  2. Use the docker inspect command to see if the version is set to v1.1.

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.

Sign In
Welcome Back!

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

Get Started
Who’s going to be learning?