Chapter 7: Docker Commands, Networks, Volumes and Compose
All Docker commands in one place
Every docker command step by step on one running example (build, run, exec, logs, volumes, networks, Compose, cleanup) with a cheat sheet: All Docker Commands Step by Step with Examples
Chala mitrano, aaj aapan rozche Docker commands, network types, volume types aani Compose. Chapter 6 madhe image, build, history aani Hub zhala. Ithe ps, logs, exec, ani API + database ek private lane var. Ghabru naka: pratyek command nantar "what you see" vacha.
What you will learn in this chapter
- Everyday commands that are not the build, run, history or push lesson
- Bridge, host, none, overlay, macvlan, and two containers talking by name
- Named, bind, tmpfs and anonymous volumes
- One Compose file and docker compose up
- Env files, and why down -v deletes lab data
Concepts in this chapter
- 7.1Everyday commands and what you see
- 7.2Network types — when to use which
- 7.3Two containers talking by name
- 7.4Volume types — data, code and secrets
- 7.5Compose file — one stack, one up
- 7.6Env files — never commit real secrets
- 7.7Compose up, down, logs, ps
- 7.8Small Compose habits
- 7.9Troubleshooting
- 7.10Lab — commands, a name, a volume, one stack
The chapter recap is at the end of the last concept page.
Practice Tasks
Suppose we are on the DevOps team at BBC, the news media company, working from London. Oliver Hughes leads the team, and we look after the live news ticker. Costs are counted in GBP. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. BBC is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.
0 / 12
Easy Read container logs
Why: Logs are the first place to look when a container misbehaves.
What to do: Show the last 100 log lines of the container live-news.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Easy Compose: build from source
Why: Developers build the image locally with the same file the team uses.
What to do: Write compose.yaml with one service api that builds from the current folder (.) and publishes 4000:4000.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Easy Start the compose stack
Why: One command starts every service of the app together.
What to do: Start all services from compose.yaml in the background.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Compose: app + database
Why: Compose describes the whole stack in one file anyone can start.
What to do: Write compose.yaml with two services. web: image bbc/live-news:1.4.6, port 4000:4000, depends_on db. db: image postgres:16, environment POSTGRES_PASSWORD: changeme, volume pgdata at /var/lib/postgresql/data. Declare the named volume pgdata at the top level.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Medium Logs of one compose service
Why: In a stack you want the logs of one service only.
What to do: Show the last 50 log lines of the compose service api.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Keep data in a named volume
Why: Data inside a container is lost when it is removed. A volume keeps it.
What to do: Run mysql:8.0 detached, named bbc-db, with MYSQL_ROOT_PASSWORD=changeme and the named volume bbc-dbdata mounted at /var/lib/mysql.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Follow logs live
Why: Watching logs live while you test shows errors the moment they happen.
What to do: Follow the logs of live-news live, starting from the last 20 lines.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Compose: env file and restart policy
Why: Secrets live in an env file that is not committed, and services come back after a reboot.
What to do: Write compose.yaml: service worker with image bbc/live-news-worker:1.4.6, env_file .env, and restart: unless-stopped.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Medium Rebuild and start
Why: After a code change the image must be rebuilt, or the old one runs.
What to do: Rebuild the images and start the stack in the background in one command.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Stop and remove the stack with volumes
Why: For a clean test run you want fresh databases.
What to do: Stop and remove the compose stack and also delete its named volumes.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Hard Compose: private backend network
Why: Only the API should reach the database; the proxy stays on the front network.
What to do: Write compose.yaml: proxy (nginx:1.27-alpine) on network front; api (image bbc/live-news:1.4.6) on networks front and back; db (redis:7) on network back. Declare networks front and back at the top level.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Hard Compose: wait for a healthy database
Why: The app crashes if it starts before the database is ready.
What to do: Write compose.yaml: db (mysql:8.0) with a healthcheck whose test is ["CMD", "mysqladmin", "ping", "-h", "localhost"] and interval 10s; app (image bbc/live-news:1.4.6) that depends_on db with condition service_healthy.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Ravi Sir's Cards: 23 revision, interview and comparison cards for this chapter · मराठी cards