Dockerfile Basics

View saved

What a Dockerfile is

A Dockerfile is a text file of instructions. Docker reads it top to bottom and produces an image layer by layer.

Common instructions

  • FROM — start from a base image
  • WORKDIR — set the working directory
  • COPY — add files from your project
  • RUN — execute build-time commands
  • EXPOSE — document a port (does not publish it)
  • CMD — default command when the container starts

Write a tiny web app image

Create these files in an empty folder, then move on to building in the next lesson.

# Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
EXPOSE 8000
CMD ["python", "app.py"]

Add a minimal app

A short Python HTTP server is enough to practice the build-run loop.

# app.py
from http.server import BaseHTTPRequestHandler, HTTPServer

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        body = b"Hello from my image\n"
        self.send_response(200)
        self.send_header("Content-Type", "text/plain")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

HTTPServer(("0.0.0.0", 8000), Handler).serve_forever()

Comments

One comment per signed-in account. Comments are saved with this page’s URL.