Introduction to Docker
What Docker is, why containers matter, and how to run your first container
Overview
Docker is a platform for packaging applications and their dependencies into lightweight, portable units called containers. A container runs the same way on your laptop, a teammate's machine, and production, which eliminates the classic "it works on my machine" problem. Unlike virtual machines, containers share the host kernel, so they start in seconds and use far fewer resources.
Syntax / Usage
After installing Docker, you interact with it through the docker command-line tool. The most common first step is pulling an image and running it as a container.
# Check your installation
docker --version
# Pull an image from Docker Hub
docker pull nginx
# Run a container in the background and map a port
docker run -d -p 8080:80 --name web nginx
# List running containers
docker ps
# Stop and remove the container
docker stop web
docker rm web
Examples
Run a temporary container and get an interactive shell inside it:
docker run -it --rm ubuntu bash
Run a container and immediately clean it up when it exits, printing a message:
docker run --rm alpine echo "Hello from Docker"
Common Mistakes
- Forgetting
-dand then wondering why the terminal is "stuck" attached to the container - Mapping ports in the wrong order (
-p host:container, not the reverse) - Leaving stopped containers around and running out of disk space
- Assuming data inside a container persists after
docker rm(it does not without volumes) - Confusing images (the template) with containers (a running instance)
See Also
docker-images-and-containers docker-dockerfile docker-compose