📘Cisco DevNet Associate (200-901 DEVASC)
A Dockerfile is a text file that contains a set of instructions used to build a Docker image. A Docker image is like a template for creating containers, which are isolated environments where your applications run.
Think of it as a recipe: the Dockerfile tells Docker exactly what steps to take to prepare an environment so that your application can run consistently anywhere.
Basic Structure of a Dockerfile
A Dockerfile usually contains:
- Base Image
- Maintainer / Metadata
- Instructions to install software or dependencies
- Copying files
- Running commands
- Exposing ports
- Defining the default command
Let’s go through these one by one.
1. FROM – Base Image
- Syntax:
FROM <image>:<tag>
- Purpose:
Specifies the starting point for the Docker image. Usually, it’s an existing image with an operating system or a programming environment. - Example:
FROM python:3.11
This means the container starts with Python version 3.11 installed.
- Exam Tip:
You need to recognizeFROMas the instruction that defines the base image. Without it, Docker won’t know what environment to start with.
2. LABEL – Metadata
- Syntax:
LABEL key="value"
- Purpose:
Adds metadata to the image, like author, version, or description. - Example:
LABEL maintainer="pooja@example.com"
LABEL version="1.0"
- Exam Tip:
LABELis optional but can provide important information about the image.
3. RUN – Execute Commands
- Syntax:
RUN <command>
- Purpose:
Executes a command while building the image. Commonly used to install software or libraries. - Example:
RUN apt-get update && apt-get install -y git
This updates the package list and installs Git inside the image.
- Exam Tip:
RUNcommands execute during image build, not when the container starts.
4. COPY and ADD – Copy Files
- COPY Syntax:
COPY <source> <destination>
- ADD Syntax:
ADD <source> <destination>
- Purpose:
Both commands copy files from your local machine into the Docker image. ADDcan also extract tar files and fetch files from URLs.COPYis simpler and preferred if you only need local files.- Example:
COPY requirements.txt /app/requirements.txt
ADD app.tar.gz /app/
- Exam Tip:
Remember:COPY= copy files,ADD= copy + more features (like auto-extracting).
5. WORKDIR – Set Working Directory
- Syntax:
WORKDIR /app
- Purpose:
Sets the current working directory inside the container for the following commands. - Example:
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
All commands now operate in /app.
- Exam Tip:
Helps prevent writing long paths for every command.
6. ENV – Environment Variables
- Syntax:
ENV <key>=<value>
- Purpose:
Defines environment variables inside the container. - Example:
ENV APP_ENV=production
- Exam Tip:
Useful for configuring the application behavior inside the container.
7. EXPOSE – Open Ports
- Syntax:
EXPOSE <port>
- Purpose:
Indicates which network port the container listens on. - Example:
EXPOSE 5000
This tells Docker (and other developers) that the app uses port 5000.
- Exam Tip:
Does not actually publish the port to the host — that happens withdocker run -p.
8. CMD – Default Command
- Syntax:
CMD ["executable","param1","param2"]
- Purpose:
Specifies the default command to run when the container starts. - Example:
CMD ["python", "app.py"]
- Exam Tip:
- Only one
CMDis allowed per Dockerfile; later ones override earlier ones. - Use
CMDfor the main process of your container.
9. ENTRYPOINT – Fixed Command
- Syntax:
ENTRYPOINT ["executable","param1"]
- Purpose:
Similar toCMD, but cannot be easily overridden when running the container. - Often used when the container should always run the same executable.
- Example:
ENTRYPOINT ["python"]
CMD ["app.py"]
Here, python is fixed, but app.py can be overridden.
- Exam Tip:
Understand the difference:ENTRYPOINTis mandatory,CMDis default/optional.
10. USER – Set the User
- Syntax:
USER <username>
- Purpose:
Runs container commands as a specific user (not root). - Example:
USER appuser
- Exam Tip:
Important for security; always prefer running apps as a non-root user.
11. VOLUME – Mount Data
- Syntax:
VOLUME ["/data"]
- Purpose:
Creates a persistent storage location inside the container that can map to the host machine. - Exam Tip:
Used to save data that persists even after the container stops.
Key Exam Takeaways
For Cisco DevNet Associate, you should be able to:
- Identify Dockerfile instructions:
FROM,RUN,COPY,WORKDIR,CMD,ENTRYPOINT,ENV,EXPOSE,USER,VOLUME,LABEL. - Understand the purpose of each instruction.
- Interpret a Dockerfile to know how the container environment is built.
- Recognize differences between
CMDandENTRYPOINT, and betweenCOPYandADD. - Understand build-time vs run-time instructions:
- Build-time:
RUN,COPY,ADD,WORKDIR,ENV - Run-time:
CMD,ENTRYPOINT,EXPOSE,USER,VOLUME
- Build-time:
Sample Dockerfile
Here’s a complete example you might see in the exam:
# Base image
FROM python:3.11# Metadata
LABEL maintainer="pooja@example.com"# Set working directory
WORKDIR /app# Copy files
COPY requirements.txt .# Install dependencies
RUN pip install -r requirements.txt# Copy application code
COPY . .# Set environment variable
ENV APP_ENV=production# Expose port
EXPOSE 5000# Default command
CMD ["python", "app.py"]
- This tells Docker:
- Start with Python 3.11
- Set
/appas the working directory - Install dependencies from
requirements.txt - Copy application code into the container
- Expose port 5000
- Run
python app.pywhen the container starts
✅ Tip for Students:
If you can read a Dockerfile and explain each instruction, you’re ready for this exam topic. Focus on purpose + timing (build-time vs run-time).
