Differences between a Dockerfile, Docker Image and Docker Container
Being able to identify what something is can lead to learning complex topics faster. Here's how these 3 Docker concepts tie together.
In casual conversation you may hear phrases like:
- “Yeah, I’m using Docker containers”
- “Yay, I built my app with Docker”
- “I have many Docker images being used in production”
- “Sure, let’s get the containers up and running”
While none of those phrases are technically wrong, they don’t really tell you the full story on how a few Docker related files and concepts tie together and it can be very confusing to learn something new when you hear phrases like that used all over the place.
I don’t know about you, but I learn best when I can identify and relate to whatever I’m trying to learn. It’s that magic thing that lets me go from thinking “…WTF am I reading?” to using it to solve my own problems without any trouble.
So let’s see how a Dockerfile, Docker image and Docker container all come together.
# It All Starts With a Dockerfile
A Dockerfile
is a file that you create which in turn produces a Docker image
when you build it.
Just by breaking down that sentence we can start making a few assumptions, such as:
- A Docker image can be created by taking 2 actions
- The first action is to create a thing called a
Dockerfile
- The second action is to run some type of build command that uses the
Dockerfile
- The first action is to create a thing called a
- A Docker image is a file or file-like thing, since it gets “built”
Those assumptions are correct, but let’s put them on the back burner for now and
talk a little bit more about the Dockerfile
.
A Dockerfile
is a text file that Docker reads in from top to bottom. It
contains a bunch of instructions which informs Docker HOW the Docker image
should get built.
You can relate it to cooking. In cooking you have recipes. A recipe lets you know all of the steps you must take in order to produce whatever you’re trying to cook.
The act of cooking is building the recipe.
A Dockerfile
is a recipe (or blueprint if that helps) for building Docker
images, and the act of running a separate build command produces the Docker image
from that recipe.
# If You Build It, They Will Run (Usually)
The act of running a Docker image creates a Docker container, that’s it.
Here’s an easy thing to relate to if you’re a software developer:
In the world of object oriented programming, you often deal with classes. You can think of a Docker image as a class, where as a Docker container is an instance of that class.
Personally I’m not a fan of that analogy because it’s not the most accurate thing in the world but sometimes having a “good enough” relation is enough to grasp something and that analogy is definitely “good enough”.
So at the end of the day, we can sum things up as:
- A
Dockerfile
is a recipe for creating Docker images - A Docker image gets built by running a Docker command (which uses that
Dockerfile
) - A Docker container is a running instance of a Docker image
If you want to identify and see how a few different Docker tools work together then check out another article I put together that lets you get to know Docker’s ecosystem.