January 29, 2023


Docker is a powerful platform for developing, shipping, and running applications in containers. It ensures consistency across environments and simplifies deployment. Below is a detailed breakdown of essential Docker commands and concepts to help you manage containers efficiently.
The docker run command combines several operations:
docker pull)docker create)docker start)docker run <image_name>-i: Keeps STDIN open for interactive processes-t: Allocates a pseudo-TTY (terminal)
Together they create an interactive shell session. Essential for debugging or when containers need terminal input.-d): Runs container in backgroundstop: SIGTERM (graceful shutdown)kill: SIGKILL (immediate termination)Bridge Networks:
Host Networks:
Overlay Networks:
--link is deprecated in favor of DNS-based discoverybridge: Default network (NAT)host: Shares host's network stacknone: No networking-P: Auto-maps all EXPOSE ports to random ephemeral ports (32768-60999)-p: Explicit mapping (host_port:container_port)Docker uses storage drivers to manage image layers and container writable layers:
overlay2 (default on Linux)aufsdevicemapperzfsBind Mounts:
Named Volumes:
/var/lib/docker/volumes/tmpfs Mounts:
Docker's multi-stage build capability allows creating optimized production images by separating build-time dependencies from runtime requirements. The build process occurs in multiple phases where only the necessary artifacts are copied to the final image. This approach significantly reduces image size since build tools and intermediate files don't get included in the production image. For example, a Node.js application might use one stage with the full Node.js SDK for building, and then only copy the built JavaScript files to a lightweight nginx stage for serving.
# Build stage with full toolchain
FROM node:16 as builder
WORKDIR /app
COPY . .
RUN npm install && npm run build
# Production stage with only runtime
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/htmlSecure container deployment follows several key principles. First, containers should always run as non-root users whenever possible to limit potential damage from container breakout scenarios. Second, regular vulnerability scanning of images helps identify known security issues in dependencies. Third, careful management of build contexts through .dockerignore files prevents accidental inclusion of sensitive files. These practices combine to create defense-in-depth for containerized applications.
FROM node:16
RUN useradd -m appuser && chown -R appuser /app
USER appuser # Principle of least privilegeDocker's BuildKit represents an evolution of the image building process, introducing parallel stage execution, improved cache efficiency, and advanced features like secret management. When enabled, it transforms the build process by creating a low-level build graph that can execute operations concurrently. The secret management feature allows securely passing credentials during build without leaving traces in the final image or intermediate layers.
# syntax=docker/dockerfile:1.3
FROM alpine
RUN --mount=type=secret,id=mysecret \
cat /run/secrets/mysecret > /config.txtThe healthcheck system provides a way for containers to self-report their operational status. This mechanism is particularly valuable in orchestrated environments where systems need to automatically detect and recover from failed containers. Health checks typically verify that application endpoints are responding or that internal processes are functioning correctly.
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost/health || exit 1Effective container deployment requires proper resource constraints to prevent any single container from monopolizing system resources. Docker allows setting memory limits, CPU shares, and other constraints that ensure fair resource allocation. These controls are especially important in multi-tenant environments where multiple containers share the same host.
docker run -it --memory="512m" --cpus="1.5" app-serverDocker provides native clustering through Swarm mode, which offers service discovery, load balancing, and rolling updates. The architecture consists of manager nodes that control the cluster and worker nodes that run containers. Services can be scaled horizontally, and the swarm handles distributing instances across available nodes.
docker swarm init
docker service create --name web --replicas 3 -p 80:80 nginxDocker Content Trust implements digital signing of images using Notary, providing cryptographic verification of image authenticity. When enabled, it ensures that only signed images can be pulled or run, protecting against tampering in the supply chain. This is particularly valuable for production environments where image integrity is critical.
export DOCKER_CONTENT_TRUST=1
docker pull organization/trusted-image| Command | Description |
|---|---|
docker run <image> | Run a container from an image |
docker run -it <image> /bin/bash | Run container interactively with shell access |
docker run -d <image> | Run container in detached mode |
docker run --name <name> <image> | Run container with custom name |
docker ps | List running containers |
docker ps -a | List all containers (including stopped) |
docker start <container> | Start a stopped container |
docker stop <container> | Stop a running container gracefully |
docker restart <container> | Restart a container |
docker kill <container> | Force stop a container |
docker rm <container> | Remove a stopped container |
docker rm -f <container> | Force remove a running container |
docker container prune | Remove all stopped containers |
docker exec <container> ls | Run a command inside a running container |
| Command | Description |
|---|---|
docker images | List all images |
docker pull <image> | Download an image from registry |
docker push <image> | Push an image to registry |
docker rmi <image> | Remove an image |
docker image prune | Remove unused images |
docker build -t <name> . | Build image from Dockerfile |
docker history <image> | Show image layers |
docker tag <image> <new_tag> | Tag an image |
| Command | Description |
|---|---|
docker network ls | List networks |
docker network create <name> | Create a network |
docker network inspect <name> | Inspect a network |
docker network connect <network> <container> | Connect container to network |
docker network disconnect <network> <container> | Disconnect container from network |
docker network prune | Remove unused networks |
| Command | Description |
|---|---|
docker volume ls | List volumes |
docker volume create <name> | Create a volume |
docker volume inspect <name> | Inspect a volume |
docker volume rm <name> | Remove a volume |
docker volume prune | Remove unused volumes |
docker run -v /host/path:/container/path <image> | Bind mount host directory |
docker run -v volume_name:/container/path <image> | Mount named volume |
| Command | Description |
|---|---|
docker run -p 8080:80 <image> | Map host port 8080 to container port 80 |
docker run -P <image> | Auto-map all exposed ports to random ports |
docker port <container> | Show port mappings for container |
| Command | Description |
|---|---|
docker logs <container> | View container logs |
docker logs -f <container> | Follow logs in real-time |
docker stats | Show live container resource usage |
docker top <container> | Show running processes in container |
docker events | Show real-time events from server |
| Command | Description |
|---|---|
docker info | Display system-wide information |
docker version | Show Docker version |
docker system df | Show disk usage |
docker system prune | Remove unused data (images, containers, networks) |
docker system prune -a | Remove all unused data including dangling images |
| Command | Description |
|---|---|
docker-compose up | Create and start containers |
docker-compose up -d | Start containers in detached mode |
docker-compose down | Stop and remove containers |
docker-compose ps | List containers |
docker-compose logs | View output from containers |
docker-compose build | Build images |
docker-compose exec <service> <command> | Execute command in running service |
Docker simplifies application deployment by encapsulating environments in containers. Mastering these commands will help you efficiently manage containers, networks, and volumes.
By understanding these Docker fundamentals, you can streamline your development and deployment workflows. Happy Dockering! 🐳
Thank you for reading! I hope you found this post insightful. Stay curious and keep learning!
📫 Connect with me:
© 2025 Ayush Rudani