Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Getting Started with Docker: Core Concepts, Setup, and Practical Usage

Tech 1

Docker is an open-source application container engine that enables developers to package their applications and depandencies in to portable containers, which can then be deployed and run on any Linux system with Docker installed. Containers provide full sandboxing, ensuring complete isolation from one another.

A complete Docker setup consists of five core components:

  1. Docker Client: The user-facing interface for issuing commands
  2. Docker Daemon: The background process that manages Docker operations
  3. Docker Image: A read-only template for creating containers
  4. Docker Container: A running instance of a Docker image
  5. Docker Registry: A storage repository for Docker images

Docker uses a client-server (C/S) architecture, where the Docker Daemon acts as the server handling requests from the client to create, run, or distribute containers. The client and server can run on the same machine or communicate via a socket or RESTful API. The Docker Daemon typically runs in the background of the host system, while the client provides executable commands for users to interact with the daemon.

Key advantages of Docker include:

  • Cross-Platform Compatibility with Images: Containers standardize application environments into portable images, achieving "build once, run anywhere" functionality.
  • Continuous Deployment and Testing: Eliminates environment inconsistencies between development, testing, and production stages, simplifying CI/CD pipelines.
  • Environment Standardization and Version Control: Container images can be version-controlled, allowing for quick rollbacks and efficient backup/restore operations compared to traditional virtual machine images.
  • High Resource Utilization and Isolation: Containers share the host OS kernel, reducing overhead and enabling more applications to run on the same hardware, while providing precise resource allocation and isolation.
  • Application Image Registry: Docker Hub and private registries offer a vast repository of pre-built images, simplifying access to common services like databases, web servers, and message brokers.

Docker Installation on Linux

Docker runs on CentOS 7.0+ with a 3.10+ kernel. Follow these steps to install Docker CE:

  1. Verify system version and kernel
    cat /etc/redhat-release
    uname -r
    
  2. Install required dependencies
    yum install -y yum-utils device-mapper-persistent-data lvm2
    
  3. Add Docker software repository
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    
  4. Uninstall existing Docker versions (if applicable)
    yum list installed | grep docker
    sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
    
  5. Install Docker CE
    yum install -y docker-ce-19.03.15.ce-1.el7.centos
    
  6. Start Docker service and enable auto-start
    systemctl start docker
    systemctl enable docker
    
  7. Verify installation
    docker version
    

Install Docker Compose

  1. Download Docker Compose binary
    curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    
  2. Grant executable permissions
    chmod +x /usr/local/bin/docker-compose
    
  3. Verify installation
    docker-compose --version
    

Configure Image Registries

  1. Create or edit the Docker daemon configuration file
    cat > /etc/docker/daemon.json <<EOF
    {
      "registry-mirrors": ["https://registry.docker-cn.com", "https://mirror.baidubce.com"],
      "insecure-registries": ["192.168.1.100:5000", "registry.internal.company.com:8080"]
    }
    EOF
    
  2. Restart Docker service
    systemctl restart docker
    
  3. Ensure Docker starts on boot
    systemctl enable docker
    

Docker Images, Containers, and Registries

Docker Images

Docker images are read-only templates for creating containers. They can be pulled from public registries like Docker Hub or private ones. Common image operations:

# Search for images
docker search postgres

# Pull images
docker pull centos:7.9
# Pull from private registry
docker pull registry.internal.company.com:8080/ubuntu:20.04

# Push images
docker push registry.internal.company.com:8080/ubuntu:20.04

# List local images
docker images

# Remove images
docker rmi nginx:1.18.0
# Remove by ID
docker rmi e21c33333d69

# Commit a container to an image
docker commit 8f37a7c70000 registry.internal.company.com:8080/myapp:v1.0

# Tag an image
docker tag e21c33333d69 registry.internal.company.com:8080/myapp:v1.0

# Export/import containers as images
docker export 8f37a7c70000 > myapp_v1.0.tar
cat myapp_v1.0.tar | docker import - myapp:v1.0

# Save/load images (recommended)
docker save registry.internal.company.com:8080/myapp:v1.0 > myapp_v1.0_image.tar
cat myapp_v1.0_image.tar | docker load

Docker Containers

Containers are running instances of Docker images. Common container operations:

# Create and start a container
# Background mode, port mapping, volume mount, auto-restart
docker run -d -p 3000:3000 --name myapp_container --restart always -v ./config:/app/config myapp:v1.0

# Enter a running container
docker exec -it myapp_container /bin/bash

# Stop a container
docker stop myapp_container

# Start a stopped container
docker start myapp_container

# Restart a container
docker restart myapp_container

# Remove a container
docker rm myapp_container

# Stop all running containers
docker stop $(docker ps -q)

# Remove all containers
docker rm $(docker ps -aq)

# Remove all images
docker rmi $(docker images -aq)

Docker Registries

Docker Registry stores and manages container images. Docker Hub is the public registry, while private registries can be deployed internally for secure image distribution within an organization.

Docker Compose Configuration

Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML configuration file (docker-compose.yml) to describe the services, networks, and volumes of an application.

Compose Configuration Structure

A typical Compose file includes three main sections: version, services, and networks.

version: '3.7'
services:
  mqtt:
    image: emqx/emqx:v4.0.0
    container_name: mqtt_broker
    ports:
      - "1883:1883"
    restart: always
    networks:
      - app_network
  myapp:
    image: registry.internal.company.com:8080/myapp:v1.0
    container_name: myapp_server
    restart: always
    ports:
      - "8080:8080"
    volumes:
      - ./app_config:/app/config:rw
    networks:
      - app_network
networks:
  app_network:
    driver: bridge

Key Compose configuration fields:

  • version: Specifies the Compose file format version (3.7 is recommended for Docker 19.03+)
  • services: Defines the containers that make up the application
  • image: Specifies the image to use for the service
  • container_name: Customizes the container name
  • ports: Maps container ports to the host system
  • volumes: Mounts host directories or volumes into the container
  • restart: Configures container restart behavior
  • networks: Connects the service to a specific network

Using Docker Compose

# Start services in background
cd /path/to/compose/config
Docker-compose up -d

# List running services
Docker-compose ps

# Stop services
Docker-compose stop

# Stop and remove services
Docker-compose down

# Update service images
Docker-compose pull

Docker Application Deployment

Deployment Process

  1. Create a deployment directory
    mkdir -p /opt/deploy/myapp
    cd /opt/deploy/myapp
    
  2. Copy the Docker Compose configuration file to the directory
  3. Start the application
    Docker-compose up -d
    
  4. Verify container status
    Docker-compose ps
    
  5. Access the application Open http://<server_ip>:8080 in a web browser

Docker Operation and Maintenance

Common Docker Commands

# List all containers
Docker ps -a

# Find containers by name pattern
Docker ps -a | grep "myapp"

# Stop all containers
Docker stop $(Docker ps -aq)

# Remove all containers
Docker rm $(Docker ps -aq)

# Remove untagged images
Docker rmi $(Docker images | grep "<none>" | awk '{print $3}')

# View container logs
Docker logs -f --tail 50 myapp_container

# Save an image to a tar file
Docker save registry.internal.company.com:8080/myapp:v1.0 | gzip > myapp_v1.0.tar.gz

# Load an image from a tar file
Docker load -i myapp_v1.0.tar.gz

Docker Compose Maintenance

# View service logs
Docker-compose logs -f myapp

# Restart a specific service
Docker-compose restart myapp

# Scale services
Docker-compose up -d --scale myapp=3

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.