Docker: introduction to containers
Introduction to Docker: what containers are, how they package an application with its dependencies, and why they isolate better than virtual machines.

Introduction to Docker
Containerization is a software development approach in which an application or service, its dependencies, and its configuration (extracted as deployment manifest files) are packaged as a container image. The containerized application can be tested as a unit and deployed as a container image instance on the host operating system (OS).Just as shipping containers allow goods to be transported by ship, train, or truck regardless of the cargo inside, software containers act as a standard unit of software deployment that can contain different code and dependencies. Software in containers in this way allows developers and IT professionals to deploy them in environments with little or no modification.Containers also isolate applications from each other on a shared operating system. Containerized applications run on a container host, which in turn runs on the operating system (Linux or Windows). Therefore, containers have a significantly smaller footprint than virtual machine (VM) images.Each container can run a complete web application or service, as shown in Figure 2-1. In this example, the Docker host is a container host, and App1, App2, Svc 1, and Svc 2 are containerized applications or services.
Motivation
Cargo containers completely transformed the shipping industry and enabled the global commerce experience of today. Similarly, software containers simplify application development and deployment, which helps enable the cloud-native software architecture that powers the modern technology we all rely on. Although you can get benefits from containerizing your applications after the fact, you get the most value when you take a container-first approach.
Fundaments
Docker Architecture
Docker uses a client-server architecture. The Docker client talks to the Docker Daemon, which does the heavy lifting of building, running, and distributing containers. Both the client and the Daemon can run on the same system, or you can connect a remote client to a Docker daemon.
The Docker client is the primary user interface for Docker. It accepts commands from the user and communicates with the Docker daemon.
Docker Images
Docker images are read-only templates. That is, an image can contain a CentOS or Ubuntu operating system with Apache installed, but this will only allow us to create containers based on this configuration. If we make changes to an already running container, when we stop it, these changes will not be reflected in the image. We'll understand this part better later on.
Docker Registries
Docker registries store images. These are public or private repositories where we can upload or download images. The public registry is provided by Docker Hub, which serves a collection of images for our use. Docker registries are basically the Distribution component of Docker.
Docker Containers
A Docker container hosts everything needed to run an application. Each container is created from a Docker image. Each container is an isolated platform.
Docker Installation Guide
Linux Installation
Ubuntu/Debian
- Update the system
sudo apt update
sudo apt upgrade -y- Install required dependencies
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y- Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg- Add Docker repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null- Install Docker Engine
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y- Verify installation
sudo docker --version
sudo docker run hello-world- Add user to docker group (optional)
sudo usermod -aG docker $USER
newgrp dockerCentOS/RHEL/Fedora
- Update the system
sudo yum update -y- Install dependencies
sudo yum install -y yum-utils- Add Docker repository
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo- Install Docker Engine
sudo yum install docker-ce docker-ce-cli containerd.io -y- Start and enable Docker
sudo systemctl start docker
sudo systemctl enable docker- Verify installation
sudo docker --version
sudo docker run hello-worldWindows Installation
For Windows, it's sufficient to install Docker Desktop, which includes all the necessary tools:
- Download Docker Desktop from: https://www.docker.com/products/docker-desktop
- Run the downloaded installer
- Follow the installation wizard instructions
- Restart your system if required
- Open Docker Desktop and wait for it to start
- Verify installation by opening PowerShell or CMD:
docker --version
docker run hello-worldRequirements:
- Windows 10/11 Pro, Enterprise, or Education (64-bit)
- WSL 2 enabled
- Virtualization enabled in BIOS
Getting Started with Docker
In previous chapters we saw what Docker is and how to install it on Linux CentOS and Ubuntu. But before diving fully into Docker, remember I mentioned Docker registries? I want to briefly talk to you about Docker Hub.
Docker Hub is a cloud-based registry service for building and delivering service or application containers. It provides a centralized resource for container discovery, distribution, change management, team collaboration, and workflow automation. Think of it as a Github for containers.
Docker Hub Features
- Image Repositories: Find, manage, upload and download community and official images
- Automated Images: Create new images when you make a change to the Github or BitBucket source
- Webhooks: automatic image builds when making a successful push to a repository
- GitHub and BitBucket Integration
Docker Hub provides you and your organization a place to host and deliver images.
Docker Hub repositories can be configured in two ways:
- Repositories, which allow us to upload and update images whenever we want from the docker daemon
- Automated Images that allow us to configure a Github or BitBucket account that triggers the rebuilding of an image when a change is made to the repository
You can create public repositories that can be accessed by other HUB users, or you can create private repositories with limited access.
You can create your account at https://hub.docker.com, later we'll see how we can take advantage of all this.
The Docker daemon uses Hub to get and upload its images. Now that we have a foundation, let's play around with Docker a bit.
Search for a CentOS base image
$ docker search centosWe're going to download the official CentOS base image. So we look at NAME and pass it to docker pull:
$ docker pull docker.io/centosAnother way we can search for an image is through the Hub.
If we search for centos we'll have something like this.
Here we have the command and tags to get a specific version. As you can see we have the command docker pull centos, and if we want to download for example CentOS version 7 or the latest version, the commands would look like this:
$ docker pull centos:centos7
$ docker pull centos:latestOnce the image is downloaded, we confirm it's there with:
$ docker imagesdocker.io/centos latest ce20c473cd8a 2 weeks ago 172.3 MB
Now we can see we have the image there. Now how can we interact with that image or launch a container with that image? Let's get to that, but first remember this: when we launch a container and perform any task, this is not reflected when launching another container with the same image.
Launch a container
We're going to launch a container based on that image and interact with the command line of that container:
$ docker run -i -t docker.io/centos:latest /bin/bash-i: Interactive-t: tty
[root@71c6ab01cd3e /]#
Here we're already interacting with the container, that number you see after root is the container ID, we can update the System, install packages, etc. This container also has an IP that we can use to interact with the container.
View running containers
Let's see what containers we have running. Open another terminal on the server:
$ docker psHere we have the container ID, the image we used to launch it, the command we ran, and at the end the Name. With this name that docker randomly assigned we can interact with the container. We can also assign this Name ourselves.
Stop a container
If we want to stop the container:
$ docker stop reverent_bardeen(which is the name assigned by docker)
If we run docker ps, we'll see there's nothing there anymore. The good thing is that this container remains there, with the changes we made to it.
$ docker ps -aRestart a container
We can restart that container again and connect to it:
$ docker start reverent_bardeen
$ docker attach reverent_bardeenHere we are connected to our container again.
Authors

GIIA