1. What is Docker?
Docker is a platform that lets you package your applications (code + dependencies + configuration) into containers — lightweight, isolated environments that run consistently anywhere (on your machine, on a server, or in the cloud).
Think of it like this:
Without Docker: “It works on my machine!”
With Docker: “It works the same everywhere!”
It uses OS-level virtualization, not full virtual machines — so containers are fast and lightweight.
2. Why use Docker?
Problem | Docker Solution |
---|---|
Different dev/prod setups | Same container image works everywhere |
Dependency conflicts | Each container has isolated environment |
Complex setup | Just docker run postgres and it works |
Scaling | Containers can be easily replicated |
CI/CD | Build once → deploy anywhere |
3. When to Use vs. When Not To Use Docker
Use Docker When:
- You want a reproducible environment.
- You need to run multiple services (DB, API, frontend) locally.
- You deploy microservices or cloud-native apps.
- You want simple dev/test setups.
Avoid Docker When:
- You only run a single, simple app (no dependencies).
- You need a full OS or GUI apps (containers are headless).
- You’re working in highly resource-limited environments.
- You need ultra-low-level hardware access.
4. Image vs. Container
Term | Description | Analogy |
---|---|---|
Image | A blueprint — immutable snapshot of an environment | A “class” or “template” |
Container | A running instance of an image | An “object” created from a class |
Relationship:
You create containers from images.
You can have multiple containers from the same image.
Example:
docker pull postgres
docker run -d --name db1 postgres
docker run -d --name db2 postgres
Here both db1
and db2
come from the same postgres
image.
5. Basic Docker Commands Cheat Sheet
List things
# List all images
docker images
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
a. Pull an image (e.g. PostgreSQL)
docker pull postgres
This downloads the image from Docker Hub.
b. Create & run a container
docker run -d \
--name my-postgres \
-e POSTGRES_PASSWORD=secret \
-p 5432:5432 \
postgres
Explanation:
-
-d
→ detached (background) -
--name my-postgres
→ give it a name -
-e
→ set environment variable -
-p 5432:5432
→ map container port → host port -
postgres
→ the image name
c. Reuse a container
# Stop
docker stop my-postgres
# Start again
docker start my-postgres
Docker containers can be restarted without losing data if you use volumes, otherwise data is wiped when removed.
d. Enter a container terminal
docker exec -it my-postgres bash
# or for Alpine-based images
docker exec -it my-postgres sh
-it
means interactive terminal.
e. See container logs
docker logs my-postgres
docker logs -f my-postgres # follow live logs
f. Remove a container
docker rm my-postgres
# If it’s running, first stop:
docker stop my-postgres && docker rm my-postgres
g. Remove an image
docker rmi postgres
If containers still use it, you’ll need -f
(force):
docker rmi -f postgres
h. What is a Docker Network?
A Docker network connects containers together so they can talk to each other by name.
- Default: all containers use the same
bridge
network. - You can create custom networks for isolation and internal DNS.
When to use:
- When running multiple containers (e.g., API + DB).
- When you don’t want to expose all ports to the host.
i. Create and connect containers to a network
# Create network
docker network create mynet
# Run containers on it
docker run -d --name db --network mynet postgres
docker run -d --name app --network mynet my-spring-app
Now your app
container can connect to Postgres via db:5432
instead of localhost
.
Bonus: Cleaning up
docker system prune # Remove unused containers/images/networks
docker system prune -a # Remove EVERYTHING unused
Summary Mindmap
Docker
├── Images (templates)
│ └── Created from Dockerfile or pulled from Docker Hub
├── Containers (instances of images)
│ ├── Start/stop/restart
│ ├── Exec into (bash)
│ └── Logs and volumes
├── Networks
│ └── Connect containers by name
└── Commands
├── docker images / ps / pull / run / stop / rm / logs
└── docker network create / connect
Top comments (0)