Docker


Docker is an open-source platform that automates application deployment inside lightweight, portable containers. Containers package an application with all its dependencies, ensuring it runs consistently across different environments.

Core Concepts of Docker

  1. Containers: Lightweight, standalone packages that contain everything needed to run an application, including code, runtime, libraries, and system tools.
  2. Images: Read-only templates used to create containers. An image is a snapshot of an application and its environment at a specific point in time. It’s typically built from a Dockerfile.
  3. Dockerfile: A text file containing instructions for building a Docker image. It specifies the base image, dependencies, environment variables, commands to run, and other configurations needed to set up the application.
  4. Docker Hub: A cloud-based registry where users can find and share Docker images.
  5. Volumes: Docker’s method for managing persistent storage. Volumes allow data to persist beyond the container’s lifecycle, enabling data sharing between containers or between a container and the host machine.
  6. Docker Compose: A tool for defining and running multi-container Docker applications. Compose uses a docker-compose.yml file to configure the services, networks, and volumes required for an application, making it easy to manage complex applications.

Basic Docker Commands

  1. Setup and Teardown:

    • docker pull <image>: Downloads an image from a registry
    • docker run <options> <image>: Starts a new container from an image.
    • docker stop <container-id>: Stops a running container.
    • docker rm <container-id>: Deletes a stopped container.
    • docker rmi <image>: Deletes an image from your local machine.
  2. Working with Containers

    • docker ps: Lists all running containers.
    • docker ps -a: Lists all containers, including stopped ones.
    • docker logs <container-id>: Shows the logs from a container.
    • docker exec -it <container-id> <command>: Runs a command in a running container.
  3. Building and Managing Images:

    • docker build -t <image-name> .: Builds an image from a Dockerfile in the current directory and tags it with .
    • docker images: Lists all images stored on your local machine.
  4. Volumes:

    • docker volume create <volume-name>: Creates a new volume for persistent data.
    • docker run -v <volume-name>:<container-path> <image>: Attaches a volume to a container.
  5. Docker Compose:

    • docker-compose up: Builds, (re)creates, starts, and attaches to containers for a multi-container application defined in a docker-compose.yml file.
    • docker-compose down: Stops and removes containers, networks, volumes, and images created by docker-compose up.

Best Practices for Using Docker

  1. Keep Images Small: Use lightweight base images (e.g., alpine) and limit dependencies to reduce image size, which speeds up builds and deployments.
  2. Use Multi-Stage Builds: Separate the build environment from the final runtime environment in the Dockerfile. This practice minimizes the final image size.
  3. Minimize Layers in the Dockerfile: Each line in a Dockerfile creates a new layer, so consolidating commands (like combining multiple RUN statements) reduces the number of layers.
  4. Use .dockerignore: Exclude unnecessary files from being added to the image, similar to a .gitignore, to reduce image size and build times.
  5. Set Up Health Checks: Use HEALTHCHECK in the Dockerfile to define a command that verifies the container’s health, allowing automatic restarts if a service becomes unhealthy.
  6. Manage Secrets Securely: Avoid hard-coding secrets in Dockerfiles. Instead, use Docker Secrets, environment variables, or secret management tools to handle sensitive information.