Docker - Introduction, Architecture, and Most used Commands

WHAT TO KNOW - Sep 13 - - Dev Community

<!DOCTYPE html>





Docker: A Comprehensive Guide

<br> body {<br> font-family: Arial, sans-serif;<br> line-height: 1.6;<br> margin: 0;<br> padding: 0;<br> }</p> <div class="highlight"><pre class="highlight plaintext"><code> h1, h2, h3, h4 { color: #333; } h1 { font-size: 2.5em; margin-bottom: 1em; } h2 { font-size: 2em; margin-bottom: 0.8em; } h3 { font-size: 1.5em; margin-bottom: 0.6em; } h4 { font-size: 1.2em; margin-bottom: 0.4em; } p { margin-bottom: 1em; } code { background-color: #eee; padding: 2px 4px; border-radius: 4px; font-family: Consolas, monospace; } pre { background-color: #eee; padding: 1em; border-radius: 4px; overflow-x: auto; } img { max-width: 100%; height: auto; display: block; margin: 1em 0; } .container { max-width: 800px; margin: 2em auto; padding: 0 1em; } .section { margin-bottom: 2em; } .code-block { background-color: #eee; padding: 1em; border-radius: 4px; overflow-x: auto; margin-bottom: 1em; } .code-block pre { margin: 0; padding: 0; } </code></pre></div> <p>




Docker: A Comprehensive Guide




Introduction



Docker is an open-source platform that enables developers to build, ship, and run applications in lightweight, portable containers. It revolutionizes software development by promoting consistency, efficiency, and scalability. Docker containers package applications and their dependencies, ensuring that they run the same way regardless of the environment. This eliminates the "it works on my machine" problem and facilitates seamless deployment across different systems.


Docker logo



Why Docker?



Docker offers numerous advantages, making it a preferred choice for developers and organizations:



  • Consistent Environments:
    Containers ensure that applications run identically across development, testing, and production environments.

  • Faster Development Cycles:
    Docker streamlines the development workflow, allowing developers to quickly build, test, and deploy applications.

  • Resource Efficiency:
    Containers are lightweight and consume fewer resources compared to virtual machines.

  • Improved Scalability:
    Docker containers can be easily scaled up or down to meet changing demands.

  • Simplified Deployment:
    Docker simplifies deployment by packaging applications and their dependencies into portable units.

  • Enhanced Collaboration:
    Docker allows developers to share and reuse containers, promoting collaboration and code reusability.




Docker Architecture



Docker's architecture is built around several key components:



Docker Client



The Docker client is a command-line interface (CLI) or a graphical user interface (GUI) that allows users to interact with the Docker daemon. It sends instructions to the daemon to build, run, and manage containers.



Docker Daemon



The Docker daemon is the background service that runs on the host machine. It receives instructions from the client and manages the lifecycle of containers, images, and networks.



Docker Images



Docker images are read-only templates that contain everything needed to run an application, including the application code, libraries, tools, and dependencies. They serve as blueprints for creating containers.



Docker Containers



Docker containers are runtime instances of Docker images. They are created from images and run on the host machine, providing isolated environments for applications.



Docker Registry



Docker registries store Docker images. They act as centralized repositories where images can be shared and retrieved. Docker Hub is a publicly accessible registry, while private registries can be used for internal image storage.


Docker architecture diagram



Docker Commands



Here are some of the most frequently used Docker commands:




  • docker version


    Displays the Docker client and server versions.






  • docker search [image_name]





    Searches for Docker images in the Docker Hub registry.






  • docker pull [image_name]





    Downloads an image from a registry to the local system.






  • docker run [image_name]





    Creates and runs a container from an image.






  • docker ps





    Lists all running containers.






  • docker ps -a





    Lists all containers, including stopped ones.






  • docker exec -it [container_id] [command]





    Executes a command inside a running container.






  • docker stop [container_id]





    Stops a running container.






  • docker restart [container_id]





    Restarts a stopped container.






  • docker rm [container_id]





    Removes a stopped container.






  • docker images





    Lists all downloaded Docker images.






  • docker rmi [image_id]





    Removes an image from the local system.






  • docker build [context] -t [image_name]





    Builds a new Docker image from a Dockerfile.






  • docker push [image_name]





    Uploads a Docker image to a registry.










Dockerfile





A Dockerfile is a text file that contains instructions for building a Docker image. It defines the base image, the application code, dependencies, and configuration settings for the container. Here is an example Dockerfile:







FROM node:18-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

CMD ["npm", "start"]







This Dockerfile builds an image based on the Node.js 18 Alpine image, copies the package.json file, installs dependencies, copies the application code, and starts the application by running the 'npm start' command.










Docker Compose





Docker Compose is a tool that defines and manages multi-container Docker applications. It uses a YAML file to define the services, networks, and volumes required for the application. This makes it easier to orchestrate and deploy complex applications with multiple containers.







version: "3.9"

services:

web:

build: .

ports:

- "80:80"

depends_on:

- db

db:

image: mysql:8.0

environment:

MYSQL_ROOT_PASSWORD: "password"

MYSQL_DATABASE: "mydatabase"







This Docker Compose file defines two services: "web" and "db." The "web" service builds from the current directory, exposes port 80, and depends on the "db" service. The "db" service uses the MySQL 8.0 image and defines environment variables for the root password and database name.










Docker Networking





Docker provides different networking options to connect containers and enable communication between them:






Bridge Network





The default network in Docker. It creates a private network for containers to communicate with each other.






Host Network





Allows containers to use the host machine's network stack. Containers can access services running on the host machine directly.






None Network





Containers on the "none" network are isolated and cannot communicate with other containers or the host machine.






Overlay Network





Provides a more flexible and scalable networking solution for multi-host deployments.










Docker Volumes





Docker volumes provide persistent storage for containers. Data stored in volumes persists even if the container is removed or recreated. This allows you to share data between containers or store data outside the container's filesystem.






Named Volumes





Named volumes are managed by Docker and can be shared between containers. They persist even if the container is deleted.






Anonymous Volumes





Anonymous volumes are created implicitly when a container is started. They are tied to a specific container and are deleted when the container is removed.






Bind Mounts





Bind mounts map a directory on the host machine to a directory inside the container. Changes made to the host directory are reflected in the container, and vice versa.










Docker Hub





Docker Hub is a publicly accessible registry that hosts Docker images. It provides a platform for developers to share, discover, and download Docker images. Docker Hub offers a wide range of official images, as well as community-contributed images.










Conclusion





Docker has become an essential tool for modern software development, enabling developers to build, ship, and run applications efficiently and consistently. By leveraging containers, Docker simplifies application deployment, improves resource utilization, and promotes collaboration. Understanding Docker's architecture, commands, and best practices is crucial for harnessing its power and optimizing your software development process.








. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .