- What is Docker?
- Docker architecture and components.
- Installing Docker.
- Real-time use case: Creating a simple web application with Docker.
What is Docker?
Docker is an open-source platform that enables developers to automate the deployment, scaling, and management of applications within lightweight, portable containers.
Containers are self-contained units that package an application along with its dependencies, libraries, and configurations, ensuring consistency and portability across different environments.
Docker makes it easier to develop, ship, and run applications in a consistent and isolated manner.
In more detail, Docker consists of the following components:
1. Docker Engine: The core component of Docker that enables container management. It includes the Docker daemon (dockerd), which runs as a background service, and the Docker CLI (docker), which provides a command-line interface for interacting with Docker.
2. Docker Image: A read-only template that contains the application code, libraries, and dependencies required to run a container. Images serve as the building blocks for containers. Docker images can be based on other images, and they are versioned to allow for reproducibility.
3. Docker Container: A running instance of a Docker image. Containers are isolated from the host system and other containers, but they share the host OS kernel. This isolation ensures that applications can run consistently regardless of the underlying infrastructure.
4. Docker Registry: A repository that stores Docker images. Docker Hub is the default public registry that allows developers to access and share images. Private registries can also be used for more secure image storage.
Docker architecture and components.
Docker follows a client-server architecture, with the main components being Docker Client, Docker Daemon, and Docker Registry. Let’s explain each of these components and their interactions using a neat diagram:
1. Docker Client (CLI):
· The Docker Client is the command-line interface (CLI) that allows users to interact with Docker. It sends commands to the Docker Daemon to manage containers, images, networks, and other Docker resources. Users interact with the Docker Client by running commands like docker run
, docker build
, etc.
2. Docker Daemon (dockerd):
· The Docker Daemon is the core engine that runs as a background service on the host machine. It manages the containers, images, and other Docker objects. The Docker Daemon listens to commands from the Docker Client through a REST API and executes those commands accordingly.
3. Docker Containers:
· Containers are isolated, lightweight, and portable runtime instances of Docker images. Each container runs a specific application along with its dependencies in an isolated environment. Containers are created from Docker images.
4. Docker Images:
· Docker Images are read-only templates that contain the application code, libraries, dependencies, and configurations required to run a container. Images are the building blocks used to create containers.
5. Docker Registry (Docker Hub):
· The Docker Registry is a repository that stores Docker images. Docker Hub is the default public registry managed by Docker, where developers can access and share Docker images. Private registries can also be used for more secure image storage.
The interaction between these components is as follows:
· The Docker Client communicates with the Docker Daemon through a REST API to send commands and manage Docker resources.
· The Docker Daemon manages containers, images, networks, and volumes on the host machine.
· Docker Images are used to create containers, which run isolated applications on the host machine.
· Docker Registries store Docker images, making them accessible to users worldwide.
This architecture allows developers to create, share, and run applications in a consistent and isolated manner across different environments, making Docker a powerful tool for containerization and application deployment.
Installing Docker
The process of installing Docker may vary slightly depending on your operating system. Below are the general steps for installing Docker on some popular platforms:
1. Install Docker on Linux (Ubuntu/Debian):
Open a terminal and run the following commands one by one:
# Update the package index
sudo apt update
# Install required dependencies
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
# Add Docker 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=amd64 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
# Update the package index again
sudo apt update
# Install Docker
sudo apt install -y docker-ce docker-ce-cli containerd.io
# Start Docker service
sudo systemctl start docker
# Add your user to the ‘docker’ group to run Docker commands without sudo
sudo usermod -aG docker $USER
2. Install Docker on macOS:
1. Download the Docker Desktop for Mac installer from the Docker website (https://www.docker.com/products/docker-desktop) and double-click it to start the installation.
2. Drag and drop the Docker.app to the Applications folder to complete the installation.
3. Launch Docker Desktop from the Applications folder, and it will run in the background.
3. Install Docker on Windows:
1. Download the Docker Desktop for Windows installer from the Docker website (https://www.docker.com/products/docker-desktop) and run it.
2. Follow the on-screen instructions to complete the installation. Docker Desktop will install the necessary components, including Docker Engine, on your system.
3. Once the installation is complete, Docker Desktop will be available in your system tray, and Docker commands can be run from the command prompt or PowerShell.
4. Verify Docker Installation:
After installing Docker, you can verify the installation by running the following command in the terminal or command prompt:
If Docker is properly installed, it will display the version number.
Running Your First Docker Container
· Use the docker run command to pull an image (if not available locally) and run a container.
· For example, run a simple “Hello, World!” web server using the official Nginx Docker image.
Real-time Use Case:
Create a simple web application using Docker: Pull the Nginx image and run it as a container, exposing port 80, then access the web server in your browser.
Step 1: Install Docker Make sure you have Docker installed. You can download and install Docker from the official website for your specific operating system.
Step 2: Pull Nginx Image Open a terminal or command prompt and pull the official Nginx image from Docker Hub using the following command:
docker pull nginx
Step 3: Run Nginx Container Now, run the Nginx container using the docker run command:
docker run -d -p 80:80 — — name my_web_app nginx.
Explanation of the command options:
· -d: Detached mode. The container will run in the background.
· -p 80:80: Publishes port 80 from the container to port 80 on the host machine. This allows you to access the web server on your browser.
· — — name my_web_app: Assigns a custom name (“my_web_app”) to the container for easier identification.
· nginx: The name of the image to run (in this case, the official Nginx image).
Step 4: Access the Web Server Now that the Nginx container is running, open your web browser and enter the following URL:
http://localhost or Public IP
You should see the default Nginx landing page, indicating that the web server is up and running successfully.
List local images
docker images
To list currently running containers:
docker ps
If you want to stop the container, you can use the following command:
docker stop my_web_app
And if you want to remove the container, you can use:
docker rm my_web_app
With these steps, you have successfully created a simple web application using Docker and Nginx. This is just the beginning, and Docker’s real power comes when you start building and deploying more complex applications in containers. Happy Dockerizing!
Happy Learning.
Sudheer Baraker