Docker & Containers

Introduction to Docker

Lesson 1 of 9 · 6 min · Beginner

If you’ve ever faced the dreaded “It works on my machine but not on yours” problem — Docker is the hero you need. It packages your code, PHP runtime, and all dependencies into an isolated environment.

The Engine Under the Hood

Docker operates on a client-server architecture. They communicate via REST API, Unix sockets, or a network interface.

Analogy: You (the client) say “Make tea!” and the kitchen (the daemon) makes it.

Docker Client

The tool we use to interact with Docker. It runs commands like docker run or docker pull and can communicate with local or remote Daemons.

Docker Daemon

The engine running in the background. It is fully responsible for building images, running containers, and managing networks and volumes.

Core Puzzle Pieces

Dockerfile
FROM ubuntu
RUN apt-get update && apt-get install -y nginx
CMD ["nginx", "-g", "daemon off;"]

The Container Lifecycle

  • pull Download a read-only Image from a Registry (like Docker Hub).

    docker pull ubuntu

  • create Initialize a Container, adding a temporary read-write layer on top of the image.

    Ephemeral by default

  • network Connect the container to a virtual network and assign it a unique IP.

    Bridged networking

  • interact Execute your code, open a terminal, or expose ports to the host machine.

    docker run -p 80:80

Common Docker Commands

Whether you are building infrastructure or troubleshooting an application, you will spend most of your time interacting with the Docker CLI using these image and container commands.

terminal

# Image Commands
$ docker image ls        → List all downloaded images
$ docker pull <image>    → Download an image from Hub
$ docker rmi <image>     → Remove an image from host
$ docker history <image> → View the layers of an image

# Container Commands
$ docker run -d -p 80:80 nginx → Run Nginx in background
$ docker ps -a                  → List all active & stopped containers
$ docker exec -it <id> /bin/bash → Open terminal inside container
$ docker logs <id>              → View standard output/error logs
$

Docker vs Virtual Machine

FeatureDocker (Container)Virtual Machine (VM)
Startup TimeSeconds Minutes
Resource UsageLightweight Heavy
OS RequirementShares Host OSNeeds Full Guest OS
PerformanceFaster Slower
IsolationProcess-levelHardware-level