47.12 All Docker commands step by step with examples
Chala mitrano! Today is one big practical. With a small web app we run all the main Docker commands in order: pull an image, run a container, look inside, logs, copy, stop, build, tag, push, network, volume and clean up. Type every command yourself.
चला मित्रांनो! आज एक मोठं practical. एक छोटं web app घेऊन Docker चे सगळे मुख्य commands क्रमाने चालवू: image आणणं, container चालवणं, आत बघणं, logs, copy, बंद करणं, build, tag, push, network, volume आणि साफ करणं. प्रत्येक command स्वतः टाइप करा.
चलो दोस्तों! आज एक बड़ा practical. एक छोटे web app से Docker के सारे मुख्य commands क्रम से चलाएँगे: image लाना, container चलाना, अंदर देखना, logs, copy, बंद करना, build, tag, push, network, volume और सफ़ाई. हर command खुद टाइप कीजिए.
1. One app, the whole life cycle
Think of a new joiner at Flipkart on day one. Her lead says: take the nginx image, run our product page in it, check the logs, fix one file, build our own image, push it, and clean up after. If she can do this, she knows Docker for daily work.
This lesson follows that same path. The commands come in the order you would really use them, not in alphabetical order. Each step has three examples, and each example shows the command, the exact output and what it means.
Keep one terminal open on an Ubuntu machine or EC2 with Docker installed, as in lesson 47.5.
2. Images
Step 1: docker pull and docker images
Why: You cannot run what you do not have.
What: docker pull downloads an image from a registry. docker images (same as docker image ls) lists the images on your machine.
Example 1: Pull a fixed version
Type this:
docker pull nginx:1.27
You will see:
1.27: Pulling from library/nginx
a2abf6c4d29d: Pull complete
Digest: sha256:0d17b565...
Status: Downloaded newer image for nginx:1.27
What it means: Each line is one layer. Always pull a fixed tag like 1.27, not latest.
Example 2: List images
Type this:
docker images
You will see:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.27 39286ab8a5e1 2 weeks ago 192MB
What it means: One image, 192 MB.
Example 3: Filter the list
Type this:
docker images --format '{{.Repository}}:{{.Tag}} {{.Size}}' nginx
You will see:
nginx:1.27 192MB
What it means: --format gives clean lines for scripts.
Step 2: docker image inspect and history
Why: Before running an image from the internet, look at what is inside.
What: inspect prints the image settings as JSON. history shows the layers and the Dockerfile line that made each one.
Example 4: Which port and command
Type this:
docker image inspect nginx:1.27 --format '{{.Config.ExposedPorts}} {{.Config.Cmd}}'
You will see:
map[80/tcp:{}] [nginx -g daemon off;]
What it means: The image listens on 80 and starts nginx.
Example 5: The layers
Type this:
docker history nginx:1.27 --format '{{.Size}}\t{{.CreatedBy}}' | head -3
You will see:
0B CMD ["nginx" "-g" "daemon off;"]
0B STOPSIGNAL SIGQUIT
0B EXPOSE map[80/tcp:{}]
What it means: Newest layer first. Size 0B lines only change settings.
Example 6: The digest
Type this:
docker image inspect nginx:1.27 --format '{{index .RepoDigests 0}}'
You will see:
nginx@sha256:0d17b565...
What it means: A digest is a fingerprint. It never changes for this exact image.
3. Containers
Step 3: docker run
Why: This is the command you will type most.
What: docker run makes a container from an image and starts it. -d runs it in the background, --name names it, and -p 8080:80 maps host port 8080 to container port 80.
Example 7: Run in the foreground
Type this:
docker run --rm alpine echo Namaste
You will see:
Namaste
What it means: The container prints and exits. --rm deletes it after.
Example 8: Run a web server
Type this:
docker run -d --name shop -p 8080:80 nginx:1.27
You will see:
4c1d9a7e2b3f...
What it means: Docker prints the new container ID and gives you the prompt back.
Example 9: Test it
Type this:
curl -s localhost:8080 | head -4
You will see:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
What it means: The page comes from inside the container.
Step 4: docker ps
Why: You need to see what is running, and what has stopped.
What: docker ps lists running containers. -a also lists stopped ones.
Example 10: Running only
Type this:
docker ps
You will see:
CONTAINER ID IMAGE COMMAND STATUS PORTS NAMES
4c1d9a7e2b3f nginx:1.27 "/docker-entrypoint.…" Up 1 minute 0.0.0.0:8080->80/tcp shop
What it means: The PORTS column confirms the mapping.
Example 11: Include stopped
Type this:
docker run --name oops alpine false
docker ps -a --format '{{.Names}} {{.Status}}'
You will see:
shop Up 2 minutes
oops Exited (1) 3 seconds ago
What it means: Exit code 1 means the program failed.
Example 12: Only IDs
Type this:
docker ps -aq
You will see:
4c1d9a7e2b3f
8b2e1f0c9d4a
What it means: -q means quiet. Useful to feed other commands.
Step 5: docker logs
Why: When a customer says the page is broken, logs are where you look first.
What: docker logs shows what the container printed. -f follows new lines, --tail shows the last few and --since limits the time.
Example 13: All logs
Type this:
docker logs shop | tail -2
You will see:
172.17.0.1 - - [25/Oct/2026:10:30:01 +0000] "GET / HTTP/1.1" 200 615
What it means: Our earlier curl shows up as a 200 OK.
Example 14: Follow live
Type this:
docker logs -f --tail 0 shop
# second terminal: curl -s localhost:8080/missing
You will see:
172.17.0.1 - - [25/Oct/2026:10:31:12 +0000] "GET /missing HTTP/1.1" 404 153
What it means: New lines appear as they happen. Press Ctrl+C to stop following.
Example 15: Last 10 minutes with times
Type this:
docker logs --since 10m -t shop | wc -l
You will see:
2
What it means: -t adds a timestamp to each line.
Step 6: docker exec
Why: Sometimes you must look inside a running container.
What: docker exec runs a new command inside a running container. -it gives you an interactive terminal.
Example 16: One command
Type this:
docker exec shop nginx -v
You will see:
nginx version: nginx/1.27.2
What it means: Runs, prints, returns.
Example 17: A shell inside
Type this:
docker exec -it shop sh
ls /usr/share/nginx/html
exit
You will see:
50x.html index.html
What it means: You are inside the container. exit leaves; the container keeps running.
Example 18: Change a file inside
Type this:
docker exec shop sh -c 'printf "Flipkart sale page\n" > /usr/share/nginx/html/index.html'
curl -s localhost:8080
You will see:
Flipkart sale page
What it means: Changes inside a container are lost when it is removed. We fix that with a volume later.
Step 7: docker cp
Why: You may need to take a log file out, or put a config in.
What: docker cp copies files between your host and a container, in either direction.
Example 19: Container to host
Type this:
docker cp shop:/etc/nginx/nginx.conf ./nginx.conf
ls -l nginx.conf
You will see:
-rw-r--r-- 1 ravi ravi 648 Oct 25 10:33 nginx.conf
What it means: The file is now on your host.
Example 20: Host to container
Type this:
printf 'Diwali offer\n' > offer.html
docker cp offer.html shop:/usr/share/nginx/html/
curl -s localhost:8080/offer.html
You will see:
Diwali offer
What it means: Copied in and served at once.
Example 21: A whole folder
Type this:
docker cp shop:/var/log/nginx ./nginx-logs
ls nginx-logs
You will see:
access.log error.log
What it means: Folders copy too.
Step 8: docker stop, start, restart and rm
Why: You stop containers to save memory, and remove them when finished.
What: stop asks the main process to end politely, start starts a stopped container again, restart does both, and rm deletes a stopped container.
Example 22: Stop and check
Type this:
docker stop shop
docker ps -a --filter name=shop --format '{{.Status}}'
You will see:
shop
Exited (0) 2 seconds ago
What it means: Exit code 0 means a clean stop.
Example 23: Start again
Type this:
docker start shop
curl -s localhost:8080/offer.html
You will see:
shop
Diwali offer
What it means: Same container, same files. Stop does not delete anything.
Example 24: Remove
Type this:
docker rm oops
docker rm -f shop
You will see:
oops
shop
What it means: rm needs the container stopped. -f stops and removes in one go.
4. Build and share
Step 9: docker build
Why: Real apps need their own image, not plain nginx.
What: docker build reads a Dockerfile and makes an image. -t gives it a name and tag. The dot means use this folder.
Example 25: Write a tiny Dockerfile
Type this:
mkdir sale && cd sale
printf 'Big Billion Days\n' > index.html
printf 'FROM nginx:1.27\nCOPY index.html /usr/share/nginx/html/\n' > Dockerfile
cat Dockerfile
You will see:
FROM nginx:1.27
COPY index.html /usr/share/nginx/html/
What it means: Two lines: start from nginx, add our page.
Example 26: Build
Type this:
docker build -t sale-page:1.0 .
You will see:
[+] Building 1.2s (7/7) FINISHED
=> [1/2] FROM docker.io/library/nginx:1.27
=> [2/2] COPY index.html /usr/share/nginx/html/
=> naming to docker.io/library/sale-page:1.0
What it means: Our image is ready.
Example 27: Run our image
Type this:
docker run -d --name sale -p 8081:80 sale-page:1.0
curl -s localhost:8081
You will see:
Big Billion Days
What it means: Our own content, packed inside the image.
Step 10: docker tag and docker push
Why: Other servers can only pull images that live in a registry.
What: docker tag gives an image an extra name that includes your registry account. docker push uploads it. You must log in first (lesson 47.11).
Example 28: Tag
Type this:
docker tag sale-page:1.0 ravibagale/sale-page:1.0
docker images sale-page --format '{{.ID}}'; docker images ravibagale/sale-page --format '{{.ID}}'
You will see:
5e7a2c1b9d0f
5e7a2c1b9d0f
What it means: Same ID: a tag is only a second name, not a copy.
Example 29: Push
Type this:
docker push ravibagale/sale-page:1.0
You will see:
The push refers to repository [docker.io/ravibagale/sale-page]
1.0: digest: sha256:4b8e... size: 1987
What it means: Only our new layer uploads; nginx layers are already on Docker Hub.
Example 30: Pull it elsewhere
Type this:
docker rmi ravibagale/sale-page:1.0 sale-page:1.0 -f
docker run --rm -d -p 8082:80 ravibagale/sale-page:1.0
You will see:
Unable to find image 'ravibagale/sale-page:1.0' locally
1.0: Pulling from ravibagale/sale-page
Status: Downloaded newer image
What it means: Any server can now run it.
5. Connect and store
Step 11: docker network
Why: An app container must find its database by name.
What: A user-defined bridge network lets containers reach each other by container name. docker network create makes one.
Example 31: Create a network
Type this:
docker network create shopnet
You will see:
b1c2d3e4f5a6...
What it means: An empty private network.
Example 32: Two containers on it
Type this:
docker run -d --name db --network shopnet -e POSTGRES_PASSWORD=devonly postgres:16
docker run --rm --network shopnet alpine ping -c1 db
You will see:
PING db (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.08 ms
What it means: The name db works like a phone book entry. devonly is a practice password, never a real one.
Example 33: See who is connected
Type this:
docker network inspect shopnet --format '{{range .Containers}}{{.Name}} {{end}}'
You will see:
db
What it means: Only db is still running on the network.
Step 12: docker volume
Why: Data inside a container dies with the container. Orders and users must survive.
What: A volume is storage that Docker manages outside the container. -v name:/path attaches it.
Example 34: Create and use
Type this:
docker volume create pgdata
docker rm -f db
docker run -d --name db --network shopnet -e POSTGRES_PASSWORD=devonly -v pgdata:/var/lib/postgresql/data postgres:16
You will see:
pgdata
db
9a8b7c...
What it means: The database files now live in the volume.
Example 35: Prove it survives
Type this:
docker exec db psql -U postgres -c 'CREATE TABLE orders(id int);'
docker rm -f db
docker run -d --name db -e POSTGRES_PASSWORD=devonly -v pgdata:/var/lib/postgresql/data postgres:16
sleep 3; docker exec db psql -U postgres -c '\dt'
You will see:
CREATE TABLE
db
...
List of relations
public | orders | table | postgres
What it means: New container, same table.
Example 36: Where it lives
Type this:
docker volume inspect pgdata --format '{{.Mountpoint}}'
You will see:
/var/lib/docker/volumes/pgdata/_data
What it means: On the host disk, managed by Docker.
Step 13: Clean up everything from this lesson
Why: A good engineer leaves the server as clean as she found it.
What: Remove containers, then the network, then images. Remove the volume only when you are sure.
Example 37: Containers
Type this:
docker rm -f sale db
You will see:
sale
db
What it means: Both gone.
Example 38: Network and images
Type this:
docker network rm shopnet
docker rmi sale-page:1.0 ravibagale/sale-page:1.0 nginx:1.27
You will see:
shopnet
Untagged: sale-page:1.0
...
What it means: Images are removed only when no container uses them.
Example 39: Volume, after thinking
Type this:
docker volume rm pgdata
docker system df
You will see:
pgdata
TYPE TOTAL ACTIVE SIZE
Images 2 0 250MB
What it means: Practice data only, so it is safe to remove.
Ravindra Bagale's Tip
Use this order in daily work: pull, run, ps, logs, exec, stop, rm. Log in before build and push. And always clean up at the end.
रोजच्या कामात हाच क्रम वापरा: pull, run, ps, logs, exec, stop, rm. Build आणि push साठी आधी login. आणि शेवटी नेहमी साफसफाई.
रोज़ के काम में यही क्रम अपनाइए: pull, run, ps, logs, exec, stop, rm. Build और push से पहले login. और आख़िर में हमेशा सफ़ाई.
Check yourself
Q1. What is the difference between docker ps and docker ps -a?
ps shows only running containers; -a also shows stopped ones.
Q2. Does docker stop delete the container?
No. docker rm deletes it.
Q3. Why does docker tag not use more disk?
A tag is just another name for the same image ID.
Q4. How do two containers find each other by name?
Put both on the same user-defined network.
Q5. How do you keep database data when a container is removed?
Store it in a volume with -v name:/path.
Friends, remember this order as one story: we pulled an image, ran a container, looked inside, built our own image, shared it, added a network and a volume, and cleaned up. Did you understand? Let us try it once more. If not, look at Picture 1 again.
मित्रांनो, हा क्रम एक गोष्ट म्हणून लक्षात ठेवा: image आणली, container चालवला, आत बघितलं, आपली image बनवली, share केली, network आणि volume जोडले, आणि साफ केलं. समजलं का? चला, पुन्हा एकदा करून पाहू. नसेल तर Picture 1 परत बघा.
दोस्तों, यह क्रम एक कहानी की तरह याद रखिए: image लाई, container चलाया, अंदर देखा, अपनी image बनाई, share की, network और volume जोड़े, और सफ़ाई की. समझ आया? चलिए, एक बार फिर करके देखते हैं। नहीं तो Picture 1 फिर देखिए.
Lab: try at home
- Pull nginx:1.27 and read its history.
- Run it on port 8080, follow its logs, and change the page with docker exec.
- Copy nginx.conf out with docker cp.
- Build your own sale-page image, tag it and push it to Docker Hub.
- Run Postgres with a volume, remove the container, and prove the table survives.
Recap
- pull, images and inspect handle images.
- run, ps, logs, exec and cp work with containers.
- stop keeps the container; rm deletes it.
- build, tag and push share your own image.
- Networks connect containers by name; volumes keep data.
Revision card
Interview card