Package your application and everything it needs.
Docker runs applications inside containers.
Instead of:
"Works on my computer."
you can package the application with its environment.
1. Check Docker
docker --version
2. Run a Container
docker run nginx
Docker downloads the image if necessary and starts a container.
Run it in the background:
docker run -d nginx
-d = detached.
3. See Running Containers
docker ps
See all containers, including stopped ones:
docker ps -a
4. Stop a Container
docker stop CONTAINER_ID
Start it again:
docker start CONTAINER_ID
5. Remove a Container
docker rm CONTAINER_ID
6. Images vs Containers
This distinction is important.
Image
Blueprint
Container
Running instance of that blueprint
For example:
nginx image
↓
nginx container
7. See Images
docker images
Remove an image:
docker rmi IMAGE_ID
8. View Container Logs
docker logs CONTAINER_ID
Follow the logs:
docker logs -f CONTAINER_ID
This is one of the most useful Docker commands when debugging.
9. Run a Container with a Port
A container might listen on port 80.
Expose it on your machine:
docker run -d -p 8080:80 nginx
Think:
Your machine : 8080
↓
Container : 80
Then visit:
http://localhost:8080
10. Docker Compose
When an application needs several services:
Application
Database
Redis
etc.
Docker Compose lets you define them together.
Start:
docker compose up -d
Stop:
docker compose down
See services:
docker compose ps
View logs:
docker compose logs
The Commands to Remember
| What you want | Command |
|---|---|
| Check Docker | docker --version |
| Run container | docker run |
| Running containers | docker ps |
| All containers | docker ps -a |
| Stop | docker stop |
| Start | docker start |
| Remove container | docker rm |
| Images | docker images |
| Remove image | docker rmi |
| Logs | docker logs |
| Port mapping | docker run -p |
| Start Compose | docker compose up -d |
| Stop Compose | docker compose down |
Remember
Docker Image
↓
Docker Container
↓
Your Application
And for multi-service applications:
docker compose up -d
is often where the magic starts.
