CodeBlanks Logo Icon CodeBlanks

Use cases

Learn common Docker use cases.

Use cases

Docker's most common uses:

  • Development environment: starting services like PostgreSQL and Redis with one command, without installing them.
  • CI/CD: running tests every time in a clean, identical environment.
  • Microservices: deploying each service independently in its own container.
  • Portable deployment: running the same image on different cloud providers.
docker run -d -e POSTGRES_PASSWORD=gizli -p 5432:5432 postgres:16

Throwaway tools

Docker is also ideal for using a tool once without installing it on the machine. The --rm flag deletes the container automatically when its work is done:

docker run --rm python:3.12 python -c "print(2 ** 10)"
# 1024
docker run --rm -v "$PWD":/app -w /app node:20 npm test

This way different Python or Node versions can be used for different projects without mixing.

Practice One-off Python

Write the command that runs python -c "print(2 ** 10)" in the python:3.12 image and deletes the container when done.

Starter code

terminal
# buraya yaz

Expected output

1024

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the main benefit of running tests inside a container in a CI pipeline?

  • Tests never failing
  • A clean, identical environment on every run Correct answer
  • Tests containing less code
  • Not needing an internet connection

Explanation: Leftover files from earlier runs or different versions don't affect the result.

2 Delete the container automatically when it finishes.

Question
docker run ____ python:3.12 python -c "print(1)"
Answer
docker run --rm python:3.12 python -c "print(1)"

Explanation: --rm removes the container when it exits.

3 Match the scenarios.

Term Meaning
local database docker run postgres
CI tests Clean environment
microservice A container per service
--rm One-off tool

All lessons in Beginner