3.6 Exercise 2 - Docker recipes and build images
Do the following exercise:
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.
- Write a docker recipe in a file Dockerfile_figlet that:
Answer
Recipe saved in Dockerfile_figlet:
FROM ubuntu:18.04
ENTRYPOINT ["echo"]
CMD ["I love containers"]
Build:
Run the image:
# with no argument
docker run mytest_${USER}
# with argument "Docker course"
docker run mytest_${USER} "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.
Run the new image, with the default parameter, then with “Docker course” as an argument.
- Random numbers
- Copy the following short
bash
script in a file calledrandom_numbers.bash
.
- Copy the following short
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: