In this assignment, you will apply what you learned in Lecture 2 – Leveraging Docker by writing multiple Dockerfiles with different requirements and running them as one-off containers. Each container will perform a single task, print output to the terminal, and then exit.
You will build images, run containers, inspect container behavior, and actively manage Docker artifacts using the Docker CLI. The goal is not just to “get output,” but to develop a clear understanding of how Dockerfile instructions translate into images, how images become containers, and how Docker tracks and manages those resources.
All containers in this assignment should be treated as disposable.
You will complete four Docker tasks, each increasing slightly in complexity. Each task should live in its own directory and contain its own Dockerfile.
Suggested folder structure:
docker-assignment/
├── task-1-hello/
├── task-2-file-read/
├── task-3-env-vars/
└── task-4-script/Create a Docker container that prints a custom message to the terminal and exits.
- Use
alpineas the base image - The container must run a single command
- The output must appear in the terminal
- The container must exit immediately after execution
- Create a directory named
task-1-hello - Inside it, create a
Dockerfile - Write a Dockerfile that prints a message of your choosing
- Build the image and tag it appropriately
- Run the container using
--rm - Verify the output appears in your terminal
- A working Dockerfile
- Terminal output showing the message
- Confirmation that no stopped containers remain
Create a container that reads and prints the contents of a text file that is included in the image at build time.
- Create a text file locally
- Copy the file into the image using
COPY - Use
WORKDIR - Use a command that prints the file’s contents
- Container must exit after execution
-
Create a directory named
task-2-file-read -
Add a text file with any content
-
Write a Dockerfile that:
- Sets a working directory
- Copies the file into the image
- Prints the file contents
-
Build the image
-
Run the container using
--rm -
View the output in the terminal
- Dockerfile
- Text file
- Terminal output showing file contents
Demonstrate how environment variables can be defined and accessed inside a container.
- Use an environment variable inside the container
- Print the value of that variable
- No interactive containers
- Container must exit after execution
-
Create a directory named
task-3-env-vars -
Write a Dockerfile that:
- Defines an environment variable
- Prints the value of that variable
-
Build the image
-
Run the container using
--rm -
Observe the output
- Override the environment variable at runtime using a Docker flag
- Dockerfile
- Terminal output showing the environment variable value
Run a small script inside a container and print output to the terminal.
- Create a script file locally (shell or Python)
- Copy the script into the image
- Ensure the script executes when the container runs
- Container must exit after execution
-
Create a directory named
task-4-script -
Write a script that prints multiple lines
-
Write a Dockerfile that:
- Copies the script into the image
- Executes the script
-
Build the image
-
Run the container using
--rm -
Confirm all script output appears in the terminal
- Script file
- Dockerfile
- Terminal output
Throughout this assignment, you are expected to actively manage Docker artifacts.
At minimum, you should demonstrate:
- Viewing running containers
- Viewing all containers (including stopped)
- Removing stopped containers
- Listing images
- Removing unused images
You should be able to answer the following by the end:
- Why don’t stopped containers appear with
docker ps? - Why can’t an image be deleted if a container depends on it?
- What does the
--rmflag prevent?
This assignment is not about memorizing commands—it’s about developing confidence in Docker’s lifecycle. By intentionally building, running, inspecting, and cleaning up containers, you are practicing the exact workflow used daily in professional software engineering environments.
If Docker feels repetitive here, that’s the point.