0
0
Dockerdevops~5 mins

Why images are blueprints for containers in Docker - Why It Works

Choose your learning style9 modes available
Introduction
When you want to run software in a container, you need a recipe that tells the computer how to build it. Docker images are like blueprints that describe exactly what goes inside a container. They make sure every container starts the same way, with the same software and settings.
When you want to create a reusable setup for your app that can run anywhere.
When you need to share your app environment with teammates or deploy it on servers.
When you want to make sure your app runs the same on your laptop and in the cloud.
When you want to quickly start multiple copies of your app with the same setup.
When you want to save time by not installing software every time you start a container.
Commands
This command downloads the official nginx image version 1.23.3 from Docker Hub. It is the blueprint for creating containers running the nginx web server.
Terminal
docker pull nginx:1.23.3
Expected OutputExpected
1.23.3: Pulling from library/nginx Digest: sha256:3f7a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 Status: Downloaded newer image for nginx:1.23.3 docker.io/library/nginx:1.23.3
This command lists all downloaded images with the name nginx. It shows the image ID, tag, and size, confirming the blueprint is ready to use.
Terminal
docker images nginx
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.23.3 4bb46517cac3 2 weeks ago 133MB
This command creates and starts a container named my-nginx from the nginx image blueprint. It runs in the background and maps port 8080 on your computer to port 80 inside the container.
Terminal
docker run --name my-nginx -d -p 8080:80 nginx:1.23.3
Expected OutputExpected
a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0
--name - Assigns a name to the container for easy reference
-d - Runs the container in detached (background) mode
-p - Maps a port on the host to a port in the container
This command lists all running containers. It shows the container ID, image used, status, and port mappings, confirming the container is running from the image blueprint.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1b2c3d4e5f6 nginx:1.23.3 "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp my-nginx
Key Concept

Docker images are the fixed recipes that define what software and settings a container will have before it runs.

Common Mistakes
Trying to run a container without pulling the image first.
Docker will try to download the image automatically, but if the image name or tag is wrong, it will fail.
Always pull the correct image with the right tag before running a container, or verify the image exists locally.
Using the 'latest' tag without specifying a version.
The 'latest' tag can change over time, causing unexpected differences in container behavior.
Use specific version tags like '1.23.3' to ensure consistent container setups.
Not mapping ports when running a container that serves web traffic.
Without port mapping, you cannot access the container's service from your computer.
Use the '-p' flag to map container ports to host ports.
Summary
Docker images act as blueprints that define what goes inside a container.
You pull images to download these blueprints and then run containers from them.
Running containers from images ensures your app runs the same everywhere.