Docker is an open platform for developing, shipping and running applications. It packages an application and the files it needs into a container, an isolated process, so the app runs the same way on a laptop, a test server or a cloud machine.
The fastest way to see what that means is to run one. With Docker installed, this command starts a small web server from an image Docker publishes for its own tutorial:
docker run -d -p 8080:80 docker/welcome-to-docker
Open http://localhost:8080 in a browser and the page is served by a container. The -d flag runs it in the background, and -p 8080:80 makes port 80 inside the container reachable on port 8080 of your machine. Docker pulls the image first if it is not already on your computer. Nothing else had to be installed on your machine for the web server to work.
What problem does Docker solve?
An application usually needs more than its own code: a language runtime, libraries, system tools, configuration. If those differ between your machine and the server, the app can work in one place and fail in the other. Docker bundles the app with the files it needs, so the environment travels with it.
Docker's overview lists three uses:
- Consistent delivery. Developers work in standardized environments, which fits continuous integration and delivery workflows.
- Portable deployment and scaling. Containers run on a laptop, in a data center or in the cloud, so workloads can be moved and scaled up or down.
- More workloads on the same hardware. Containers are lighter than virtual machines that run on a hypervisor.
What is a Docker container?
Docker's documentation puts it simply: "containers are isolated processes for each of your app's components." It lists four properties:
- Self-contained. A container has what it needs and does not rely on dependencies installed on the host.
- Isolated. A container has minimal influence on the host and on other containers.
- Independent. Each container is managed on its own. Deleting one does not affect the others.
- Portable. It runs the same way on a developer machine, in a data center or in the cloud.
A container is not a small virtual machine. It shares the host's kernel instead of booting its own operating system. Docker vs virtual machine covers that difference, including why Docker Desktop on Windows and macOS runs a Linux VM behind the scenes.
What is a Docker image?
An image is "a read-only template with instructions for creating a Docker container." Docker's image guide gives two principles:
- Images are immutable. Once an image is created, it cannot be modified. You build a new image to change something.
- Images are composed of layers. In Docker's overview, each instruction in a Dockerfile creates a layer.
A container is "a runnable instance of an image." One image can start many containers. To write your own image, see how to write a Dockerfile.
How Docker works: client, daemon and registry
Docker is a few cooperating parts rather than one program:
| Part | What it is | Example |
|---|---|---|
| Client | The primary way many users interact with Docker. It sends your commands to the daemon. | docker run |
| Daemon | The background service that listens for Docker API requests and manages Docker objects. | dockerd |
| Registry | A place that stores images. Docker Hub is the public registry Docker looks in by default; you can also run a private one. | Docker Hub |
| Docker Desktop | An installable application for Mac, Windows or Linux that bundles the daemon, the client, Docker Compose and other tools. | The Docker Desktop app |
When you type docker run, the client asks the daemon to do the work. The daemon checks whether the image is in its local cache and pulls it from a registry if it is not. Docker's reference describes this as running a command in a new container, "pulling the image if needed and starting the container." The default pull policy is missing: pull only when the image is not already cached. --pull always pulls before every run, and --pull never fails if the image is not cached.
Your first Docker commands
These use only flags documented for docker run:
-d(--detach) starts the container as a background process that does not occupy your terminal.-p(--publish) publishes a container's port to the host, writtenhost-port:container-port.--nameassigns a name to the container, so you can refer to it in later commands.--rmautomatically removes the container, and its anonymous volumes, when it exits.
A named version of the earlier example, which cleans itself up when stopped:
docker run -d --rm --name welcome -p 8080:80 docker/welcome-to-docker
docker ps
docker stop welcome
docker ps lists running containers, and docker stop welcome stops the one you named. Because of --rm, stopping it also deletes it. If port 8080 is already taken on your machine, the run fails. Pick another host port, for example -p 8081:80, or see Docker port mapping for how the mapping works. More commands are in the Docker commands cheat sheet.
What do people use Docker for?
- Running software without installing it. Start a database or a web server from an image, then remove it when you are done.
- Matching environments. The same image runs in development, in CI and in production.
- Running several services together. A web app, a database and a cache can each run in their own container. Docker Compose describes them in one file.
Things that surprise beginners
- Data inside a container is not permanent by default. Removing the container removes changes made inside it. Keep data you need in a volume or a bind mount.
- Docker Desktop is not Docker itself. It is a convenient package around the daemon, client and extra tools. On Linux you can also install Docker Engine without it.
- The image is not the running thing. Pulling an image does not start anything.
docker runcreates and starts a container from it.
Frequently asked questions
- Is Docker free?
- Docker's documentation describes Docker as an open platform, and the command-line client and daemon can be installed without paying. Docker Desktop, the packaged application for Windows, macOS and Linux, and Docker Hub have their own licensing and plan terms that change over time, so check Docker's current pricing page before using them at work.
- What is the difference between a Docker image and a container?
- An image is a read-only template with instructions for creating a container. A container is a runnable instance of an image. You can start many containers from one image, and deleting a container does not delete the image it came from.
- Do I need to know Linux to use Docker?
- Not to start. The docker commands are the same on Windows, macOS and Linux. Most images are Linux-based, though, so you will meet Linux file paths and shell commands inside containers sooner or later.
- Is Docker the same as Kubernetes?
- No. Docker packages and runs containers. Kubernetes is a separate system for scheduling and managing containers across many machines. Docker Desktop can bundle a Kubernetes option, but you do not need Kubernetes to use Docker.