We will create a custom Docker Image using the “Whale say ” image which is a small Docker Image (based on an Ubuntu Image) which when you run it, it says something that you programmed to say back to you.

First, fire up a terminal and create a new folder by typing:

mkdir mywhale

This directory serves as the “context” for your build. The context just means that it contains all the things that you need in order to successfully build your image.

Get inside to your new folder with:

cd mywhale

and create a Dockerfile in the folder by typing:

touch Dockerfile

Now you should see the empty Dockerfile that we created if you give ‘ll’ command:

ll

$ ll
total 8.0K
-rw-rw-r– 1 user user 0 23:26 Dockerfile

Open it with your favorite text editor and add:

FROM docker/whalesay:latest

This first line of instruction, with the FROM keyword, tells Docker which image your image is based on. You are basing your new work on the existing whalesay image.

The next instruction that we will add will give the ability to our whale to tell a fortune. To accomplish this task, we will use the fortune package that is available in the Ubuntu repositories (remember that the whale image is based on a Ubuntu image). The fortunes program has a command that prints out wise sayings for our whale to say.

So, the first step is to install it. To do this we add the usual apt install instruction:

RUN apt -y update && apt -y install fortunes

Once the image has the software it needs, you instruct the software to run when the image is loaded. To do this we add the following instruction:

CMD /usr/games/fortune -a | cowsay

The above line tells the fortune program to send a randomly chosen quote to the cowsay program

And we are done! Now save the file and exit.
You can verify what you did by running “cat Dockerfile” so as that your Dockerfile looks like this:

cat Dockerfile

FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay

Now that everything (hopefully) looks good, its time to build our Docker Image (don’t forget the . period at the and of the command).:

docker build -t my-docker-whale .

The above command takes the Dockerfile in the current folder and builds an image called “my-docker-whale” on your local machine.

You can verify that your Docker image is indeed stored on your computer with:

docker images

Then you may run your Docker image by typing the following:

Once it runs, you will get your image

Leave a Reply

Your email address will not be published. Required fields are marked *