Chapter 6: Why Docker, Images and How a Container Is Created
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 container ka lagto, EC2 var app direct install ka nahi, aani image kasa banato. Nginx, PHP, MySQL, layers, safer image aani Docker Hub. Network aani volume types pudhchya chapter madhe. Ghabru naka: pahila docker version aani ek curl.
What you will learn in this chapter
- Why not install the app straight onto EC2
- How one image stops the developer vs operations argument
- Image vs container, once
- run, build, a Compose pointer, and commit as an emergency
- nginx, PHP and MySQL as three containers
- docker history, safer images, and Docker Hub
Concepts in this chapter
- 6.1Why the app should not be installed straight on the EC2
- 6.2One image, and what a container is not
- 6.3Image vs container
- 6.4Way 1 — docker run an image that already exists
- 6.5Way 2 — docker build from a Dockerfile, then run
- 6.6Way 3 — docker compose up (pointer)
- 6.7Way 4 — docker commit, emergency only
- 6.8nginx, PHP and MySQL — three containers, three jobs
- 6.9docker history — read the layers
- 6.10Make the image safer
- 6.11Push to Docker Hub and pull on another machine
- 6.12Lab — one image, three names
The chapter recap is at the end of the last concept page.
Practice Tasks
Suppose we are on the DevOps team at Flipkart, the e-commerce company, working from Bengaluru. Karthik Reddy leads the team, and we look after the cart checkout service. Costs are counted in INR. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Flipkart is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.
0 / 12
Easy Static site image
Why: Shipping the built site inside nginx gives one artefact to deploy.
What to do: Write a Dockerfile: FROM nginx:1.27-alpine, copy the folder dist/ into /usr/share/nginx/html/, and expose 80.
- Start with FROM and the base image.
- Set WORKDIR, then COPY files and RUN install steps, one instruction per line.
- Add ENV, EXPOSE or USER lines if the task asks.
- End with CMD (or ENTRYPOINT) and click Check answer.
Easy Dockerfile for a Node.js app
Why: A Dockerfile is the written recipe, so every build is the same.
What to do: Write a Dockerfile for the cart checkout service (Node.js): base image node:20-alpine, working folder /app, copy package*.json, run npm ci, copy the rest of the code, expose 4000, and start with node server.js.
- Start with FROM and the base image.
- Set WORKDIR, then COPY files and RUN install steps, one instruction per line.
- Add ENV, EXPOSE or USER lines if the task asks.
- End with CMD (or ENTRYPOINT) and click Check answer.
Easy Build an image
Why: Building from a Dockerfile gives the same image on every machine.
What to do: Build an image from the Dockerfile in the current folder and tag it flipkart/cart-checkout:1.6.5.
- 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 Run nginx in the background
Why: A web server must keep running after you close the terminal.
What to do: Run nginx:1.27-alpine detached, name the container flipkart-web and publish host port 8080 to container port 80.
- 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 Run the container as a non-root user
Why: If the app is hacked, a non-root user limits the damage.
What to do: Write a Dockerfile: FROM node:20-alpine, WORKDIR /app, COPY . ., RUN npm ci --omit=dev, then switch to the existing user node, and CMD ["node","index.js"].
- Start with FROM and the base image.
- Set WORKDIR, then COPY files and RUN install steps, one instruction per line.
- Add ENV, EXPOSE or USER lines if the task asks.
- End with CMD (or ENTRYPOINT) and click Check answer.
Medium Dockerfile for a Python API
Why: Installing requirements before copying code keeps the cache warm on every code change.
What to do: Write a Dockerfile for a Flask API: python:3.12-slim, WORKDIR /srv, copy requirements.txt and pip install with --no-cache-dir -r requirements.txt, copy the code, set ENV PORT=4000, expose 4000, and run gunicorn -b 0.0.0.0:4000 app:app.
- Start with FROM and the base image.
- Set WORKDIR, then COPY files and RUN install steps, one instruction per line.
- Add ENV, EXPOSE or USER lines if the task asks.
- End with CMD (or ENTRYPOINT) and click Check answer.
Medium Build from another Dockerfile
Why: Teams keep separate Dockerfiles for dev and prod.
What to do: Build using docker/Dockerfile.prod with the current folder as context, tagged flipkart/cart-checkout:prod.
- 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 Pass environment variables
Why: The same image runs in dev and prod; only the settings change.
What to do: Run flipkart/cart-checkout:1.6.5 detached, named cart-checkout, with APP_ENV=production and PORT=4000, publishing 4000:4000.
- 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 Open a throw-away shell
Why: A short-lived container is a safe place to test commands.
What to do: Start an interactive ubuntu:24.04 container with a terminal, run bash, and remove the container automatically when you exit.
- 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 Build argument for the version
Why: One Dockerfile can build any version when the version is an ARG.
What to do: Write a Dockerfile: ARG NODE_VERSION=20 before FROM, then FROM node:${NODE_VERSION}-alpine, then LABEL maintainer="karthik@flipkart.example.com".
- Start with FROM and the base image.
- Set WORKDIR, then COPY files and RUN install steps, one instruction per line.
- Add ENV, EXPOSE or USER lines if the task asks.
- End with CMD (or ENTRYPOINT) and click Check answer.
Hard Add a HEALTHCHECK
Why: Docker and orchestrators restart containers that stop answering.
What to do: Write a Dockerfile: FROM flipkart/cart-checkout:1.6.5, then a HEALTHCHECK every 30s with timeout 3s that runs curl -f http://localhost:4000/health || exit 1.
- Start with FROM and the base image.
- Set WORKDIR, then COPY files and RUN install steps, one instruction per line.
- Add ENV, EXPOSE or USER lines if the task asks.
- End with CMD (or ENTRYPOINT) and click Check answer.
Hard Multi-stage Go build
Why: The final image carries only the binary, so it is small and has fewer CVEs.
What to do: Write a two-stage Dockerfile. Stage "build": FROM golang:1.22 AS build, WORKDIR /src, COPY . ., RUN CGO_ENABLED=0 go build -o /out/cart-checkout . Stage 2: FROM gcr.io/distroless/static, COPY --from=build /out/cart-checkout /cart-checkout, USER nonroot, ENTRYPOINT ["/cart-checkout"].
- Start with FROM and the base image.
- Set WORKDIR, then COPY files and RUN install steps, one instruction per line.
- Add ENV, EXPOSE or USER lines if the task asks.
- End with CMD (or ENTRYPOINT) and click Check answer.
Ravi Sir's Cards: 27 revision, interview and comparison cards for this chapter · मराठी cards