Development
docker
Manage containers, images, networks, and volumes.
Additional Notes
docker manages lightweight, portable containers that package applications with their dependencies. It provides commands for building images, running containers, managing storage, configuring networking, and orchestrating multi-container workflows. Docker uses a client-server architecture where the docker CLI communicates with the dockerd daemon.
Containers are created from images stored in registries such as Docker Hub. Images are built using Dockerfile instructions in a layered format, allowing efficient reuse and distribution. Docker is widely used for development environments, CI/CD pipelines, microservices, and production deployment.
Syntax
docker [command] [options] [arguments]
Parameters
command: A subcommand such asrun,build,pull,ps,stop,rm,exec, orlogs.
Common Global Options
-D,--debug: Enable debug mode.-H,--host: Daemon socket to connect to (e.g.,unix:///var/run/docker.sockortcp://...).-v,--version: Print version information.
Common Commands
docker run [options] IMAGE [command]: Create and start a container from an image.docker ps [options]: List running containers. Use-ato show all containers.docker images: List available images.docker pull IMAGE: Download an image from a registry.docker build -t NAME .: Build an image from aDockerfilein the current directory.docker stop CONTAINER: Gracefully stop a running container.docker rm CONTAINER: Remove a stopped container.docker rmi IMAGE: Remove an image.docker exec -it CONTAINER command: Run a command in a running container.docker logs CONTAINER: Fetch the logs of a container.docker compose up: Start services defined incompose.yaml.
Container Run Options
-d,--detach: Run the container in the background.-it: Run interactively with a terminal (combine-iand-t).-p HOST:CONTAINER: Publish a container port to the host.-v HOST:CONTAINER: Mount a volume or host directory.--name NAME: Assign a name to the container.--restart POLICY: Set the restart policy (no,always,on-failure,unless-stopped).-e KEY=VALUE: Set environment variables.--rm: Automatically remove the container when it exits.--network NETWORK: Connect the container to a network.
Examples
docker run -it ubuntu bash
Start an interactive Ubuntu container with a bash shell.
docker run -d -p 80:80 --name web nginx
Start Nginx as a background container, mapping host port 80 to container port 80.
docker build -t myapp .
Build an image named myapp from a Dockerfile in the current directory.
docker ps -a
List all containers, including stopped ones.
docker exec -it myapp sh
Open a shell inside a running container named myapp.
docker logs -f myapp
Follow the log output of a container in real time.
docker compose up -d
Start all services defined in compose.yaml in the background.
Practical Notes
- Running
dockerrequires the daemon (dockerd) to be running. Usesystemctl start dockeror enable it withsystemctl enable docker. - To run
dockerwithoutsudo, add your user to thedockergroup, then log out and back in. - Images are versioned by tags.
docker pull ubuntu:22.04pulls a specific release. - Use
docker system pruneto clean up unused containers, images, and networks. - Data in a container is lost when the container is removed unless stored in a volume.
- For orchestration across multiple hosts, use Docker Swarm or Kubernetes.
- Docker Compose files (
compose.yamlordocker-compose.yml) define multi-container applications.