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.
To list all images on your system:
docker image ls
docker image lsTo download an image from Docker Hub:
docker pull ubuntu
docker pull ubuntuTo see how an image is built layer by layer:
docker history ubuntu
docker history ubuntuTo remove an image:
docker rmi ubuntu
docker rmi ubuntuThink of an image like a cake recipe 🍰.
You don’t eat the recipe — you use it to bake the cake.
In Docker:
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:
To create and run a container:
docker run ubuntu
docker run ubuntuTo run a container in the background:
docker run -d nginx
docker run -d nginxTo list running containers:
docker ps
docker psTo list all containers (running + stopped):
docker ps -a
docker ps -aTo enter inside a running container:
docker exec -it <container_id> /bin/bash
docker exec -it <container_id> /bin/bashTo view container logs:
docker logs <container_id>
docker logs <container_id>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.
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>Imagine a mobile app installer:
You can install the same app many times → multiple containers from one image.