Docker Build Command – How to Build an Image

You wrote your Dockerfile. Now you turn it into a real Docker image using the docker build command. This topic covers the build process in detail, what the output means, and how to tag and manage your images.

The Basic Build Command

Open your terminal, navigate to your my-app folder (where the Dockerfile lives), and run:

docker build -t my-python-app:1.0 .

Breaking down each part:

docker build        → the command to build an image
-t                  → "tag" — give the image a name
my-python-app:1.0   → image name (my-python-app) and tag (1.0)
.                   → build context: the current directory
                      Docker sends all files here to the daemon

Reading the Build Output

Docker prints output for each Dockerfile instruction it processes:

[+] Building 18.3s (10/10) FINISHED
 => [internal] load build definition from Dockerfile          0.0s
 => [internal] load .dockerignore                             0.0s
 => [internal] load metadata for python:3.11-slim             1.2s
 => [1/5] FROM python:3.11-slim                               5.4s
 => [2/5] WORKDIR /app                                        0.0s
 => [3/5] COPY requirements.txt .                             0.0s
 => [4/5] RUN pip install -r requirements.txt                 9.8s
 => [5/5] COPY . .                                            0.0s
 => exporting to image                                        0.3s
 => naming to docker.io/library/my-python-app:1.0             0.0s

Each numbered step matches a Dockerfile instruction. The time shown is how long that step took. Step 4 (pip install) takes the longest because it downloads packages from the internet.

What the Build Context Is

Your machine:
my-app/
├── app.py           ─┐
├── requirements.txt  ├── All these files get sent to Docker daemon
├── Dockerfile       ─┘
└── __pycache__/     ← You probably do NOT want this sent

Build context = everything in the current directory
Large contexts = slow builds

Docker sends the entire current directory to the daemon before building. If your folder contains large files (like datasets or node_modules), the build slows down significantly. Use a .dockerignore file to exclude unnecessary files.

Creating a .dockerignore File

Create a file called .dockerignore in your project folder:

__pycache__
*.pyc
*.pyo
.git
.env
node_modules
*.log

This works exactly like .gitignore — files listed here are not sent to the Docker daemon during build.

Image Tagging — Naming Your Images Properly

Docker image names follow a specific format:

registry/username/imagename:tag

Examples:
my-python-app:1.0         → local image, version 1.0
my-python-app:latest      → local image, "latest" tag
myusername/my-app:1.0     → ready to push to Docker Hub
nginx:1.25                → official nginx image, version 1.25
nginx:latest              → official nginx image, latest version

The latest tag is the default — if you do not specify a tag, Docker uses latest. Relying on latest in production is risky because it changes when new versions are published. Always use explicit version tags in production environments.

Building With Different Tags

You can tag the same image multiple times:

docker build -t my-python-app:1.0 .
docker build -t my-python-app:latest .

Or tag an existing image without rebuilding:
docker tag my-python-app:1.0 myusername/my-python-app:1.0

Listing and Inspecting Images

See all images on your machine:

docker images

REPOSITORY        TAG       IMAGE ID       CREATED         SIZE
my-python-app     1.0       a1b2c3d4e5f6   2 minutes ago   145MB
nginx             latest    f8b2c3d4e5f7   3 days ago      187MB
python            3.11-slim e9c4d5e6f7g8   1 week ago      125MB

Inspect detailed metadata about an image:

docker inspect my-python-app:1.0

This shows the environment variables, exposed ports, default command, layers, and more.

Run Your Newly Built Image

docker run -d -p 5000:5000 --name flask-app my-python-app:1.0

Open your browser at http://localhost:5000. You see "Hello from my Docker container!" — your own Python app running inside a container you built yourself.

Removing Images

Remove a specific image:
docker rmi my-python-app:1.0

Remove all unused images (saves disk space):
docker image prune

Remove all unused images including stopped container images:
docker image prune -a

Leave a Comment