
Docker enables you to build, ship, and run applications in isolated environments called containers. These share the host machine’s kernel but run as separate processes, making them lightweight and portable. This approach helps developers avoid environment specific bugs and streamlines collaboration. It allows you to experiment without altering your local setup, supports quick testing of software stacks (like web servers or databases), and is widely used in industry for microservices and cloud deployments. Start small to build confidence, as complexities like networking can arise in multi container setups.
Docker is an open source containerization platform that packages software and its dependencies into lightweight, portable containers. Unlike virtual machines, containers share the host OS kernel, making them efficient and ensuring consistent application performance across development, testing, and production environments. Containers leverage the host OS kernel for isolation, offering VM-like separation with minimal computing power, enabling applications to run consistently across environments like laptops, data centers, or clouds. Benefits include portability, efficiency, resource control, and widespread adoption by companies like Google, Facebook, and Netflix.
Key concepts for beginners include images (blueprints for containers), containers (running instances of images), Dockerfiles (scripts to build images), volumes (for data persistence), and networks (for container communication). Docker Hub serves as a public registry for sharing images. The Docker daemon manages containers in the background, while the CLI client interacts with it.
Step 1: Install Docker
Installation varies by operating system. Prerequisites include basic command-line knowledge and a text editor; optional Git and web development experience. Always verify with docker –version after installation.
On Windows (Using Docker Desktop)
System requirements for WSL 2 backend: Windows 10/11 (specific builds), WSL 2.1.5+, 4GB RAM, hardware virtualization enabled. For Hyper-V: Similar OS requirements, Hyper-V and Containers features enabled.
- Download the installer from https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe (x86_64) or Arm version.
- Verify or install WSL 2: Run wsl –install or wsl –update in PowerShell as admin.
- Run the installer, select WSL 2 (recommended), and complete the wizard.
- If needed, add your user to the docker-users group via Computer Management.
- Launch Docker Desktop and accept the Subscription Service Agreement.
- Test: Run docker run hello-world.
For command-line install: Start-Process ‘Docker Desktop Installer.exe’ -Wait install. Use flags like –backend=wsl-2 for customization.
On macOS (Using Docker Desktop)
Requires macOS (current and two previous major releases), 4GB RAM, optional Rosetta 2 for some tools.
- Download Docker.dmg from https://docs.docker.com/desktop/release-notes/.
- Quit any running Docker tools.
- Open Docker.dmg and drag Docker to Applications.
- Launch Docker.app, accept the agreement, and choose recommended or advanced settings.
- Test: Run docker run hello-world.
For command-line: Mount the dmg and run sudo /Volumes/Docker/Docker.app/Contents/MacOS/install. Flags like –accept-license automate acceptance.
On Linux (Ubuntu Example)
Requires 64-bit Ubuntu (specific versions like 24.04 LTS), compatible with iptables.
- Uninstall old versions: sudo apt remove docker.io docker-compose etc.
- Set up repository: Install packages, add GPG key with sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc, add sources.
- Update and install: sudo apt update; sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin.
- Start service: sudo systemctl start docker.
- For non-root access, add user to docker group (see post-install docs).
- Test: sudo docker run hello-world.
Alternative: Use convenience script curl -fsSL https://get.docker.com -o get-docker.sh; sudo sh get-docker.sh.
Step 2: Understand Basic Commands
Use the CLI for most tasks. Here’s a table of essential commands from the official cheat sheet:
| Category | Command | Description |
| Images | docker build -t <image_name> . | Build an image from a Dockerfile. |
| Images | docker build -t <image_name> . –no-cache | Build without cache. |
| Images | docker images | List local images. |
| Images | docker rmi <image_name> | Delete an image. |
| Images | docker image prune | Remove unused images. |
| Registry | docker login -u <username> | Login to Docker Hub. |
| Registry | docker push <username>/<image_name> | Publish to Docker Hub. |
| Registry | docker search <image_name> | Search for an image. |
| Registry | docker pull <image_name> | Pull from Docker Hub. |
| Containers | docker run –name <container_name> <image_name> | Run with custom name. |
| Containers | docker run -p <host_port>:<container_port> <image_name> | Run and publish ports. |
| Containers | docker run -d <image_name> | Run in background. |
| Containers | docker start <container_name> or docker stop <container_name> | Start/stop container. |
| Containers | docker rm <container_name> | Remove stopped container. |
| Containers | docker exec -it <container_name> sh | Open shell in running container. |
| Containers | docker logs -f <container_name> | Follow logs. |
| Containers | docker inspect <container_name> | Inspect container. |
| Containers | docker ps | List running containers. |
| Containers | docker ps –all | List all containers. |
| Stats | docker container stats | View resource usage. |
Additional commands: docker pull nginx to fetch, docker rmi nginx to remove.
Step 3: Run Your First Container
- Pull an image: docker pull busybox or docker pull hello-world.
- Run interactively: docker run -it busybox sh (use commands like ls, then exit).
- List: docker ps -a for all containers.
- Clean up: docker rm <ID> or docker container prune.
- For a web server: docker run -d -p 8080:80 nginx, access at http://localhost:8080.
Step 4: Build a Custom Image
Use a Dockerfile to define your image. Example for a Python Flask app:
FROM python:3.9
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
- Create the file in a directory with your app code.
- Build: docker build -t my-flask-app .
- Run: docker run -d -p 5000:5000 my-flask-app, access at http://localhost:5000.
- Push to Hub: docker login; docker push <username>/my-flask-app.
Step 5: Work with Volumes for Data Persistence
Containers are ephemeral; use volumes to persist data.
- Create: docker volume create my-volume.
- Run with mount: docker run -d -p 8080:80 -v my-volume:/app/data nginx.
Step 6: Set Up Networking
For multi container apps, create networks.
- Create: docker network create my-custom-network.
- Run containers on it: docker run –network my-custom-network –name app1 …
- Test: docker exec -it app1 ping app2.
Step 7: Use Docker Compose for Multi Container Apps
Docker Compose defines and runs multi container apps with YAML. Example for Flask with Redis:
- Create project dir, add app.py (Flask app using Redis), requirements.txt (flask, redis).
- Dockerfile: FROM python:3.10-alpine, WORKDIR /code, etc.
- compose.yaml:
services:
web:
build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"
- Run: docker compose up. 5. Access at http://localhost:8000. 6. Stop: docker compose down. Add watch for dev: Modify yaml with develop: watch.
Step 8: Example Project – Static Website
- Pull and run: docker run –rm -it prakhar1989/static-site.
- With ports: docker run -p 8888:80 prakhar1989/static-site.
- Stop: docker stop <name>.
Step 9: Multi Container Example – Food Trucks App
- Clone repo, pull Elasticsearch: docker pull docker.elastic.co/elasticsearch/elasticsearch:6.3.2.
- Run ES: docker run -d –name es -p 9200:9200 -e “discovery.type=single-node” <image>.
- Build Flask image with Dockerfile, run on network.
- Use Compose for easier management.
Step 10: Cleanup and Best Practices
- Prune: docker container prune, docker image prune.
- Use –rm for temporary runs.
- For dev: Mount volumes for code changes without rebuilds.
- Avoid running as root in production; use multi-stage builds for smaller images.
This guide provides a foundation; explore official docs for advanced topics like orchestration with Kubernetes.

