Screen Too Small

This course material requires a larger screen to properly display the terminal and editor interfaces.

Please use a tablet, laptop, or desktop computer for the best learning experience.

You can continue browsing, but the experience will be significantly limited.

5/8
Free Trial: 4 lessons remaining
Unlock Full Access

Your Very First Docker Image

Estimated time: 5 minutes

Getting hands-on is the best way to learn when it comes to coding.

💡
Doing pretty much anything hands-on will help you go far. Docker is no exception.

In our last tutorial, you learned everything starts with the Dockerfile. It represents the instructions on what you need, where you need it, and how to start it. We're going to build our own Dockerfile to return to the glory of ASCII art.

The Dockerfile

In a new directory, let's create our Dockerfile with a simple touch Dockerfile command. Once it's created, we can jump in and paste the following code. Don't worry we'll explain what this file does in detail below.

# Use the latest Ubuntu image as the base
FROM ubuntu:latest

# Install figlet for ASCII art
RUN apt-get update && apt-get install -y figlet wget

# Download and install the Star Wars font for figlet
RUN wget http://www.jave.de/figlet/fonts/details/starwars.flf -O /usr/share/figlet/starwars.flf

# Copy the ASCII art script to the container
COPY print-message.sh /print-message.sh

# Make sure the script is executable
RUN chmod +x /print-message.sh

# Set the command to run the ASCII art script
CMD ["/print-message.sh"]

Let's break this down.

  1. Use the latest Ubuntu.
  2. Update and install figlet and wget— we'll use this for the ASCII art.
  3. wget the Star Wars font for figlet
  4. Copy print-message.sh into the container
  5. Make print-message.sh executable with chmod +x
  6. Execute print-message.sh when the container launches.

So what's print-message.sh doing?

#!/bin/bash
# Define an array of catchphrases
PHRASES=("We think you're awesome!" "LDUR!" "Are you running Arch, btw?" "Thanks Nerd" "Is it Wednesday, btw?")

# Randomly select a phrase
RANDOM_INDEX=$(( RANDOM % ${#PHRASES[@]} ))
SELECTED_PHRASE=${PHRASES[$RANDOM_INDEX]}

# Print the messages with figlet
figlet -w 200 -f starwars "$SELECTED_PHRASE"

Create print-message.sh similarly with touch.

  1. First, have Ubuntu run this script using the bash shell.
  2. Define a bunch of catchphrases. Viewers of our YouTube channel know these well.
  3. Randomly select an index within the bounds of the PHRASES array.
  4. Grab a phrase using this random index.
  5. Print the phrase using a width of 200 using the StarWars font

Next Steps

So now that we have our Dockerfile and our entry point script, we need to build our image. Run the following command from within our directory containing these two files.

docker build -t ascii .

Build an image called ascii from the current directory.

This command says "build the Dockerfile found in this directory. We'll tag it with latest and we'll call the image ascii.

💡
The -t parameter tags this build. If you don't specify a tag, it will use latest. If you want to tag it, you would use something like this. `docker build -t ascii:awesome .`

When you run this command, you'll see something like the following.

💡
If you're not running Docker Desktop, you won't see build details, including any vulnerabilities.
Build it, build it!

Viewing the Image

So now that we have it built, we should see it in docker images.

It is built.

Running the Image

This is all fine and good, but how do you run the image? To do this, you can run docker run ascii. If all goes well, you should see something like this.

Left, Down, Up, Right!

Great!

Containers

So now that we've run a container instance of ascii, do we have any containers to see? Let's check by docker ps.

By default, docker ps only shows containers that are currently running. We should check all containers by using docker ps -a.

A stopped container is listed.

Here, we see a stopped container. Does this matter? In the grand scheme, it won't likely cause you problems, but in general — it is better not to leave stopped containers around. To remove this container, run this command. The specific name (in my case, suspicious_golick), can be found in the docker ps -a output. You can also use the container ID.

docker rm suspicious_golick
💡
Have a docker container that runs and should be removed right after? Use the --rm option or flag tells Docker to remove the container after it exits. This is useful for containers that perform an action and immediately stop (e.g. docker run --rm ascii).

Starting a stopped container

Starting an existing container is simple. Just execute docker run CONTAINER_ID_OR_NAME. Be aware that this will not cause the entry point to execute, so our script will not display another message

Recap

Wow, we did a lot just now.

  1. You learned about the basic building blocks of a Dockerfile.
  2. We wrote some code to write ASCII art.
  3. We learned about containers and images and how they related to one another.

Believe it or not, this is just the surface of what Docker can offer. Next, we're going to spin up a Postgres instance.

You've completed this lesson!

You completed this lesson less than a minute ago.