Course Lessons
Images, Containers, and Dockerfiles
In our last article, we pulled the hello-world
image, but now that you've seen it working it's time to understand how these pieces fit together.
Specifically, what is an image
or a container
? What's a Dockerfile
? These puzzle pieces fit together to make this whole thing work so it's important that you understand these.
Let's break it down with an analogy with coffee. I don't know about you — but I can't code without my morning mocha.
What is a Dockerfile
Dialing in your perfect mocha (or drink of choice) involves figuring out what you like best. Think of the Dockerfile as the instructions on exactly what to do from the ratio of coffee grinds to extraction or how much milk to use.
That might look something like this.
FROM ubuntu:latest
RUN apt-get update && apt-get install -y coffee-maker
COPY . /coffee-recipe
RUN ./prepare-beans.sh
CMD ["./brew-coffee.sh"]
Dockerfile for brewing coffee
Breaking Down the Dockerfile
- FROM ubuntu:latest
- This line specifies the base image for your Docker image. In this case, it's the latest version of the Ubuntu operating system.
- Purpose: Give us a foundation to start building from.
- RUN apt-get update && apt-get install -y coffee-maker
- This line runs a command in the container to update the package list and install a
coffee maker
. This is just a fictitious dependency of our Dockerfile. - Purpose: Installs necessary software or dependencies required for your application.
- This line runs a command in the container to update the package list and install a
- COPY . /coffee-recipe
- This line copies files from your local machine (where the Dockerfile is) to the container’s file system. Here, it copies everything in the current directory (
.
) to the/coffee-recipe
directory in the container. - Purpose: Adds your application’s code and files to the Docker image.
- This line copies files from your local machine (where the Dockerfile is) to the container’s file system. Here, it copies everything in the current directory (
- RUN ./prepare-beans.sh
- This line runs a script (
prepare-beans.sh
) inside the container to prepare the coffee beans. It’s like executing a step in your coffee recipe to get the beans ready for brewing. - Purpose: Executes any setup commands or scripts needed to prepare the environment.
- This line runs a script (
- CMD ["./brew-coffee.sh"]
- This line specifies the command to run when the container starts. Here, it’s a script (
brew-coffee.sh
) that brews the coffee. - Purpose: Defines the default command to execute when the container runs. This is typically your application’s entry point.
- This line specifies the command to run when the container starts. Here, it’s a script (
brew-coffee.sh
. Okay, so if the Dockerfile defines the instructions, what's an image?
What is a Docker Image
Think of a Docker image as a blueprint of everything you need to run a given application. It could be as simple as outputting some text like in hello-world
or it could include an entire codebase, runtime, libraries and more.
To continue the analogy, it would be like having a pre-packaged coffee kit with everything you need. This includes the Dockerfile
above.
A Container
So now we have our instructions, our pre-packaged coffee kit. How does a container fit into all this? A container is the actual mocha that you drink.
What's Next
This all may seem confusing but let's pull the curtain back and write our very own Dockerfile right now. By the end of it, the whole thing will be demystified.
You've completed this lesson!
You completed this lesson less than a minute ago.