What is a Dockerfile?
A Dockerfile is a recipe for building a Docker image.
Dockerfile
│
docker build
│
Image
│
docker run
│
ContainerThink of it like:
Recipe -> Cake
Source -> Binary
Dockerfile -> ImageBasic Dockerfile
FROM ubuntu:24.04
CMD ["bash"]Build:
docker build -t myubuntu .Run:
docker run -it myubuntuCommon Instructions
FROM
Base image.
FROM ubuntu:24.04RUN
Execute commands while building the image.
RUN apt-get update && \
apt-get install -y git vim treeRUN executes only during docker build.
WORKDIR
Set the working directory for subsequent instructions.
WORKDIR /workspaceEquivalent to:
cd /workspacefor all following instructions.
COPY
Syntax:
COPY <source> <destination>Examples:
COPY hello.txt /tmp/
COPY . .
COPY . /appCOPY . . means:
- First
.: current build context (host) - Second
.: current working directory inside the image
Usually used together with:
WORKDIR /app
COPY . .ENV
Define environment variables.
ENV APP_HOME=/workspaceCMD
Default command executed when the container starts.
CMD ["bash"]Can be overridden:
docker run myimage lsENTRYPOINT
Defines the executable.
ENTRYPOINT ["python3"]Then
docker run myimage script.pyactually runs
python3 script.pyDocker Image Layers
Example:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y git
WORKDIR /app
COPY . .
RUN makeLayers:
Ubuntu
├── apt update
├── install git
├── WORKDIR
├── COPY source
└── makeDocker caches each layer.
If only source code changes, Docker reuses the earlier layers and rebuilds only:
COPY
RUN makeLayer Ordering
Good:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y git
WORKDIR /app
COPY . .
RUN makeBad:
COPY . .
RUN apt-get install -y gitChanging one source file forces all later layers to rebuild.
Build Context
The final . in:
docker build -t myapp .means:
Send the current directory to Docker as the build context.
COPY . . copies files from this build context.
.dockerignore
Exclude unnecessary files from the build context.
Example:
.git
build
node_modules
*.log
*.oAlmost every real project should have a .dockerignore.
Build
docker build -t myimage .Specify Dockerfile:
docker build -f Dockerfile.dev -t myimage .Naming Convention
Docker 29+ accepts both:
Dockerfile
dockerfileHowever, the community convention is:
DockerfileReasons:
- Matches documentation
- Used by nearly all open-source projects
- Better IDE support
- More portable across environments
Best Practices
- Keep stable instructions first.
- Put
COPY . .near the end. - Use
.dockerignore. - Use
WORKDIRinstead of manually creating directories. - Build images once; run containers many times.
Key Takeaways
- Dockerfile describes how to build an image.
docker buildcreates an image.docker runcreates a container from the image.- Docker caches image layers to speed up rebuilds.
COPY . .copies the build context into the currentWORKDIR.- Prefer
Dockerfileas the filename even though modern Docker also acceptsdockerfile.