Docker

How to Use Docker: A Step-by-Step Tutorial

How to Use Docker: A Step-by-Step Tutorial

Your code runs perfectly on your laptop. Then it crashes on the server.

This “works on my machine” problem has plagued software development for decades. Learning how to use Docker fixes it by packaging applications into portable containers that run identically anywhere.

No more dependency conflicts. No more environment mismatches between team members.

This guide walks you through the complete container lifecycle: installation, image management, container operations, Dockerfile creation, and multi-container orchestration.

By the end, you’ll run containers, build custom images, and manage persistent data with confidence.

What is Docker and When Do You Need It?

Docker is an open-source platform that packages applications into lightweight, portable units called containers.

These containers bundle your code, runtime, libraries, and system tools into a single executable package.

Unlike traditional virtual machines, Docker shares the host Linux kernel, making containers faster to start and more resource-efficient.

You need Docker when:

  • Your app works on your machine but breaks in production
  • Team members run different OS versions or dependencies
  • You want consistent deployment across development, staging, and production environments
  • You’re building microservices that need isolated runtime environments

This guide covers 10 steps requiring 30-45 minutes and basic command line familiarity.

Why has Docker revolutionized deployment?

Explore Docker statistics: containerization adoption, DevOps transformation, enterprise usage, and how containers changed software delivery.

Discover Docker Insights →

Prerequisites

Before running docker commands, confirm your system meets these requirements:

  • Docker Desktop 4.25+ for Windows/macOS, or Docker Engine 24.0+ for Linux
  • Windows 10/11 Pro with WSL 2 enabled, macOS 12+, or Ubuntu 20.04+/Debian 11+
  • Minimum 4GB RAM (8GB recommended), 20GB free disk space
  • Admin or sudo access for installation
  • Terminal or command prompt experience

Windows Home users need WSL 2 backend. Linux users must add their account to the docker group for non-root access.

Step One: How Do You Install Docker on Your System?

maxresdefault How to Use Docker: A Step-by-Step Tutorial

Docker installation varies by operating system but follows the same pattern: download the installer, run it, and configure system permissions.

The Docker daemon runs as a background service, managing all container operations on your machine.

Action

Windows/macOS: Download Docker Desktop from docker.com/products/docker-desktop.

Run the installer (.exe or .dmg) and follow the setup wizard. Restart your system when prompted.

Linux (Ubuntu/Debian):

sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin `

After installation, the Docker whale icon appears in your system tray (Windows/macOS) or the docker service starts automatically (Linux).

For detailed platform-specific instructions, see this installation guide.

Purpose

Installs Docker Engine, CLI tools, and Docker Compose for container orchestration.

Step Two: How Do You Verify Docker Installation?

Verification confirms the Docker daemon is running and your user account has proper permissions to execute docker commands.

Action

Open your terminal and run:

` docker --version `

Expected output: Docker version 24.0.x, build xxxxxx

Next, test the full container lifecycle:

` docker run hello-world `

This command pulls the hello-world image from Docker Hub, creates a container, runs it, and outputs a success message.

You can also check if Docker is running with docker info for detailed system information.

Purpose

Confirms Docker Engine, CLI, and network connectivity to Docker Hub registry all function correctly before you start real work.

Step Three: How Do You Pull Your First Docker Image?

Images are read-only templates containing your application and its dependencies. Pulling downloads these templates from a container registry to your local machine.

Action

Pull the official NGINX image:

` docker pull nginx:latest `

The command downloads image layers sequentially. Each layer represents a filesystem change, and Docker caches them for reuse.

Verify the downloaded image:

` docker images `

Output shows REPOSITORY (nginx), TAG (latest), IMAGE ID, CREATED date, and SIZE.

Purpose

Downloading images locally enables offline container creation and faster startup times since layers are already cached on your system.

Step Four: How Do You Run a Docker Container?

Running a container creates an isolated instance from an image, complete with its own filesystem, network, and process space.

Action

Start an NGINX web server:

` docker run -d -p 8080:80 --name my-nginx nginx:latest `

Flag breakdown:

  • -d: Detached mode (runs in background)
  • -p 8080:80: Maps host port 8080 to container port 80
  • –name my-nginx: Assigns a readable container name

Open http://localhost:8080 in your browser. You should see the NGINX welcome page.

For more options, see how to create a Docker container with custom configurations.

Purpose

Port mapping exposes containerized services to your host machine and network.

Step Five: How Do You List Running Containers?

Monitoring active containers helps you track resource usage and troubleshoot application issues.

Action

View running containers:

` docker ps `

Output columns: CONTAINER ID, IMAGE, COMMAND, CREATED, STATUS, PORTS, NAMES.

Include stopped containers:

` docker ps -a `

Filter by status: docker ps -f “status=exited”

Purpose

Identifies which containers consume resources; reveals port conflicts and naming collisions before they cause problems.

Step Six: How Do You Stop and Remove Containers?

Proper container cleanup prevents disk bloat and frees system resources for new deployments.

Action

Stop a running container:

` docker stop my-nginx `

Remove a stopped container:

` docker rm my-nginx `

Force-remove a running container: docker rm -f my-nginx

Clean up all stopped containers: docker container prune

Need to bring it back? Learn how to restart a Docker container.

Purpose

Stopped containers still occupy disk space; removing them reclaims storage and keeps your environment clean.

Step Seven: How Do You Create a Dockerfile?

A Dockerfile contains instructions for building custom images tailored to your application’s requirements.

Action

Create a file named Dockerfile (no extension) in your project root:

` FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["npm", "start"] `

Instruction breakdown:

  • FROM: Base image (Alpine Linux variant for smaller size)
  • WORKDIR: Sets working directory inside container
  • COPY: Transfers files from host to container
  • RUN: Executes commands during build
  • EXPOSE: Documents which port the app uses
  • CMD: Default command when container starts

Store your Dockerfile alongside your codebase in source control.

Purpose

Dockerfiles enable reproducible builds across any machine running Docker, eliminating environment inconsistencies.

Step Eight: How Do You Build a Custom Docker Image?

Building converts your Dockerfile instructions into a reusable image with layered filesystem caching.

Action

Build from current directory:

` docker build -t my-app:1.0 . `

The -t flag assigns a name and tag. The . specifies build context (current directory).

Watch the output as Docker executes each instruction and creates image layers.

Verify your new image: docker images | grep my-app

Understanding where Docker images are stored helps with disk management.

Purpose

Custom images package your application exactly as it should run, making continuous deployment predictable.

Step Nine: How Do You Manage Docker Volumes?

Volumes persist data beyond container lifecycle, surviving stops, restarts, and removal.

Action

Create a named volume:

` docker volume create my-data `

Mount it to a container:

` docker run -d -v my-data:/var/lib/mysql --name mysql-db mysql:8.0 `

List all volumes: docker volume ls

Inspect volume details: docker volume inspect my-data

Check where Docker volumes are stored on your filesystem for backup purposes.

Purpose

Database containers, user uploads, and configuration files need persistent storage that outlasts individual containers.

Step Ten: How Do You Use Docker Compose for Multi-Container Applications?

Compose defines and runs multi-container applications using a single YAML file, perfect for local development of backend services/).

Action

Create docker-compose.yml:

` version: '3.8' services: web: build: . ports:

  • “3000:3000”

dependson:

  • db

db: image: postgres:15 environment: POSTGRESPASSWORD: secret volumes:

  • db-data:/var/lib/postgresql/data

volumes: db-data: `

Start all services:

` docker compose up -d `

Stop and remove: docker compose down

Need Compose separately? See how to install Docker Compose on older systems.

Purpose

Compose orchestrates service dependencies, networks, and volumes in a single command, speeding up rapid development workflows.

Verification

Confirm your Docker setup works correctly:

  • Run docker info and verify no errors appear
  • Check container health: docker ps –format “table {{.Names}}t{{.Status}}”
  • Test networking: docker exec my-nginx curl -s localhost
  • Verify volume mounts: docker inspect my-container | grep Mounts -A 10

Access container shell for debugging with SSH or exec commands.

When finished, exit the container with exit or Ctrl+D.

Troubleshooting

Issue: Docker Daemon Not Running

Solution: Learn how to start Docker daemon manually.

Windows/macOS: Launch Docker Desktop from applications. Linux: sudo systemctl start docker

Issue: Permission Denied Errors

Solution: Add your user to the docker group:

` sudo usermod -aG docker $USER `

Log out and back in for changes to take effect.

Issue: Port Already in Use

Solution: Find the conflicting process:

` lsof -i :8080 `

Either stop that process or map to a different host port: -p 8081:80

Issue: Image Pull Fails

Solution: Check internet connectivity and Docker Hub status. For private registries, verify credentials with docker login.

Behind a proxy? Configure Docker’s daemon.json with your proxy settings.

Issue: Container Exits Immediately

Solution: Check logs: docker logs container-name

Common causes: missing environment variables, crashed application, or incorrect CMD instruction.

Related Processes

Once comfortable with Docker basics, explore these connected topics:

Docker integrates with GitHub Actions, Jenkins, and GitLab CI for automated testing and deployment.

FAQ on How To Use Docker

What is the difference between a Docker image and a container?

An image is a read-only template containing your application code, runtime, and dependencies. A container is a running instance of that image with its own writable layer, isolated processes, and network stack.

How much RAM does Docker need to run properly?

Docker itself requires minimal resources. Allocate at least 2GB RAM for Docker Desktop, plus whatever your containers need. Most development setups run smoothly with 8GB total system RAM.

Can Docker containers communicate with each other?

Yes. Containers on the same Docker network communicate using container names as hostnames. Create custom networks with docker network create to isolate groups of related services from others.

What happens to data when a container stops?

Data inside the container’s writable layer persists until you remove the container. For permanent storage, mount volumes or bind mounts that exist independently of container lifecycle.

Is Docker the same as a virtual machine?

No. Docker containers share the host kernel and start in seconds. A virtual machine runs a complete guest OS, consuming more resources and taking minutes to boot.

How do I update an application running in Docker?

Build a new image with your updated code, stop the old container, and start a new one from the updated image. For zero-downtime updates, use blue-green deployment or rolling updates.

Can I run Docker on Windows without WSL?

Docker Desktop supports Windows containers natively without WSL. However, Linux containers (the most common type) require WSL 2 backend or Hyper-V on Windows 10/11 Pro editions.

How do I troubleshoot a container that keeps crashing?

Check logs with docker logs container-name. Inspect the exit code using docker ps -a. Common causes include missing environment variables, port conflicts, or application errors during startup.

What is the best way to manage environment variables?

Use –env-file flag to load variables from a file, keeping secrets out of your Dockerfile. For production, integrate with secret management tools or your orchestration platform's native secrets.

How do I reduce Docker image size?

Use Alpine-based images, implement multi-stage builds, and combine RUN commands to reduce layers. Remove package manager caches and unnecessary files in the same layer they were created.

Conclusion

You now know how to use Docker from installation through multi-container orchestration. The commands and concepts covered here form the foundation for modern application deployment.

Docker transforms how teams ship software. Containers eliminate dependency conflicts, speed up onboarding, and enable software scalability across cloud providers.

Start small. Run a single container, then build your first custom image.

As your projects grow, explore deployment pipelines and infrastructure as code for automated workflows.

The portability Docker provides makes collaboration between development and operations seamless.

Pick one application today. Containerize it. That first successful docker run` changes everything.

50218a090dd169a5399b03ee399b27df17d94bb940d98ae3f8daff6c978743c5?s=250&d=mm&r=g How to Use Docker: A Step-by-Step Tutorial

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.