2.Images & Containers

📦 Docker Images

A Docker image is like a blueprint.
It contains everything needed to run an application:

Images are read-only and built in layers, which makes them fast and efficient.


Common Image Commands

To list all images on your system:

docker image ls
docker image ls

To download an image from Docker Hub:

docker pull ubuntu
docker pull ubuntu

To see how an image is built layer by layer:

docker history ubuntu
docker history ubuntu

To remove an image:

docker rmi ubuntu
docker rmi ubuntu

Image Example (Real Life)

Think of an image like a cake recipe 🍰.
You don’t eat the recipe — you use it to bake the cake.

In Docker:


🏃 Docker Containers

A container is a running instance of an image.
When you start an image, Docker creates a container and adds a read-write layer on top.

Containers can be:


Common Container Commands

To create and run a container:

docker run ubuntu
docker run ubuntu

To run a container in the background:

docker run -d nginx
docker run -d nginx

To list running containers:

docker ps
docker ps

To list all containers (running + stopped):

docker ps -a
docker ps -a

To enter inside a running container:

docker exec -it <container_id> /bin/bash
docker exec -it <container_id> /bin/bash

To view container logs:

docker logs <container_id>
docker logs <container_id>

Create vs Run

docker create ubuntu
docker create ubuntu

👉 Creates the container but does not start it.

docker start <container_id>
docker start <container_id>

👉 Starts an already created container.


Stop and Remove Containers

To stop a running container:

docker stop <container_id>
docker stop <container_id>

To remove a container:

docker rm <container_id>
docker rm <container_id>

Container Example (Real Life)

Imagine a mobile app installer:

You can install the same app many times → multiple containers from one image.


Key Difference