Container Drills
Let’s make sure the cargo is packed right. These drills will help you lock in the muscle memory for creating, building, and running Docker containers.
Tier 1: The Basic Build
Section titled “Tier 1: The Basic Build”Objective: Write a standard Dockerfile and successfully build an image.
What to do:
- Create a
Dockerfilefor a simple Node app (if you don’t have one, justnpm init -yan empty folder). - Write the standard instructions:
FROM,WORKDIR,COPY,RUN,COPY,EXPOSE, andCMD. - Create a
.dockerignorefile and addnode_modulesto it. - Run the build command:
docker build -t node-test-app .
Expected Outcome: Docker successfully builds the image. The output should show the daemon stepping through your instructions layer by layer.
Student-to-AI Prompt:
“I just built a Docker image for my Node app. Can you explain to me, in very simple terms, what my
.dockerignorefile actually protected me from doing?”
Tier 2: Port Mapping and Traffic
Section titled “Tier 2: Port Mapping and Traffic”Objective: Run the built image and map traffic correctly.
What to do:
- Assume the Node app inside your image from Tier 1 is listening on port
3000. - Start the container, but intentionally map the host port to
8080. - Navigate to
http://localhost:8080in your browser to verify it connects.
Expected Outcome:
The container starts cleanly. You can reach the app via port 8080 on your host machine, while the app internally believes it is running on 3000.
Student-to-AI Prompt:
“I ran my Docker container with
-p 8080:3000. If I wanted to run a second instance of this exact same image at the same time, what command would I need to use to avoid a port collision?”
Tier 3: Caching Diagnostics
Section titled “Tier 3: Caching Diagnostics”Objective: Force cache invalidation to see the performance cost.
What to do:
- Modify your Dockerfile to use the “less helpful” order we discussed in the caching section:
COPY . .RUN npm install
- Build the image once (
docker build -t slow-cache-test .). - Add a single comment to your
server.jsorpackage.jsonfile. - Build the image again. Note how long it takes, and watch what steps Docker re-runs.
- Change the Dockerfile back to the correct order:
COPY package*.json ./RUN npm installCOPY . .
- Build it twice more, modifying
server.jsin between. Note the speed difference.
Expected Outcome:
You should visibly see npm install being skipped (using cache) when the Dockerfile is structured correctly, and you should see it forcefully re-running when the structure is wrong.
Student-to-AI Prompt:
“I’m looking at output from a
docker buildcommand. Can you help me understand how Docker decides whether it can use the cache for a specific layer, or if it has to rebuild it from scratch?”
Getting fast at writing these foundational Dockerfiles pays massive
dividends later when we start chaining multiple containers together.
Don’t forget to stop your running containers when you are done. docker ps will show you what is running, and docker stop <id> will shut them down.
⏭ Container Compendium
Section titled “⏭ Container Compendium”Quick reference for the commands used in this lesson.