Understanding Dockerfiles
Learn the syntax and common instructions used in Dockerfiles to define how an image is built.
Understanding Dockerfiles is a free Docker & DevOps Fundamentals lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & DevOps Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What's a Dockerfile?
Imagine you have a recipe for baking a cake. A Dockerfile is very similar! It's a plain text file that contains all the instructions Docker needs to automatically build a Docker image.
Instead of mixing ingredients, you're telling Docker which base operating system to use, what software to install, and how to set up your application inside the image.

Benefits: Build Automation
Dockerfiles bring many advantages to your development workflow:
- Automation: No more manual steps! Docker builds images consistently every time.
- Version Control: Dockerfiles are code, so you can track changes using Git.
- Reproducibility: Anyone with the Dockerfile can build the exact same image.
- Transparency: You can see exactly what's inside an image.
Basic Dockerfile Structure
A Dockerfile is a list of instructions, executed in order from top to bottom. Each instruction creates a new layer in the image.
The basic syntax is simple:
INSTRUCTION argumentsComments start with a #, just like in many programming languages. Docker instructions are case-insensitive, but convention is to use uppercase.
Choosing a Base Image with FROM
Every Dockerfile must start with the FROM instruction. It specifies the base image your new image will be built upon. This base image usually contains an operating system (like Alpine Linux) and sometimes pre-installed software.
Think of it as choosing your starting point – a blank canvas or a canvas with some basic colors already on it.
FROM alpine:latestExecuting Commands with RUN
The RUN instruction is used to execute commands during the image build process. This is where you'd install packages, create directories, or compile code.
Each RUN command creates a new layer, so combining commands with && can help reduce image size.
RUN apk update && apk add gitDefault Command with CMD
The CMD instruction provides default commands or arguments for an executing container. Unlike RUN, CMD commands are executed when a container starts, not during the build.
If you specify a command when running a container (e.g., docker run my-image ls), that command will override the CMD instruction.
CMD ["echo", "Hello from container!"]Adding Files with COPY
The COPY instruction copies new files or directories from your host machine (where you're building the image) into the Docker image's filesystem at a specified path.
It's generally preferred over ADD because it's more transparent and only copies local files.
COPY ./app /usr/src/appWork Directory & Environment Variables
WORKDIR sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it. It makes your Dockerfile cleaner.
ENV sets environment variables. These variables are available to subsequent instructions in the Dockerfile and also to the running container.
WORKDIR /app
ENV PORT=8080Informing About Ports with EXPOSE
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. It doesn't actually publish the port.
Think of it as documentation. To actually make the port accessible from outside the container, you need to use the -p flag with docker run.
EXPOSE 80
EXPOSE 443Your First Simple Dockerfile
Let's put it all together! Here's a basic Dockerfile that sets up an Alpine Linux image, installs a package, and defines a default command.
To build this, save it as Dockerfile in an empty directory, then run docker build -t my-simple-app . in your terminal.
FROM alpine:latest
LABEL maintainer="CoddyKit"
RUN apk update && apk add --no-cache bash
WORKDIR /app
CMD ["bash", "-c", "echo 'Hello from my custom Alpine image!' && sleep 3600"]Dockerfile Instructions Quiz
Test your knowledge! Which of these instructions are executed during the image build process, directly modifying the image's content or structure?
Recap: Dockerfile Essentials
You've taken a big step in understanding Docker images! We learned that Dockerfiles are text files with instructions for building images, offering great benefits like automation and reproducibility.
Key instructions covered:
FROM: Defines the base image.RUN: Executes commands during build.CMD: Sets the default command for a running container.COPY: Adds files from host to image.WORKDIR,ENV,EXPOSE: Configure the image and container environment.
Frequently asked questions
Is the “Understanding Dockerfiles” lesson free?
Yes — the full text of “Understanding Dockerfiles” is free to read here on the web, and the Docker & DevOps Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & DevOps Fundamentals course, upgrade to CoddyKit PRO.
What will I learn in “Understanding Dockerfiles”?
Learn the syntax and common instructions used in Dockerfiles to define how an image is built. You practise Docker & DevOps Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Docker & DevOps Fundamentals?
No prior experience is required. Docker & DevOps Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Understanding Dockerfiles” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Docker & DevOps Fundamentals lesson?
Yes. Every Docker & DevOps Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Understanding Dockerfiles
- Creating Custom Docker Images
- Image Layers and Optimization
- Multi-Stage Builds & Smaller Images