3.6 Exercise 2 - Docker recipes and build images

In breakout rooms, do the following exercise:

  1. figlet recipe.
    • Write a docker recipe in a file Dockerfile_figlet that:
      • is based on ubuntu:18.04.
      • echoes “I love containers” by default or any other word/sentence, if given as the argument.
    • Build the image (give the image the name of your choice).
    • Run the image:
      • with no argument.
      • with “Docker course” as an argument.
    • Modify the Dockerfile_figlet:
      • add the MAINTAINER field.
      • update and upgrade Ubuntu packages.
      • install the figlet program.
      • change “echo” to “figlet” in the ENTRYPOINT
    • Build the image.
    • Run the new image, with the default parameters, then with “Docker course” as an argument.
Answer

Recipe saved in Dockerfile_figlet:

FROM ubuntu:18.04

ENTRYPOINT ["echo"]
CMD ["I love containers"]

Build:

docker build --file Dockerfile_figlet -t mytest .

Run the image:

# with no argument
docker run mytest

# with argument "Docker course"
docker run mytest "Docker course"

Recipe:

FROM ubuntu:18.04

MAINTAINER Name Surname <name.surname@mail.com>

RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y figlet

ENTRYPOINT ["figlet"]
CMD ["I love containers"]

Build the image.

docker build --no-cache --file Dockerfile_figlet -t mytestfiglet .

Run the new image, with the default parameter, then with “Docker course” as an argument.

# run with no argument
docker run mytestfiglet

# run with argument "Docker course"
docker run mytestfiglet "Docker course"
  1. Random numbers
    • Copy the following short bash script in a file called random_numbers.bash.
#!/usr/bin/bash
seq 1 1000 | shuf | head -$1

This script outputs random intergers from 1 to 1000: the number of integers selected is given as the first argument.

  • Write a recipe for an image:
    • Based on centos:7
    • That will execute this script (with bash) when it is run, giving it 2 as a default argument (i.e. outputs 2 random integers): the default can be changed as the image is run.
  • Build the image.
    • Start a container with the default argument, then try it with another argument.
Answer

Recipe (in Dockerfile_RN):

FROM centos:7

MAINTAINER Name Surname <name.surname@mail.com>

# Copy script from host to image
COPY random_numbers.bash .

# Make script executable
RUN chmod +x random_numbers.bash

# As the container starts, "random_numbers.bash" is run
ENTRYPOINT ["/usr/bin/bash", "random_numbers.bash"]

# default argument (that can be changed on the command line)
CMD ["2"]

Build and run:

docker build -f Dockerfile_RN -t random_numbers .
docker run random_numbers
docker run random_numbers 10