352.1 Container Virtualization Concepts (weight: 7)

Tech Tutorial: 352.1 Container Virtualization Concepts (weight: 7) #

Introduction #

In this tutorial, we will delve into the concept of container virtualization, focusing on Docker, which is one of the most popular containerization platforms. Container virtualization allows developers to package applications with all of their dependencies into a container that can run on any Linux machine. This ensures consistency across environments, simplifies development, and enhances scalability and isolation.

Exam Objective: #

  • Understand and identify what container virtualization is
  • Understand the role and implementation of Docker
  • Manage containers and images using Docker

Key Knowledge Areas: #

  • Basic concepts of containerization
  • Differences between containers and virtual machines
  • Docker installation and configuration
  • Managing Docker containers
  • Managing Docker images

Utilities: #

  • docker
  • docker-compose

Step-by-Step Guide #

Docker Installation #

First, ensure your system is ready to install Docker. We’ll be using Ubuntu for this example:

  1. Update your package index:

    sudo apt-get update
    
  2. Install packages to allow apt to use a repository over HTTPS:

    sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
    
  3. Add Docker’s official GPG key:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
  4. Set up the stable repository:

    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    
  5. Install Docker CE:

    sudo apt-get update
    sudo apt-get install docker-ce
    
  6. Verify that Docker CE is installed correctly by running the hello-world image:

    sudo docker run hello-world
    

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

Managing Docker Containers #

Running a container: #

  • Run an interactive Ubuntu container:

    sudo docker run -it ubuntu /bin/bash
    
  • List running containers:

    sudo docker ps
    
  • List all containers (running and stopped):

    sudo docker ps -a
    

Stopping and starting containers: #

  • Stop a container:

    sudo docker stop [CONTAINER_ID]
    
  • Start a stopped container:

    sudo docker start [CONTAINER_ID]
    

Managing Docker Images #

Working with images: #

  • Pull an image from Docker Hub:

    sudo docker pull nginx
    
  • List all images on the local system:

    sudo docker images
    
  • Remove an image:

    sudo docker rmi [IMAGE_ID]
    

Docker Compose #

Docker Compose simplifies the deployment of multi-container applications.

  • Install Docker Compose:

    sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    
  • Run a Docker Compose file:

    # docker-compose.yml
    version: '3'
    services:
      web:
        image: nginx
        ports:
          - "80:80"
      database:
        image: postgres
        environment:
          POSTGRES_PASSWORD: example
    
    sudo docker-compose up
    

Conclusion #

Container virtualization with Docker provides a powerful, efficient, and scalable way to deploy and manage applications. By encapsulating applications and their environments into containers, Docker allows for portable and consistent behavior across different platforms and development stages. Learning how to effectively use Docker commands and understanding container concepts are crucial skills for developers and system administrators in today’s IT landscape.