How to install Docker on a system?

DockerDockerBeginner
Practice Now

Introduction

Docker is a powerful containerization platform that has revolutionized the way developers build, deploy, and manage applications. In this tutorial, you will learn how to install Docker on your system and get started with running your first Docker container.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/SystemManagementGroup(["`System Management`"]) docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/run("`Run a Container`") docker/ContainerOperationsGroup -.-> docker/start("`Start Container`") docker/ContainerOperationsGroup -.-> docker/stop("`Stop Container`") docker/SystemManagementGroup -.-> docker/info("`Display System-Wide Information`") docker/SystemManagementGroup -.-> docker/version("`Show Docker Version`") docker/ContainerOperationsGroup -.-> docker/ls("`List Containers`") subgraph Lab Skills docker/ps -.-> lab-411559{{"`How to install Docker on a system?`"}} docker/run -.-> lab-411559{{"`How to install Docker on a system?`"}} docker/start -.-> lab-411559{{"`How to install Docker on a system?`"}} docker/stop -.-> lab-411559{{"`How to install Docker on a system?`"}} docker/info -.-> lab-411559{{"`How to install Docker on a system?`"}} docker/version -.-> lab-411559{{"`How to install Docker on a system?`"}} docker/ls -.-> lab-411559{{"`How to install Docker on a system?`"}} end

Understanding Docker

Docker is a popular open-source platform that enables developers to build, deploy, and run applications in a containerized environment. Containers are lightweight, standalone, and executable software packages that include everything needed to run an application, including the code, runtime, system tools, and libraries.

What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Benefits of Using Docker

  1. Consistency: Docker containers ensure that applications run the same way, regardless of the underlying infrastructure.
  2. Scalability: Docker makes it easy to scale applications up or down as needed, improving resource utilization and reducing costs.
  3. Portability: Docker containers can run on any system that has Docker installed, making it easy to move applications between different environments.
  4. Efficiency: Docker containers are lightweight and use fewer resources than virtual machines, improving overall system performance.

Docker Architecture

Docker uses a client-server architecture, where the Docker client communicates with the Docker daemon, which is responsible for building, running, and distributing Docker containers.

graph LD client[Docker Client] daemon[Docker Daemon] registry[Docker Registry] client -- API --> daemon daemon -- Pull/Push --> registry

Docker Components

  1. Docker Images: Docker images are the blueprints for creating Docker containers. They include the application code, dependencies, and any other necessary files.
  2. Docker Containers: Docker containers are the running instances of Docker images. They are lightweight, portable, and self-contained.
  3. Docker Registry: Docker Registry is a storage and distribution system for Docker images. The most popular registry is Docker Hub, which hosts a vast collection of public images.

Getting Started with Docker

To get started with Docker, you'll need to install the Docker engine on your system. The installation process varies depending on your operating system. In the next section, we'll cover how to install Docker on a Linux system.

Installing Docker on Your System

In this section, we'll guide you through the process of installing Docker on an Ubuntu 22.04 system.

Prerequisites

Before you begin, make sure your system meets the following requirements:

  • Ubuntu 22.04 operating system
  • Root or sudo privileges to install and manage Docker

Step 1: Update the Package Index

Start by updating the package index on your system:

sudo apt-get update

Step 2: Install Docker

Install the necessary packages to allow apt to use a repository over HTTPS:

sudo apt-get install \
  ca-certificates \
  curl \
  gnupg \
  lsb-release

Add the official Docker GPG key:

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Set up the Docker repository:

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install the Docker Engine, containerd, and Docker Compose packages:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Step 3: Verify the Installation

Verify that Docker is installed correctly by running the following command:

sudo docker run hello-world

This command will download a test image and run it in a container, verifying that your Docker installation is working as expected.

Congratulations! You have successfully installed Docker on your Ubuntu 22.04 system. In the next section, we'll explore how to run your first Docker container.

Running Your First Docker Container

Now that you have Docker installed on your system, let's explore how to run your first Docker container.

Running a Container

To run a Docker container, you'll need to use the docker run command. This command will pull the specified image from the Docker registry (if it's not already present on your system) and start a new container based on that image.

Let's start by running a simple "hello world" container:

docker run hello-world

This command will download the hello-world image from the Docker Hub registry and run a container based on that image. The container will display a "Hello from Docker!" message and then exit.

Interacting with Containers

You can interact with running containers using various Docker commands:

  • docker ps: List all running containers
  • docker stop <container_id>: Stop a running container
  • docker start <container_id>: Start a stopped container
  • docker exec -it <container_id> /bin/bash: Open a shell inside a running container

For example, to open a shell inside a running container, you can use the following command:

docker exec -it < container_id > /bin/bash

This will give you a shell prompt inside the container, allowing you to explore and interact with the container's file system and running processes.

Building and Running Custom Images

In addition to running pre-built images, you can also create your own custom Docker images. To do this, you'll need to create a Dockerfile, which is a text file that contains instructions for building the image.

Here's a simple example of a Dockerfile:

FROM ubuntu:22.04
RUN apt-get update && apt-get install -y nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

This Dockerfile will create a new image based on the Ubuntu 22.04 base image, install the Nginx web server, expose port 80, and start the Nginx service when the container is run.

To build the image, you can use the docker build command:

docker build -t my-nginx-app .

This will create a new image with the tag my-nginx-app.

To run the container based on this image, you can use the docker run command:

docker run -d -p 80:80 my-nginx-app

This will start a new container, map port 80 on the host to port 80 in the container, and run the Nginx web server.

Congratulations! You have now learned how to run your first Docker container and interact with it. In the next steps, you can explore more advanced Docker concepts, such as creating your own custom images, managing container networks, and deploying multi-container applications.

Summary

By following this step-by-step guide, you will gain a solid understanding of Docker and its installation process. You will be able to set up Docker on your system, run your first container, and explore the benefits of containerization. This tutorial is a valuable resource for developers, system administrators, and anyone interested in leveraging the power of Docker.

Other Docker Tutorials you may like