Skip to main content

Docker - 1

 

Comprehensive Overview of Docker

Introduction

Docker is a powerful platform designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Docker Architecture

Docker Engine The Docker Engine is a client-server application with three major components:

  • A server which is a type of long-running program called a daemon process (the dockerd command).
  • A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.
  • A command-line interface (CLI) client (docker command).

Docker Daemon The daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes.

Docker Client The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out.

Docker Registries A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can even run your own private registry.

Docker Objects

  • Images
  • Containers
  • Networks
  • Volumes

Key Docker Concepts

Images An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization.

Containers A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

Services In a distributed application, different pieces of the app are called “services”. For example, a database and a web front end are separate services. In Docker, these services can be defined using a docker-compose.yml file.

Using Docker

Installation Installation of Docker varies based on the platform. Docker provides packages that easily configure the Docker environment on macOS, Windows, and Linux.

Basic Commands

  • docker run creates and starts a container in one operation.
  • docker stop stops a running container.
  • docker build creates an image from a Dockerfile.
  • docker pull pulls an image or a repository from a registry.
  • docker push pushes an image or a repository to a registry.
  • docker export exports a container’s filesystem as a tar archive.
  • docker exec runs a command in a run-time container.

Dockerfiles A Dockerfile is a script comprised of various commands and arguments listed successively to automatically perform actions on a base image. These actions create a new image and include it capable of launching new containers that will operate as defined by the Dockerfile.

Example Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]

Networking in Docker Docker’s networking subsystem is pluggable, using drivers. Several drivers exist by default, and provide core networking functionality:

  • bridge: The default network driver.
  • host: For standalone containers, remove network isolation between the container and the Docker host.
  • overlay: Connect multiple Docker daemons together and enable swarm services to communicate with each other.
  • macvlan: Assign a MAC address to a container, making it appear as a physical device on your network.

Docker Best Practices

  • Use a .dockerignore file to exclude files that are not necessary for building a Docker image, such as logs, cache files, and other artifacts.
  • Minimize the number of layers within images. Try to organize commands and their dependencies together.
  • Use multi-stage builds to keep images small in size.
  • Leverage build cache.
  • Use environment variables for passing secrets or other variables that can change between deployments.

Advanced Topics

  • Docker Compose: Tool for defining and running multi-container Docker applications.
  • Docker Swarm: Native clustering functionality for Docker containers, which turns a group of Docker engines into a single virtual Docker engine.
  • Kubernetes Integration: Docker can be integrated with Kubernetes to manage containers across a cluster.

Comments

Popular posts from this blog

DevOps Vs DevSecOps

   DevOps and DevSecOps are two methodologies that have gained traction in the IT industry for streamlining software development and deployment. However, their approach to security and operations differs, making each suitable for different types of projects and organizational needs. Let's explore DevOps versus DevSecOps with a real-time example, focusing on their distinctions, integration, and practical applications. DevOps: The Foundation DevOps is a cultural and professional movement that emphasizes collaboration and communication between software developers and other IT professionals while automating the process of software delivery and infrastructure changes. It aims to shorten the development life cycle and provide continuous delivery with high software quality. Core Principles: Continuous Integration and Continuous Deployment (CI/CD): Automate building, testing, and deployment of applications. Collaboration: Breaking down silos between teams (developers, IT operations...

Deploying a Node.js project to Azure App Services using Azure DevOps pipelines

Deploying a Node.js project to Azure App Services using Azure DevOps pipelines is a robust way to automate deployment processes and integrate continuous integration and deployment (CI/CD) practices into your workflow. This guide will walk you through the setup of an Azure DevOps pipeline to deploy a Node.js application from GitHub or Azure Repos to Azure App Services. Prerequisites Before you begin, ensure you have the following: An Azure account. You can sign up for a free account here . A GitHub or Azure Repos account with your Node.js project. An Azure DevOps account. Create one here if you don't have it. Step 1: Prepare Your Node.js Application Make sure your Node.js application is ready and includes a package.json file in the root. This file is crucial as it contains dependency information and scripts needed for your application. Step 2: Create an Azure Web App Log into Azure Portal: Visit https://portal.azure.com . Create a Web App: Click on "Create a resource". ...

Git Cheat Sheet

  Git Cheat Sheet Category Command Description Setup git config --global user.name "[name]" Set a name that will be attached to your commits and tags. git config --global user.email "[email]" Set an email that will be attached to your commits and tags. git init Initialize a new git repository in the current directory. git clone [url] Clone a repository into a new directory. Stage & Snapshot git status Show modified files in the working directory, staged for your next commit. git add [file] Add a file as it looks now to your next commit (stage). git reset [file] Unstage a file while retaining the changes in the working directory. git diff Show diff of what is changed but not staged. git diff --staged Diff of what is staged but not yet committed. git commit -m "[message]" Commit your staged content as a new commit snapshot. Branch & Merge git branch List all of the branches in your repo. git branch [name] Create a new branch at the current commit. gi...