If you have spent any time in software development or DevOps, you have almost certainly heard the phrase: "But it works on my machine!" This is the problem Docker was built to solve. Docker packages your application, its dependencies, and its environment into a single portable unit called a container — so it runs exactly the same way everywhere, whether that's your laptop, a colleague's machine, or a server in the cloud.
In this guide, we'll cover everything you need to know to get started with Docker: what containers are, how Docker works, how to write a Dockerfile, and how to run multi-service applications with Docker Compose.
What is a Container?
A container is a lightweight, isolated environment that runs an application and everything it needs to run — libraries, configuration files, and dependencies — bundled together as a single package. Containers share the host operating system's kernel but are isolated from each other at the process and filesystem level.
This is different from a virtual machine (VM). A VM includes its own full operating system, which makes it heavy (several gigabytes) and slow to start (minutes). A Docker container, by contrast, starts in seconds and typically weighs only megabytes because it shares the host OS kernel.
Container vs Virtual Machine
A virtual machine virtualizes hardware and runs a full OS. A container virtualizes only the application layer and shares the host kernel. This makes containers faster to start, smaller in size, and more efficient to run at scale.
Installing Docker
Docker Desktop is the easiest way to install Docker on Windows and Mac. On Linux (Ubuntu), run:
Key Docker Concepts
Docker Image
A Docker image is a read-only template that contains the instructions for creating a container. Think of it like a blueprint or a recipe. Images are built from a Dockerfile and can be stored in a registry like Docker Hub or Amazon ECR. When you run an image, Docker creates a container from it.
Docker Container
A container is a running instance of an image. You can start, stop, pause, and delete containers. Containers are isolated from each other and from the host system, but they can be connected to networks and storage volumes.
Docker Hub
Docker Hub is Docker's public registry — a library of pre-built images you can use as starting points. Instead of building from scratch, you can base your image on an official Ubuntu, Node.js, Python, or Nginx image and just add your application on top.
Your First Docker Commands
Writing a Dockerfile
A Dockerfile is a plain text file with instructions that tell Docker how to build your image. Here is a Dockerfile for a simple Node.js application:
To build this into an image and run it:
Docker Compose — Running Multiple Containers
Most real applications need multiple services — a web server, an API, a database. Docker Compose lets you define and run all of them together using a single YAML file. Here is an example for a Node.js API with a PostgreSQL database:
Start both services with a single command:
Pushing Your Image to Docker Hub
Once you've built your image, you can push it to Docker Hub so it can be pulled and run anywhere — including on your AWS EC2 server or in a Kubernetes cluster.
Docker Best Practices
- Use official base images — start from trusted, well-maintained images like node:alpine, python:slim, or nginx:alpine.
- Use multi-stage builds — separate your build environment from your production image to keep final images small.
- One process per container — each container should run a single service. Use Docker Compose to connect multiple containers.
- Never hardcode secrets in Dockerfiles — use environment variables or Docker Secrets to pass sensitive values at runtime.
- Use .dockerignore — like .gitignore, this tells Docker which files to exclude from the build context (node_modules, .git, etc.).
What to Learn Next
Once you're comfortable with Docker basics, the natural next step is Kubernetes — the system for managing containers at scale across multiple servers. Kubernetes (often called K8s) builds on the same container concepts you've learned here and adds scheduling, health monitoring, auto-scaling, and service discovery.
Learn Docker & DevOps with Real Projects
Our DevOps Engineering Program covers Docker, Kubernetes, AWS, CI/CD, and more — with live sessions and job placement included.
View DevOps Course →