Skip to main content

๐Ÿณ Docker Foundations

Let's start from "What is Docker?", assuming you've never seen Docker before.

The goal of this lesson is not to memorize commands. By the end, you should understand:

  • What Docker is
  • Why it exists
  • What a container is
  • What an image is
  • Container vs VM
  • Docker Engine
  • Docker Hub
  • Dockerfile
  • Registry
  • Basic Docker architecture
  • Your first Docker commands

1. ๐Ÿง’ Imagine You Built an App

Suppose you write a small Python application.

It works perfectly on your computer.

You send it to your friend.

Your friend runs it and gets:

ERROR
Python version mismatch

You fix that.

Then another person tries it:

ERROR
Missing library

Then you deploy it to a server:

ERROR
Configuration is different

๐Ÿ˜ญ

The problem is:

Your application depends on its environment.

Your application might need:

Python 3.12
Flask 3.x
requests
specific system libraries
environment variables
configuration

Your computer has all of these.

Another computer might not.


2. ๐Ÿ“ฆ Docker's Big Idea

Docker basically says:

"Let's package the application together with everything it needs so it can run consistently."

Think about moving houses.

Instead of saying:

"Here is my furniture. Good luck figuring out how to arrange everything."

You put everything into a standardized container:

๐Ÿ“ฆ Application
๐Ÿ“ฆ Libraries
๐Ÿ“ฆ Dependencies
๐Ÿ“ฆ Configuration

Then you can move that container between environments.

Your laptop
โ†“
Docker
โ†“
๐Ÿ“ฆ Container
โ†“
Cloud server

That's the basic idea.


3. ๐Ÿณ So What Exactly Is Docker?

Docker is a platform for building, packaging, distributing, and running applications in containers.

The important word is:

Container

A container is an isolated environment where an application runs.

For example:

๐Ÿ“ฆ Container
โ”‚
โ”œโ”€โ”€ Python
โ”œโ”€โ”€ Flask
โ”œโ”€โ”€ Application code
โ”œโ”€โ”€ Dependencies
โ””โ”€โ”€ Configuration

Docker runs that container using the host operating system's kernel.


4. ๐Ÿ  Container = Small Isolated Environment

Imagine an apartment building.

The building is the computer.

Each apartment is a container.

๐Ÿข Linux Host
โ”‚
โ”œโ”€โ”€ ๐Ÿ  Container A
โ”‚ โ””โ”€โ”€ Python application
โ”‚
โ”œโ”€โ”€ ๐Ÿ  Container B
โ”‚ โ””โ”€โ”€ Node.js application
โ”‚
โ””โ”€โ”€ ๐Ÿ  Container C
โ””โ”€โ”€ Nginx

Each container is isolated from the others.

They can have different:

  • Applications
  • Libraries
  • Dependencies
  • Configuration

Yet they can all run on the same host.


5. ๐Ÿค” Why Not Just Use Virtual Machines?

Excellent question.

Before containers became extremely popular, we commonly used virtual machines (VMs).

A VM looks roughly like:

Physical Computer
โ”‚
โ”œโ”€โ”€ Host OS
โ”‚
โ”œโ”€โ”€ VM 1
โ”‚ โ”œโ”€โ”€ Guest OS
โ”‚ โ””โ”€โ”€ Application
โ”‚
โ”œโ”€โ”€ VM 2
โ”‚ โ”œโ”€โ”€ Guest OS
โ”‚ โ””โ”€โ”€ Application
โ”‚
โ””โ”€โ”€ VM 3
โ”œโ”€โ”€ Guest OS
โ””โ”€โ”€ Application

Each VM has its own operating system.

That can be relatively heavy.

Containers work differently:

Physical Computer
โ”‚
โ”œโ”€โ”€ Host OS
โ”‚
โ”œโ”€โ”€ Docker
โ”‚
โ”œโ”€โ”€ Container 1
โ”‚ โ””โ”€โ”€ Application
โ”‚
โ”œโ”€โ”€ Container 2
โ”‚ โ””โ”€โ”€ Application
โ”‚
โ””โ”€โ”€ Container 3
โ””โ”€โ”€ Application

Containers share the host's kernel.

So they are generally:

  • Faster to start
  • More lightweight
  • Easier to package
  • Easier to deploy at scale

6. ๐Ÿง  Container vs Virtual Machine

Here's the mental model you need:

Virtual MachineContainer
Contains a guest OSShares host kernel
Usually heavierUsually lighter
Slower startupUsually very fast startup
More resource overheadLess resource overhead
Strong OS-level isolationProcess-level isolation
VM imageContainer image

Don't interpret this as:

"Containers are always better than VMs."

They're different technologies with different strengths.

And in real cloud environments, containers frequently run inside VMs.

For example:

Cloud
โ”‚
โ””โ”€โ”€ Virtual Machine
โ”‚
โ””โ”€โ”€ Docker
โ”‚
โ”œโ”€โ”€ Container
โ”œโ”€โ”€ Container
โ””โ”€โ”€ Container

7. ๐Ÿงฉ The Most Important Docker Concepts

You need to understand these six words:

Image
Container
Dockerfile
Docker Engine
Registry
Docker Compose

We'll take them one by one.


8. ๐Ÿ“ธ What Is a Docker Image?

An image is a packaged, immutable template used to create containers.

Think:

๐Ÿง Image = recipe/template

๐Ÿฐ Container = actual running thing created from that template

For example:

Docker Image
โ†“
run
โ†“
Container

You might have an image called:

nginx

You can create multiple containers from it:

nginx image
โ”‚
โ”œโ”€โ”€ Container 1
โ”œโ”€โ”€ Container 2
โ””โ”€โ”€ Container 3

The image itself isn't the running application.

It's the blueprint.


9. ๐Ÿ“ฆ What Is a Container?

A container is a running instance of an image.

Think:

Image
โ†“
"Make me a container"
โ†“
Container

For example:

nginx image
โ†“
nginx container

You can have:

Image: nginx

Containers:
โ”œโ”€โ”€ website-1
โ”œโ”€โ”€ website-2
โ””โ”€โ”€ website-3

10. ๐Ÿ“ What Is a Dockerfile?

Now we get to something extremely important.

Suppose you want to create your own Docker image.

You need to tell Docker:

"Start with this base environment, copy my application, install these dependencies, and run this command."

You write those instructions in a:

Dockerfile

For example:

FROM python:3.12

WORKDIR /app

COPY . .

RUN pip install -r requirements.txt

CMD ["python", "app.py"]

Don't worry about understanding every line yet.

We'll study Dockerfiles deeply later.

For now:

Dockerfile
โ†“
docker build
โ†“
Docker Image
โ†“
docker run
โ†“
Container

This relationship is fundamental.


11. ๐Ÿญ Docker's Factory Analogy

Think about a factory.

Dockerfileโ€‹

๐Ÿ“‹ Instructions for making the product.

Imageโ€‹

๐Ÿ“ฆ Finished packaged product.

Containerโ€‹

๐Ÿš€ Product currently being used.

So:

Dockerfile
โ†“
BUILD
โ†“
Image
โ†“
RUN
โ†“
Container

Memorize this.


12. ๐ŸŒŽ What Is a Docker Registry?

Suppose you've created an image on your laptop.

Now you want your cloud server to use it.

How do you get it there?

You can upload it to a container registry.

A registry is basically a place where container images are stored and distributed.

One popular registry is:

Docker Hub

Other examples include cloud-provider registries such as:

  • Amazon ECR
  • Azure Container Registry
  • Google Artifact Registry

Conceptually:

Your computer
โ”‚
โ”‚ docker push
โ†“
Registry
โ”‚
โ”‚ docker pull
โ†“
Cloud server

13. ๐Ÿณ What Is Docker Hub?

Docker Hub is a public registry containing Docker images.

You can find images for things like:

nginx
redis
postgres
python
node
ubuntu

Instead of building everything from scratch, you can use an existing image as your starting point.

For example:

FROM python:3.12

means:

"Use the Python 3.12 image as my starting point."


14. โš™๏ธ What Is Docker Engine?

Docker needs something to actually manage containers.

That's where the Docker Engine comes in.

Simplified:

You
โ”‚
โ”‚ docker run
โ†“
Docker CLI
โ”‚
โ†“
Docker Engine
โ”‚
โ”œโ”€โ”€ Images
โ”œโ”€โ”€ Containers
โ”œโ”€โ”€ Networks
โ””โ”€โ”€ Volumes

The Docker Engine is the underlying technology that creates and manages containers.


15. ๐Ÿ’ป Docker CLI

When you type:

docker run nginx

you're using the Docker command-line interface (CLI).

The CLI communicates with the Docker Engine.

Think:

You
โ†“
Docker CLI
โ†“
Docker Engine
โ†“
Container

16. ๐Ÿš€ Your First Docker Command

Once Docker is installed, try:

docker --version

You might get something like:

Docker version 27.x.x

That tells you Docker is installed.

Then:

docker info

This gives information about your Docker installation and engine.


17. ๐Ÿ“‹ See Your Docker Images

Run:

docker images

This shows images currently stored locally.

You might initially see:

REPOSITORY TAG IMAGE ID

Maybe nothing is there yet.

That's completely fine.


18. ๐Ÿ“ฆ Download an Image

Let's download an Nginx image:

docker pull nginx

Think:

"Docker, go to a registry and download the nginx image."

Then:

docker images

You should now see something like:

REPOSITORY TAG IMAGE ID
nginx latest xxxxxxxxx

You've downloaded your first Docker image.


19. ๐Ÿš€ Create Your First Container

Now run:

docker run nginx

What's happening?

nginx image
โ†“
Docker creates
โ†“
nginx container
โ†“
nginx starts

If everything works, Nginx starts running inside the container.

But there's an interesting problem.

Your terminal may appear to be "stuck."

It's not actually stuck.

The container is running in the foreground.

Press:

Ctrl + C

to stop it.


20. ๐Ÿƒ Run It in the Background

Usually, servers need to keep running while you continue using your terminal.

Use:

docker run -d nginx

-d means:

detached mode

In simple terms:

"Run this container in the background."

Docker will return a container ID.


21. ๐Ÿ‘€ See Running Containers

Now:

docker ps

You'll see something similar to:

CONTAINER ID IMAGE COMMAND STATUS
abc123 nginx ... Up ...

This means:

"Show me currently running containers."


22. ๐Ÿ›‘ Stop a Container

Suppose the container ID is:

abc123

You can stop it:

docker stop abc123

Or you can use its name:

docker stop <container-name>

Then:

docker ps

It won't appear because it isn't running anymore.


23. ๐Ÿ‘€ Where Did My Container Go?

Run:

docker ps -a

The -a means:

Show all containers, including stopped ones.

You'll see your previous container.

This distinction is important:

docker ps

= running containers

docker ps -a

= all containers


24. ๐Ÿ—‘๏ธ Remove a Container

If you don't need the stopped container anymore:

docker rm <container-id>

Then:

docker ps -a

It should be gone.


25. ๐Ÿง  Important Docker Commands

For now, memorize only these:

docker --version
docker info
docker images
docker pull
docker run
docker ps
docker ps -a
docker stop
docker rm

Don't try to memorize 100 Docker commands.

Understand what each one means.


26. ๐ŸŒ But How Do I Access Nginx?

This is where Docker networking begins.

If you run:

docker run -d nginx

Nginx is running inside the container.

That doesn't automatically mean you can access it from your browser through your computer's port 80.

We need port mapping.

Run:

docker run -d -p 8080:80 nginx

The important part is:

8080:80

It means:

Host port 8080
โ†“
Container port 80

Think:

Your computer
localhost:8080
โ”‚
โ†“
Docker
โ”‚
โ†“
Container
port 80
โ”‚
โ†“
Nginx

Now open:

http://localhost:8080

in your browser.

You should see the Nginx welcome page.

๐ŸŽ‰ You just exposed a containerized web server to your computer.


27. ๐Ÿง  Port Mapping

This syntax:

-p HOST_PORT:CONTAINER_PORT

is incredibly important.

For example:

-p 8080:80

means:

Host Container
8080 โ†’ 80

Another example:

-p 3000:3000

means:

Host Container
3000 โ†’ 3000

28. ๐Ÿ” See Container Details

You can inspect a container:

docker inspect <container-id>

This gives you a huge amount of information.

For example:

  • Network configuration
  • IP address
  • Mounts
  • Environment
  • Image
  • Ports
  • Runtime configuration

We'll use this heavily later for troubleshooting.


29. ๐Ÿ“œ View Container Logs

Suppose your application is running but something isn't working.

You can look at its logs:

docker logs <container-id>

This is extremely important.

Think:

"Container, tell me what happened."

For example:

docker logs my-container

We'll later use this for debugging real applications.


30. ๐Ÿ–ฅ๏ธ Execute a Command Inside a Container

You can enter a running container using:

docker exec -it <container-id> bash

Sometimes the container doesn't have bash, so you'll use:

docker exec -it <container-id> sh

Now you're effectively inside the container's environment.

For example:

ls

might show:

bin
etc
usr
var
...

You can then exit:

exit

This is extremely useful for troubleshooting.


๐Ÿง  The Docker Mental Model

This is what I want you to understand before we go any further:

Docker
โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ†“ โ†“ โ†“
Images Containers Networks
โ”‚ โ”‚
โ”‚ โ†“
โ”‚ Running
โ”‚ applications
โ”‚
โ†“
Built from
Dockerfile
โ”‚
โ†“
Registry

And the most important pipeline:

Dockerfile
โ”‚
โ”‚ docker build
โ†“
Image
โ”‚
โ”‚ docker run
โ†“
Container
โ”‚
โ”‚ docker push
โ†“
Registry
โ”‚
โ”‚ docker pull
โ†“
Another machine

๐ŸŽฏ Your First Docker Lab

If Docker is installed, don't just read. Do this yourself.

Step 1โ€‹

docker --version

Step 2โ€‹

docker pull nginx

Step 3โ€‹

docker images

Step 4โ€‹

docker run -d --name my-nginx -p 8080:80 nginx

Notice that we gave our container a name:

my-nginx

Now:

docker ps

Step 5โ€‹

Open:

http://localhost:8080

Step 6โ€‹

Check its logs:

docker logs my-nginx

Step 7โ€‹

Inspect it:

docker inspect my-nginx

Step 8โ€‹

Enter the container:

docker exec -it my-nginx bash

Then:

ls

and:

cat /etc/os-release

Then:

exit

Step 9โ€‹

Stop it:

docker stop my-nginx

Step 10โ€‹

Check all containers:

docker ps -a

Step 11โ€‹

Remove it:

docker rm my-nginx

๐Ÿงช Challenge โ€” Don't Look Up the Commands

After completing the lab, see if you can answer these without looking back:

1. What is Docker?โ€‹

2. What is a container?โ€‹

3. What is an image?โ€‹

4. What's the difference between an image and a container?โ€‹

5. What creates an image?โ€‹

6. Where can Docker images be stored/shared?โ€‹

7. What does this mean?โ€‹

-p 8080:80

8. What's the difference between:โ€‹

docker ps

and:

docker ps -a

9. What does this do?โ€‹

docker logs my-nginx

10. What does this do?โ€‹

docker exec -it my-nginx bash