Creating a Dockerfile
A Dockerfile is a text file with instructions to build a custom image. It automates the process of creating an image so you can reproduce it anywhere.
What Is a Dockerfile?
A Dockerfile contains a series of commands that Docker executes to assemble an image. Each command adds a new layer to the image. By version‑controlling your Dockerfile, you keep your infrastructure as code.
Basic Dockerfile Instructions
- FROM: Sets the base image (e.g.,
FROM ubuntu:22.04). Every Dockerfile must start withFROM. - WORKDIR: Sets the working directory for subsequent instructions.
- COPY: Copies files from your host into the image.
- RUN: Executes a command during the build (e.g., install packages).
- CMD: Specifies the default command to run when a container starts. There can be only one
CMD. - EXPOSE: Documents which port the container listens on (does not publish it).
Example: Simple Python App
Create a file named
app.py with:print("Hello from Docker")Now create a Dockerfile (no extension) in the same folder:FROM python:3.11-slim
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]Understanding Each Line
FROM python:3.11-slim– starts from a small Python image.WORKDIR /app– creates the/appdirectory and switches to it.COPY app.py .– copiesapp.pyfrom your host into/appinside the image.CMD ["python", "app.py"]– the default command when the container runs.
Two Minute Drill
- Dockerfile automates image creation.
- Key instructions:
FROM,WORKDIR,COPY,RUN,CMD. - Each instruction creates a new image layer.
- Store your Dockerfile in version control for reproducibility.
Need more clarification?
Drop us an email at career@quipoinfotech.com
