Your First Dockerfile
A Dockerfile is a text file of build steps. FROM picks the base image, COPY adds files, RUN runs a command, and CMD sets the command the container runs.
A tiny Dockerfile
Four lines build an image that prints a greeting.
FROM python:3.12-slim
COPY greet.py .
CMD ["python", "greet.py"]The program it copies
The copied file is one line of Python.
print("Hello from my image!")Build it
This command builds the image and tags it greet:1.0.
docker build -t g4-greet:1.0 . >/dev/nullExample
docker build -t g4-greet:1.0 . >/dev/null
docker run --rm g4-greet:1.0Key points
- FROM: the base image the new image starts from.
- COPY: files copied from the build folder into the image.
- RUN: a command run during the build, like installing a package.
- CMD: the command the container runs when it starts.
- Use docker build -t name:tag . to build an image from a Dockerfile.
In this example, Docker builds an image from a four-line Dockerfile and then runs it. The build copies greet.py into the image. The run prints Hello from my image! The greeting proves the Dockerfile, the build and the run all work.
Note
The dot at the end of docker build means the build folder is the current folder.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
Which Dockerfile line sets the command a container runs?