Docker for Beginners: Everything I Learned about Images

Source: Dev.to | Dec 30, 2025 (UTC)

My notes from learning Docker — explained simply for absolute beginners

🐳 This is Part 2 of my Docker series!

In my previous post, I covered the fundamentals:

  • What is Docker and why use it
  • Containers vs Virtual Machines
  • Installing Docker
  • Images vs Containers
  • Docker Hub and Registries
  • Essential commands (pull, run, stop, ps)
  • Port binding

👉 Read Part 1: Docker for Beginners: A Complete Guide to Containerisation

What You’ll Learn in This Post

Now that you understand Docker basics, it’s time to build your own images! In this post, I’ll cover:

  • 📄 Dockerfile — Writing instructions to create images
  • 🏗️ FROM — Choosing a base image
  • ⚙️ RUN — Installing packages during build
  • 📁 COPY — Adding your files to the image
  • 🌐 Downloading files — Using curl efficiently
  • 🚀 CMD — Setting the startup command
  • 🔧 Overriding commands — Running containers interactively

By the end, you’ll be able to create your own custom Docker images from scratch!

📚 Table of Contents

  1. Creating Images with Dockerfile
  2. The FROM Instruction
  3. Building a Dockerfile
  4. Naming Your Image with Tags
  5. The RUN Instruction
  6. COPYing Files into an Image
  7. Downloading Files in Docker
  8. The CMD Instruction
  9. Overriding the Default Start Command
  10. Quick Reference Cheat Sheet

Creating Images with Dockerfile

A Dockerfile is a text file with instructions to build a Docker image.

The Flow

Dockerfile → Build → Docker Image → Run → Container 📄 🔨 📀 ▶️ 📦 Recipe Cooking Frozen Meal Heat Hot Meal

Enter fullscreen mode

Exit fullscreen mode

The FROM Instruction

Every Dockerfile must start with FROM. It tells Docker what base image to use.

FROM ubuntu FROM python:3.11 FROM node:18-alpine

Enter fullscreen mode

Exit fullscreen mode

With Specific Versions (Recommended)

FROM ubuntu:22.04 FROM python:3.11.4 FROM node:18.17.0-alpine

Enter fullscreen mode

Exit fullscreen mode

💡 Always use specific versions in production to avoid unexpected changes!

Why Use a Base Image?

Starting Point

What You Get

FROM ubuntu

Empty OS, install everything yourself

FROM python:3.11

Python already installed

FROM node:18

Node.js already installed

FROM postgres:15

Full database ready to use

Building a Dockerfile

The Build Command

docker build .

Enter fullscreen mode

Exit fullscreen mode

The . (dot) means “look for Dockerfile in current folder”.

What the Dot Means

Command

Where Docker Looks

docker build .

Current folder

docker build ./app

The app subfolder

docker build ..

Parent folder

Build Output

$ docker build . [+] Building 0.1s (5/5) FINISHED => [internal] load build definition from Dockerfile => => transferring dockerfile: 54B => CACHED [1/1] FROM docker.io/library/ubuntu => exporting to image => => writing image sha256:a67f41b1d127160a7647b6709...

Enter fullscreen mode

Exit fullscreen mode

Naming Your Image with Tags

Without a name, your image gets a random ID like sha256:a67f41b1d127... 😅

Use the -t Flag

docker build -t my-app .

Enter fullscreen mode

Exit fullscreen mode

Add Version Numbers

docker build -t my-app:v1.0 . docker build -t my-app:v1.1 . docker build -t my-app:latest .

Enter fullscreen mode

Exit fullscreen mode

Real Example

# Build and name it docker build -t my-python-app:v1 . # Now you can run it easily docker run my-python-app:v1

Enter fullscreen mode

Exit fullscreen mode

The RUN Instruction

RUN executes commands during the build process.

RUN <shell-command>

Enter fullscreen mode

Exit fullscreen mode

Example: Installing Python

FROM ubuntu:22.04 RUN apt-get update RUN apt-get install -y python3

Enter fullscreen mode

Exit fullscreen mode

What Does apt-get update Do?

It refreshes the package list (catalog of available software):

Command

What It Does

Analogy

apt-get update

Get latest list of packages

Get the restaurant menu

apt-get install python3

Install the package

Order from the menu

The -y Flag

RUN apt-get install -y python3

Enter fullscreen mode

Exit fullscreen mode

-y = “Yes to all prompts”

Without it, Docker would wait for you to type “Y” (which you can’t do during build!).

Combine Commands for Smaller Images

# ❌ BAD - Creates 3 layers RUN apt-get update RUN apt-get install -y python3 RUN apt-get install -y curl # ✅ GOOD - Creates 1 layer RUN apt-get update && apt-get install -y python3 curl

Enter fullscreen mode

Exit fullscreen mode

COPYing Files into an Image

COPY takes files from your computer and puts them inside the image.

COPY <source-on-computer> <destination-in-image>

Enter fullscreen mode

Exit fullscreen mode

Copy a Single File

COPY app.py /app/app.py

Enter fullscreen mode

Exit fullscreen mode

Copy to Folder (Keeps Same Name)

COPY app.py /app/ # Result: /app/app.py

Enter fullscreen mode

Exit fullscreen mode

Copy Entire Folder

COPY src/ /app/ # Copies everything in src/ to /app/

Enter fullscreen mode

Exit fullscreen mode

⚠️ Important Rule: No Parent Directory Access

# ❌ This will FAIL COPY ../secret-file.txt /app/ # Docker can't access files ABOVE the Dockerfile location (security!)

Enter fullscreen mode

Exit fullscreen mode

Visual Example

Your Computer: Inside Image: my-project/ /app/ ├── Dockerfile ├── main.py ├── main.py ─COPY→ ├── utils.py └── utils.py └── config.py

Enter fullscreen mode

Exit fullscreen mode

Downloading Files in Docker

Instead of COPY, you can download files from the internet:

Three Steps

# 1. Download RUN curl https://example.com/file.zip -o /tmp/file.zip # 2. Unzip RUN unzip /tmp/file.zip -d /app/ # 3. Remove zip (cleanup) RUN rm /tmp/file.zip

Enter fullscreen mode

Exit fullscreen mode

⚡ Efficient Way (Single Instruction)

Each RUN creates a layer. Even deleted files add to image size!

# ❌ BAD - 3 layers, zip still counts toward size RUN curl https://example.com/file.zip -o /tmp/file.zip RUN unzip /tmp/file.zip -d /app/ RUN rm /tmp/file.zip # ✅ GOOD - 1 layer, zip deleted before layer saves RUN curl https://example.com/file.zip -o /tmp/file.zip \ && unzip /tmp/file.zip -d /app/ \ && rm /tmp/file.zip

Enter fullscreen mode

Exit fullscreen mode

What Does && Mean?

&& = “then do this” (runs next command only if previous succeeded)

What Does \ Mean?

\ = “continue on next line” (makes long commands readable)

The CMD Instruction

CMD tells Docker what command to run when the container starts.

CMD <shell-command>

Enter fullscreen mode

Exit fullscreen mode

RUN vs CMD

Instruction

When It Runs

Purpose

RUN

During build

Install software, setup

CMD

At startup

Start your application

Examples

# Run a Python script CMD python3 app.py # Start a web server CMD npm start # Run a shell script CMD ./start.sh

Enter fullscreen mode

Exit fullscreen mode

Key Points About CMD

Complete Example

FROM python:3.11 COPY app.py /app/ WORKDIR /app CMD python3 app.py

Enter fullscreen mode

Exit fullscreen mode

When you run this:

docker run my-app # Automatically executes: python3 app.py

Enter fullscreen mode

Exit fullscreen mode

Overriding the Default Start Command

You can replace the CMD when running a container:

Default (Uses CMD)

docker run my-image # Runs whatever CMD is in the Dockerfile

Enter fullscreen mode

Exit fullscreen mode

Override with Your Command

docker run my-image echo "Hello!" # Ignores CMD, runs "echo Hello!" instead

Enter fullscreen mode

Exit fullscreen mode

Interactive Mode

docker run -it ubuntu bash

Enter fullscreen mode

Exit fullscreen mode

Flag

Meaning

-i

Interactive – keeps input open

-t

Terminal – shows proper console

This opens a shell inside the container:

$ docker run -it ubuntu bash root@abc123:/# ls bin boot dev etc home lib ... root@abc123:/# echo "I'm inside the container!" I'm inside the container! root@abc123:/# exit $

Enter fullscreen mode

Exit fullscreen mode

When to Use Override?

Use Case

Command

Debug inside container

docker run -it my-image bash

Run different script

docker run my-image python3 other.py

Check files inside

docker run my-image ls /app

Quick Reference Cheat Sheet

Dockerfile Instructions

Instruction

Purpose

Example

FROM

Base image

FROM ubuntu:22.04

RUN

Run command during build

RUN apt-get install -y python3

COPY

Copy files into image

COPY app.py /app/

CMD

Command to run at startup

CMD python3 app.py

WORKDIR

Set working directory

WORKDIR /app

Docker CLI Commands

Command

What It Does

docker build -t name .

Build image from Dockerfile

docker run image

Run a container

docker run -it image bash

Run with interactive shell

docker images

List all images

docker ps

List running containers

docker pull image:tag

Download an image

Example Dockerfile

# Start from Python base image FROM python:3.11-slim # Set working directory WORKDIR /app # Copy requirements first (for caching) COPY requirements.txt . # Install dependencies RUN pip install -r requirements.txt # Copy application code COPY . . # Command to run at startup CMD ["python", "app.py"]

Enter fullscreen mode

Exit fullscreen mode

Build and Run

# Build the image docker build -t my-python-app:v1 . # Run the container docker run my-python-app:v1 # Run with interactive shell (for debugging) docker run -it my-python-app:v1 bash

Enter fullscreen mode

Exit fullscreen mode

Summary

Today I learned:

✅ Dockerfile is a recipe for building images

✅ FROM sets the base image (always first!)

✅ RUN executes commands during build

✅ COPY puts files from your computer into the image

✅ CMD runs when the container starts

✅ docker build -t name . builds and names your image

✅ docker run -it image bash lets you explore inside a container

✅ Use && to combine commands for smaller images

📬 Keep Learning With Me!

I’m documenting my learning journey in public. If you found this helpful, check out my blog for more tutorials:

👉 kerempakten.dev

You’ll find:

I’m a recent Computer Science graduate from King’s College London, learning and sharing everything along the way. No gatekeeping — just practical knowledge for beginners like us!

Let’s connect and learn together! 🚀

Found an error or have a suggestion? Drop a comment below!

For further actions, you may consider blocking this person and/or reporting abuse

Залишити відповідь

Ваша e-mail адреса не оприлюднюватиметься. Обов’язкові поля позначені *

Читайте також