Docker Introduction & Installation

Follow Us

Our Communities

Chapter 1: Introduction & Installation

Welcome to the first chapter of our comprehensive Docker tutorial! As we embark on this journey to master the fascinating world of containerization, we’ll start by laying a strong foundation, understanding the core principles and components that drive Docker, one of the most popular containerization platforms in the IT industry.

In this chapter, we’ll start with an “Introduction to Docker”, where we’ll explore what Docker is, why it has become so integral in today’s tech landscape, and how it revolutionizes the way we develop, distribute, and run applications. We’ll then dive into the structure of Docker, examining its unique “Docker Architecture”. This will help us understand the various components that make Docker so flexible and robust, from Docker images and containers to the Docker daemon and client.

After building a conceptual understanding of Docker, we’ll get our hands dirty by “Installing Docker”. We’ll guide you through the installation process for various operating systems, including Windows, MacOS, and Linux, ensuring you’re ready to get started, irrespective of your OS preference. Once installed, we’ll “Verify the Installation” by running a simple Docker command, ensuring that your Docker environment is set up correctly.

Finally, we’ll familiarize ourselves with some “Basic Docker Commands”. These commands are the tools you’ll use to interact with Docker, and understanding them is fundamental to navigating the Docker ecosystem. We’ll explore commands for managing Docker images and containers, running and stopping containers, and even building Docker images from Dockerfiles.

By the end of this chapter, you will have a solid understanding of Docker’s fundamentals and will have set up your own Docker environment ready for hands-on learning. With this groundwork laid, you’ll be well-prepared to dive deeper into the world of Docker, exploring more complex topics and techniques in the subsequent chapters. Let’s get started!

Introduction to Docker

Docker is an open-source platform that offers a streamlined, efficient approach to software deployment. Docker uses containerization technology to build, test, and deploy applications quickly and reliably.

What is Docker?

At its core, Docker is a tool that enables developers to wrap their applications and dependencies into a standardized unit for software development. This unit is known as a Docker container. Containers have all the necessary components like libraries, system tools, code, and runtime to run the application, ensuring it behaves the same way regardless of the environment it’s running on.

Docker is built on the concept of containerization. A container is a standardized unit, which can be created on the fly to deploy a particular application or environment. It could be an Ubuntu container, a CentOS container, or any other base image that can be run as a container. Inside this container, you can pack your application and any dependencies it needs to run.

Why is Docker Important?

Efficiency and Speed: Traditional virtualization approaches involve running multiple full-fledged virtual machines that include their own entire operating system, with the software and dependencies running atop it. Docker, on the other hand, allows applications to use the same Linux kernel as the system they’re running on and only requires applications to be shipped with things not already running on the host computer. This results in significant performance benefits.

Consistency: Docker containers ensure consistency across multiple development and release cycles by creating a standard Docker environment. This standardization helps developers to eliminate the “it works on my machine” problem. The Docker container running on a developer’s laptop will also run at scale, in production, on VMs, bare-metal servers, OpenStack clusters, public instances, or any combination of them.

Modularity and Scalability: Docker’s containerized model allows developers to break out the application’s functionality into individual containers. For example, the database can live in one container, the application backend in another, and the frontend in yet another. These containers can be spun up and down independently and offer real-time scalability according to the needs of the environment.

CI/CD Integration: Docker fits well with Continuous Integration/Continuous Deployment (CI/CD) workflows. The development, testing, and production teams can use the same container but define their own settings. Docker images can be shared and updated at any stage of the pipeline, and they can be spun up or down as needed.

To summarize, Docker has revolutionized the way software is developed, shipped, and run. It has given developers the freedom to focus on their applications rather than system compatibilities and dependencies. The ability to containerize applications means faster deployments, scalable applications, resource efficiency, and more robust software delivery.

Docker Architecture

To understand Docker and its components better, it’s important to familiarize ourselves with the Docker architecture. The Docker architecture includes several elements like the Docker Engine, Docker objects, Docker daemon, Docker client, and Docker registries. All these components work together to build, ship, and run Docker containers.

Docker Engine

The Docker Engine is the heart of the Docker platform. It’s a server-side application with a long-running daemon process called dockerd, which creates and manages Docker objects such as images, containers, networks, and volumes.

Docker Daemon

The Docker daemon (dockerd) is a persistent process that manages Docker containers and handles container operations on the host machine. It listens for Docker API requests and processes them. The daemon can communicate with other daemons to manage Docker services.

Docker Client

The Docker client, invoked via the docker command, is the primary method 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 Images

Docker images are read-only templates with instructions for creating Docker containers. They are like the blueprints of Docker and are built from a set of instructions written in a file called a Dockerfile. Images are stored in a Docker registry such as Docker Hub or can also be stored in a user’s local Docker environment.

Docker Containers

A Docker container is a runnable instance of a Docker image. You can run, start, stop, move, or delete a container using the Docker API or CLI commands. Each container is isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine.

Docker Registries

A Docker registry is a library of Docker images. It’s where you store and distribute Docker images. Docker Hub and Docker Cloud are public registries that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can also run your own private registry.

Docker Objects

When you use Docker, you create and manage objects like images, containers, networks, and volumes. Each Docker object is designed to be a modular, composable component. You can replace, combine, or customize Docker objects according to your needs.

In the Docker architecture, the Docker client and daemon can run on the same host or different hosts, and they communicate through a REST API, over UNIX sockets, or a network interface. The client, by interacting with the daemon, provides the primary user interface for Docker.

This architecture is designed with a decoupled, modular approach. It ensures that Docker remains flexible, portable, and scalable enough to meet the needs of different applications and environments.

Installing Docker

The installation process of Docker varies depending on the operating system you’re using. For the purposes of this post, we’ll discuss how to install Docker on some of the most popular operating systems: Windows, MacOS, and Linux.

Installing Docker on Windows

Docker Desktop for Windows is the Community version of Docker for Microsoft Windows. To install Docker Desktop on Windows:

1.1 Visit the Docker Desktop for Windows download page here.

1.2 Click on “Get Docker Desktop for Windows (stable)”.

1.3 Once downloaded, run the Docker Desktop Installer.

1.4 Follow the instructions on the installation wizard to authorize the installer and proceed with the install.

1.5 When prompted, authorize Docker.app with your system password during the install process.

Installing Docker on MacOS

Docker Desktop for Mac is the Community version of Docker for MacOS. To install Docker Desktop on Mac:

2.1 Visit the Docker Desktop for Mac download page here.

2.2 Click on “Get Docker Desktop for Mac (stable)”.

2.3 Once downloaded, open the installer file.

2.4 Drag and drop the Docker.app into the Applications folder.

2.5 Open Docker.app.

Installing Docker on Linux

The process of installing Docker on Linux depends on the Linux distribution you’re using. Here’s an example of how to install Docker on Ubuntu:

3.1 Update your existing list of packages: sudo apt-get update.

3.2 Install a few prerequisite packages which let apt use packages over HTTPS: sudo apt-get install apt-transport-https ca-certificates curl software-properties-common.

3.3 Add the GPG key for the official Docker repository to your system: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -.

3.4 Add the Docker repository to APT sources: sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable".

3.5 Update the package database with the Docker packages from the newly added repo: sudo apt-get update.

3.6 Finally, install Docker: sudo apt-get install docker-ce.

Verifying the Installation

Regardless of the installation method used, you can verify that Docker has been installed correctly by running the Hello World Docker container.

Open your terminal and run the following command:

docker run hello-world

The command does two things. If you don’t have the hello-world image on your machine, Docker will download it from Docker Hub, which is the default Docker registry. Once the image is downloaded, Docker will create a new container from that image and run the container.

If Docker is installed correctly, you should see a message indicating that your installation appears to be working correctly, along with some additional information. Here’s an example of such a message:

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

If you see this message, it means Docker has been installed successfully and is functioning as expected! If not, there will be error messages that can guide you in troubleshooting what went wrong during the installation process.

Basic Docker Commands

Once Docker has been successfully installed, you will have access to the Docker command-line interface (CLI). This allows you to interact with your Docker installation using commands. Here are some of the most common Docker commands you’ll need to manage your Docker environment:

Docker Command Structure

All Docker commands follow this structure: docker [option] [command] [arguments].

Here’s a brief explanation of each component:

    • option: These are optional and modify the behavior of the Docker command.
    • command: The Docker operation that you want to perform.
    • arguments: Additional parameters that you pass to the Docker command and/or option.

Common Docker Commands

    • docker run [image]: This command is used to create a new Docker container and start it. The [image] is the name of the Docker image that the container will be based on. If the image is not found locally, Docker will attempt to pull it from the Docker Hub.
    • docker ps: Shows you all the Docker containers that are currently running. If you add the -a option, Docker will also show you the containers that are currently stopped.
    • docker stop [container_id]: This command stops a running Docker container. You need to specify the ID of the container you want to stop.
    • docker rm [container_id]: This command deletes a Docker container. You need to specify the ID of the container you want to delete.
    • docker images: Lists all Docker images that are currently stored in your Docker installation.
    • docker pull [image]: This command is used to download Docker images from the Docker Hub.
    • docker rmi [image_id]: Deletes a Docker image. You need to specify the ID of the image you want to delete.
    • docker exec -it [container_id] [command]: Executes a command in a running Docker container. This is often used to get a shell inside the container, by specifying /bin/bash or /bin/sh as the command.

Creating and Managing Dockerfiles

Dockerfiles are scripts that contain a collection of Docker commands to automate the creation of Docker images. Here’s how you can use Docker commands to build and manage Dockerfiles:

    • docker build -t [tag] .: This command builds a Docker image from a Dockerfile. The -t option allows you to tag the image with a name. The . tells Docker to look for the Dockerfile in the current directory.
    • docker history [image]: This command shows you all the commands that were run in the Dockerfile to create an image.

Remember, practice is key when learning Docker commands. Over time, you’ll find yourself becoming more comfortable with these commands, allowing you to manage your Docker environment effectively.

Exercise & Labs

Exercise 1: Installing Docker

Practice installing Docker on your machine. The installation process will vary depending on whether you’re using Windows, MacOS, or a distribution of Linux. Make sure to verify the installation by running the hello-world Docker container.

Exercise 2: Docker Images and Containers

    • Use the docker pull command to download the official Ubuntu image from Docker Hub.
    • Once the image is downloaded, use the docker images command to list the Docker images stored on your system. The Ubuntu image should be listed.
    • Run a container from the Ubuntu image using the docker run -it ubuntu bash command. This will give you interactive shell access to the container.

Exercise 3: Managing Docker Containers

    • Use the docker ps command to list the running containers. Note the container ID of the Ubuntu container.
    • Exit the shell of the Ubuntu container. Use the docker ps -a command to show all containers, including those that have been stopped.
    • Start the stopped Ubuntu container using the docker start [container_id] command. Replace [container_id] with the actual ID of your Ubuntu container.
    • Use the docker stop [container_id] command to stop the Ubuntu container. Replace [container_id] with the actual ID of your Ubuntu container.
    • Finally, use the docker rm [container_id] command to remove the Ubuntu container. Replace [container_id] with the actual ID of your Ubuntu container.

Exercise 4: Building a Docker Image

    • Create a simple Dockerfile that uses the Ubuntu image as a base and installs the curl package. The Dockerfile should look like this:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl
    • Build a Docker image from the Dockerfile using the docker build -t my-ubuntu . command.
    • Run a container from the newly created image using the docker run -it my-ubuntu bash command. Verify that curl is installed by running the curl --version command inside the container.

Exercise 5: Research Exercise

Visit Docker Hub and explore some of the popular official Docker images (like nginx, postgres, node, etc.). Pull at least one of them to your local Docker environment and run a container from it.

These exercises should give students a hands-on understanding of the fundamental Docker operations discussed in Chapter 1. Remember, the best way to learn Docker is by doing, so don’t be afraid to experiment with different commands and options!

Docker Tutorial

UP NEXT

Working with Docker Containers