Course Lessons
Your Very First Docker Image
Getting hands-on is the best way to learn when it comes to coding.
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.
- Use the latest Ubuntu.
- Update and install
figlet
andwget
— we'll use this for the ASCII art. wget
the Star Wars font forfiglet
- Copy
print-message.sh
into the container - Make
print-message.sh
executable withchmod +x
- 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
.
- First, have Ubuntu run this script using the
bash
shell. - Define a bunch of catchphrases. Viewers of our YouTube channel know these well.
- Randomly select an index within the bounds of the
PHRASES
array. - Grab a phrase using this random index.
- 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
.
-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.

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

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.

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
.

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
--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.
- You learned about the basic building blocks of a Dockerfile.
- We wrote some code to write ASCII art.
- 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.