Introduction#
Docker is an open-source platform that allows you to package applications and their dependencies into portable containers.
Containers are lightweight, consistent, and reproducible environments, making Docker a core technology in DevOps, CI/CD pipelines, and Infrastructure as Code (IaC) practices.
With Docker, you can:
- Run applications in isolated environments.
- Ensure the same behavior across development, testing, and production.
- Easily scale services and orchestrate multiple containers.
This article will define Docker’s key vocabulary, explain prerequisites, and provide a cheat sheet for common commands and workflows.
Vocabulary#
Core Concepts#
- Container : A lightweight, standalone environment that packages an application with its dependencies.
- Image : A read-only template used to create containers.
- Dockerfile : A text file with instructions to build a Docker image.
- Registry : A repository for storing and distributing Docker images (Docker Hub, self-hosted registry).
- Container Runtime : Software that runs containers (Docker Engine).
- Volume : A persistent storage mechanism for containers.
- Network : Defines how containers communicate with each other or the outside world.
- Docker Compose : A tool to define and run multi-container applications using YAML.
Docker Objects#
- Container ID / Name : Unique identifier for running containers.
- Tag : A label for identifying image versions (
:latest
,:v1.0
). - Layer : Each instruction in a Dockerfile creates a new image layer.
- Port Mapping : Exposing container ports to the host (
-p hostPort:containerPort
). - EntryPoint : The default command executed when a container starts.
- CMD : Default arguments for the container entrypoint.
- Environment Variables : Variables passed to containers to configure runtime behavior.
Docker Commands#
- docker build : Build an image from a Dockerfile.
- docker run : Create and start a container from an image.
- docker ps : List running containers.
- docker stop / docker kill : Stop a running container.
- docker rm : Remove a stopped container.
- docker rmi : Remove an image.
- docker logs : View container logs.
- docker exec : Execute a command inside a running container.
- docker-compose up / down : Start or stop multi-container applications.
- docker system prune : Remove all unused containers, networks and images
Dockerfile Instructions#
- FROM : Base image to use.
- RUN : Execute commands to install packages or configure image.
- COPY / ADD : Copy files from host into the image.
- WORKDIR : Set working directory for subsequent instructions.
- EXPOSE : Declare which ports the container will listen on.
- ENV : Set environment variables.
- ENTRYPOINT : Define container entrypoint.
- CMD : Default arguments for ENTRYPOINT.
Prerequisites#
- Docker Engine installed on your host (Linux, macOS, or Windows).
- Docker Compose installed if using multi-container setups.
- Familiarity with command-line usage and basic Linux commands.
- Optional: A private registry if you plan to host your own images (gitea or nexus for instance).
Docker Cheat Sheet#
Command | Purpose | Example |
---|---|---|
docker build -t myapp:1.0 . | Build image from Dockerfile | Builds image tagged myapp:1.0 |
docker tag myapp:1.0 myregistry.local/myapp:1.0 | Build image from Dockerfile | Builds image tagged myapp:1.0 |
docker push myregistry.local/myapp:1.0 | Build image from Dockerfile | Builds image tagged myapp:1.0 |
docker run -d -p 8080:80 myapp:1.0 | Run container detached | Maps host port 8080 to container 80 |
docker ps | List running containers | docker ps |
docker stop mycontainer | Stop container | docker stop mycontainer |
docker rm mycontainer | Remove container | docker rm mycontainer |
docker rmi myapp:1.0 | Remove image | docker rmi myapp:1.0 |
docker logs mycontainer | View container logs | docker logs mycontainer |
docker exec -it mycontainer /bin/bash | Execute command in container | Opens shell inside container |
docker-compose up -d | Start multi-container app | Starts containers in detached mode |
docker-compose down | Stop multi-container app | Stops and removes containers, networks, volumes |