Ravindra BagaleCourses & study guides

Chapter 6: Docker Images and Containers

6.3 Dockerfile — FROM, COPY, RUN, CMD, EXPOSE

In short: A Dockerfile is a build file that builds an image.

A Dockerfile is a build file that builds an image.

# Example — badge API hello API (Python)
FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8080
CMD ["python", "app.py"]
Instruction Role
FROM Base image (starting layer)
WORKDIR Directory inside the image for following steps
COPY Copy files from build context into the image
RUN Execute a build-time command (install packages)
EXPOSE Documents the container's listen port (does not publish to the host by itself)
CMD Default command when the container starts

Node variant sketch (same ideas): FROM node:20-alpine, COPY package*.json, RUN npm ci, COPY . ., CMD ["node", "index.js"].

EXPOSE vs -p. EXPOSE 8080 is documentation and tooling hint. Host access needs docker run -p (next section). Ports are logical published ports — container counter 8080 mapped to a host counter you choose.

Ravindra Bagale's Tip

Khup students EXPOSE lihun browser ughadtat — kaam nahi. Host mapping -p lagte. EXPOSE ≠ publish. Dhyan rakho!

Steps — write a minimal Dockerfile

  1. In your badge API hello folder, ensure the app listens on 0.0.0.0 and a chosen port (for example 8080) — localhost-only bind inside the container blocks host curl through published ports.
  2. Add requirements.txt or package.json as needed.
  3. Add a Dockerfile using FROM, WORKDIR, COPY, RUN, EXPOSE, CMD.
  4. Add or extend .dockerignore (like .gitignore) to skip .env, .git, node_modules, __pycache__.