Docker: Essential Concepts and Key Commands for DevOps

Robin ThakurRobin Thakur
6 min read

What is Docker?

A Docker container is a compact, self-contained, and runnable software bundle that encompasses all essential components for executing a software, such as code, runtime, system tools, libraries, and configurations. It originates from a Docker image and executes application instances within a dependable and reproducible environment, independent of the base system.

Docker containers transformed software development and deployment by offering a uniform environment from development to production, promoting agility, scalability, and reproducibility in software delivery.

Docker Architecture

Docker's architecture centers on these components, allowing developers and DevOps teams to effectively build, deploy, and oversee containerized applications in different environments.

  • Docker Daemon: The Docker daemon (dockerd) is a continuous process that oversees Docker elements such as images, containers, networks, and volumes.

  • Docker Client: The Docker client (docker) is a command-line tool or API that enables users to interact with the Docker daemon. It sends commands to the daemon, which then performs tasks such as building images, running containers, and managing Docker objects.

  • Images: Docker images are immutable blueprints used for creating containers. They encompass the application code, runtime, libraries, dependencies, and other necessary files for running an application. These images are constructed from Dockerfiles and can be housed in repositories such as Docker Hub.

  • Containers: Containers are Docker image instances that can run. They encapsulate the application and its dependencies in an isolated environment, ensuring portability and consistency across various environments.

  • Docker Registry: A registry is a storage system for Docker images. Docker Hub is the default public registry where users can store and share images. Organizations often use private registries to store proprietary or customized images.

  • Docker Engine: The Docker Engine is formed by the combination of the Docker daemon, Docker client, and REST API. Its role is to build, run, and manage containers.

  • Networking: Docker enhances networking for communication between containers and with external networks by creating virtual networks and assigning each container a unique IP address.

  • Volumes: Volumes are essential for storing persistent data in Docker. They enable containers to share data with each other or with the host system, and can also preserve data even after a container is stopped or deleted.

Docker Commands

  • docker run command is essential in Docker, as it is used to create and run containers from Docker images.

  • docker images command lists all Docker images currently stored on your local system, providing details about the repository, tag, image ID, creation time, and size of each image.

  • docker search ommand enables you to browse for Docker images on Docker Hub, the primary public registry for Docker images. It assists in locating images for a wide range of applications, services, or tools that are shared by the community.

  • docker pull command is used for downloading Docker images from a registry. By default, it fetches images from Docker Hub, the default public registry for Docker images.

  • docker ps command is used to list the currently running Docker containers on your system.

    • To list all running containers:

        docker ps
      
    • To display all containers (running and stopped):

        docker ps -a
      
    • To show only the IDs of running containers:

        docker ps -q
      
    • To limit the number of displayed containers:

        docker ps -n 5
      

  • docker stop gracefully halts a container by sending a SIGTERM, allowing cleanup; docker kill forces immediate termination with a SIGKILL.

  • docker start command is used to resume one or more stopped containers, reactivating them from the point where they were stopped.

  • docker logs command enables you to access the logs produced by a particular container, showing the standard output (stdout) and standard error (stderr) logs generated by the container's processes.

  • docker inspect command provides detailed information about Docker objects, such as containers, images, volumes, networks, nodes (in a Swarm cluster), and other Docker entities.

  • docker exec command enables you to run commands within a container that is already running. If you are utilizing Ubuntu as the foundational image for your Docker container, you can employ docker exec to execute commands within an Ubuntu container.

  • docker rm removes stopped containers, while docker rmi deletes Docker images from your system.

Docker Volumes

Docker volumes help save and handle data made by Docker containers. They let data stick around even after a container is gone, making it simple to share info between containers or with the main computer.

  • Create a Volume:

      docker volume create <volume_name>
    
  • List Volumes:

      docker volume ls
    
  • Inspect a Volume:

      docker volume inspect <volume_name>
    
  • Remove a Volume:

      docker volume rm <volume_name>
    
  • Remove Unused Volumes:

      docker volume prune
    

    Removes all volumes not being used by any container.

  • Attach a Volume to a Container: When running a container, you can attach a volume using the -v flag:

      docker run -v <volume_name>:<container_path> <image>
    

Docker Networking

Docker networking enables containers to communicate with each other and the external world.

Types of Docker Networking:

  • Bridge Network: Allows communication between containers on the same Docker host using unique IP addresses.

  • Host Network: Connects containers directly to the host's network, removing network isolation.

  • None Network: Provides network isolation for containers with no network access.

  • Overlay Network: Enables communication between containers across multiple Docker hosts in a Swarm cluster.

  • Macvlan Network: Gives containers individual MAC addresses, appearing as physical devices on the network.

  • Custom/User-defined Networks: Offers flexible, user-configured networks for specific container communication requirements and custom settings.

Docker networking commands:

  • List Networks:

      docker network ls
    
  • Inspect Network Details:

      docker network inspect <network_name>
    
  • Create a Network:

      docker network create <network_name>
    
  • Connect a Container to a Network:

      docker network connect <network_name> <container_name>
    
  • Disconnect a Container from a Network:

      docker network disconnect <network_name> <container_name>
    
  • Remove a Network:

      docker network rm <network_name>
    

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. It enables you to specify your application's services, networks, and volumes in a YAML file, simplifying the management of complex applications with multiple interconnected containers.

Docker Compose commands:

  • Start Containers:

      docker-compose up
    

    Reads the docker-compose.yml file and starts the services defined within it. It creates necessary networks and volumes if they don’t exist.

  • Start Containers in Detached Mode:

      docker-compose up -d
    
  • Stop Containers:

      docker-compose down
    

    It cleans up the resources related to the application.

  • Start Services for a Specific Environment:

      docker-compose -f docker-compose.prod.yml up
    
  • Build or Rebuild Services:

      docker-compose build
    
  • View Logs:

      docker-compose logs
    
  • Scale Services:

      docker-compose up --scale <service_name>=<num_instances>
    
  • Execute a One-Time Command in a Service:

      docker-compose exec <service_name> <command>
    
  • Pause and Unpause Services:

      docker-compose pause
      docker-compose unpause
    

    Happy Learning ^_^

0
Subscribe to my newsletter

Read articles from Robin Thakur directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Robin Thakur
Robin Thakur

I leverage my DevOps and Wordpress skills to deliver innovative and reliable solutions for various applications and websites. I utilize Docker, Jenkins, Terraform, Python, and Bash scripting to streamline application deployments, improve scalability, and reduce overhead. I have successfully developed and maintained CI/CD pipelines, aligned deployment strategies with business needs, and resolved real-time issues in various environments.