Docker

Docker


1. Docker Components
Docker Image: A lightweight, stand-alone, executable package that includes everything needed to run a piece of software.
Docker Container: A runtime instance of a Docker image.
Docker Hub: A cloud-based registry service where Docker users and partners create, test, store, and distribute container images.
Dockerfile: A text document that contains all the commands a user could call on the command line to assemble an image.
Docker Compose: A tool for defining and running multi-container Docker applications.
Docker Engine: A client-server application managing Docker images, containers, networks, and volumes.
Docker Daemon: Listens for Docker API requests and manages Docker objects.
Docker Client: The interface used to interact with the Docker daemon.
2. Commands
docker --version: Display Docker version.
docker info: Display system-wide information.
docker run image: Run a Docker container from an image.
docker ps: List running Docker containers.
docker ps -a: List all Docker containers.
docker stop container_id: Stop a running container.
docker rm container_id: Remove a Docker container.
docker images: List Docker images.
docker rmi image_id: Remove a Docker image.
3. Dockerfile
  • FROM image: Set the base image.
  • RUN command: Run a command.
  • CMD command: Set a default command that will run when the container starts.
  • ENV VAR=VALUE: Set environment variables.
  • ADD source destination: Copy files from source to the container's filesystem at the destination.
  • COPY source destination: Copy new files or directories from source and add them to the container.
  • ENTRYPOINT command: Configure a container to run as an executable.
  • LABEL: Adds metadata to an image.
  • EXPOSE: Informs Docker that the container listens on specified network ports.
4. Compose
  • docker-compose up: Create and start containers.
  • docker-compose down: Stop and remove containers, networks, images, and volumes.
  • docker-compose build: Build or rebuild services.
  • docker-compose logs: View output from containers.
  • docker-compose restart: Restart services.
5. Networking
  • docker run -d image: Run a Docker container in detached mode.
  • docker run -p host_port:container_port image: Map a port from the host to a container.
  • docker run -v host_volume:container_volume image: Mount a volume from the host to a container.
  • docker run -e VAR=VALUE image: Set environment variables in a container.
  • docker network ls: List networks.
  • docker network create network: Create a network.
  • docker network rm network: Remove a network.
6. Volumes
  • docker volume ls: List volumes.
  • docker volume create volume: Create a volume.
  • docker volume rm volume: Remove a volume.
7. Object Commands
  • docker inspect container_id/image_id: Return low-level information on Docker objects.
  • docker build -t tag .: Build a Docker image with a tag from a Dockerfile in the current directory.
  • docker tag image new_tag: Tag an image with a new tag.
  • docker commit container image: Create a new image from a container's changes.
  • docker history image: Show the history of an image.
8. System Commands
  • docker info: Displays system-wide information.
  • docker version: Shows Docker version information.
  • docker system df: Shows Docker disk usage.
  • docker system events: Gets real-time events from the server.
  • docker system prune: Removes unused data.
9. Docker Swarm
  • docker swarm init: Initialize a swarm.
  • docker swarm join: Join a node to a swarm.
  • docker node ls: List nodes in a swarm.
  • docker service create image: Create a service.
  • docker service ls: List services in a swarm.
  • docker service rm service: Remove a service.
10. Security
  • docker secret create secret file: Create a secret from a file.
  • docker secret ls: List secrets.
  • docker secret rm secret: Remove a secret.
  • Docker Security Scanning: 🔍🛡 Scans Docker images for vulnerabilities.
  • Docker Content Trust: 🤝🔏 Uses digital signatures for data in Docker registries.
  • Docker Secrets: 🤫🔐 Manage sensitive data (passwords, SSH keys, SSL certificates).
11. Advanced Commands
  • docker save image > file: Save an image to a tar archive.
  • docker load < file: Load an image from a tar archive.
  • docker stats: Display live container resource usage statistics.
Docker Best Practices
  • Container Immutability: 🧱🔒 Always create a new container instead of updating a running one.
  • Single Process per Container: 🚀🔧 Each container should handle a single concern.
  • Leverage Build Cache: 🏗🔍 Docker caches results for faster builds.
  • Use .dockerignore: 🙈🔍 Exclude unnecessary files from builds.
  • Service Scaling: 🔢🔧 Scale services up or down in Docker Swarm.
  • Load Balancing: ⚖🚀 Docker has built-in load balancing for replicated services.