🐳 Docker Learning Hub
Lesson 2 • Images, containers, Dockerfile
Lesson 2

Images, containers, and Dockerfile

This page moves from the basic idea of Docker into the mechanics of how Docker packages and runs applications.

Docker Image

A reusable blueprint that contains code, dependencies, runtime, and setup details.

Docker Container

A running instance created from an image.

Dockerfile

A text file that gives Docker the instructions needed to build an image.

Container lifecycle

Created
Docker prepares the container.
Running
The application is active.
Stopped
The container still exists but is not active.
Removed
The container is deleted.

Build and run flow

Dockerfile defines the packaging steps
docker build creates the image
docker run starts the container
Client accesses the running application

Simple Dockerfile example

If you want Nginx to serve your custom index.html, this is enough to create a small image:

FROM nginx:latest COPY index.html /usr/share/nginx/html/index.html
docker build -t my-nginx-app . docker run -d -p 8080:80 my-nginx-app

Common Dockerfile instructions

FROM
Choose a base image.
COPY
Move files into the image.
WORKDIR
Set the working folder.
RUN
Execute commands during build.
CMD
Set the startup command.

RUN vs CMD

RUN is used at build time. CMD is used when the container starts.

Easy rule: RUN = build time, CMD = start time.

Deleting a container vs deleting an image

Removing a container does not remove the image. The image stays on your machine until you delete it separately.

Why Docker layers matter

Each major Dockerfile step can create a layer. Docker can reuse unchanged layers, which makes builds faster.

1. FROM node:18
Base runtime layer
2. WORKDIR /app
Set working directory
3. COPY package.json
Add dependency file
4. RUN npm install
Install dependencies
5. COPY . /app
Add the rest of the code

Next page: Lesson 3 turns these concepts into a real step-by-step app build.