How to Install Docker on Linux | Step-by-Step Tutorial
![How to Install Docker on Linux | Step-by-Step Tutorial 1 Graphic of engineers configuring Docker on Linux in an IOFLOOD datacenter to enhance container management](https://ioflood.com/blog/wp-content/uploads/2024/05/Graphic-of-engineers-configuring-Docker-on-Linux-in-an-IOFLOOD-datacenter-to-enhance-container-management-300x300.jpg)
Developing software at IOFLOOD is made easy when we utilize Docker to package applications and their dependencies into lightweight, portable containers. As we often receive inquiries from our customers on how to install and utilize Docker on their dedicated servers, we’ve decided to share this guide on our best practices for Docker to streamline the development, deployment, and scaling of applications in containerized environments.
In this guide, we will walk you through the process of installing Docker on Linux. We will provide you with instructions for both APT-based distributions like Debian and Ubuntu, and YUM-based distributions like CentOS and AlmaLinux. We will also delve into more advanced topics like compiling Docker from source and installing a specific version. Finally, we will guide you on how to use Docker and verify that the correct version is installed.
So, let’s dive in and start installing Docker on your Linux system!
TL;DR: How Do I Install Docker on Linux?
To install Docker on Ubuntu, you can run the command
sudo apt-get install docker-ce
. For CentOS, you would run the commandsudo yum install docker-ce
.
# For Ubuntu
sudo apt-get update
sudo apt-get install docker-ce
# For CentOS
sudo yum update
sudo yum install docker-ce
# Output:
# Docker will be installed and ready to use
This is a basic way to install Docker on Linux, but there’s much more to learn about installing Docker and using it effectively. Continue reading for more detailed information and advanced installation options.
Table of Contents
- Getting Started with Docker on Linux
- Installing Docker from Source Code
- Installing Specific Docker Versions
- Basic Docker Usage and Verification
- Alternate Docker Installation Methods
- Troubleshooting Docker Installation
- What is Docker & Containerization
- Using Docker for DevOps
- Recap: Docker Installation on Linux
Getting Started with Docker on Linux
Docker is a platform that allows you to automate the deployment, scaling, and management of applications using containerization. A Docker container, like a virtual machine, packages an application with everything it needs to run, including libraries, system tools, and code. This ensures that the application will run the same, regardless of the environment it is running in.
Installing Docker on your Linux system is the first step to leveraging this powerful tool. Let’s get started!
Installing Docker with APT
If you’re using a Debian-based distribution like Ubuntu, you’ll use the APT package manager to install Docker. First, update your package lists for upgrades and new package installations.
sudo apt-get update
# Output:
# Your system will fetch the latest list of packages from the repositories
Next, install Docker with the following command:
sudo apt-get install docker-ce
# Output:
# Docker will be installed on your system
Installing Docker with YUM
For CentOS and other Red Hat-based distributions, you’ll use the YUM package manager. As with APT, start by updating your system’s package lists.
sudo yum update
# Output:
# Your system will fetch the latest list of packages from the repositories
Then, install Docker with the following command:
sudo yum install docker-ce
# Output:
# Docker will be installed on your system
Installing Docker with DNF
Fedora and some other distributions use the DNF package manager. The process is similar to the previous examples. First, update your package lists.
sudo dnf update
# Output:
# Your system will fetch the latest list of packages from the repositories
Then, install Docker.
sudo dnf install docker-ce
# Output:
# Docker will be installed on your system
After installation, it’s a good practice to verify that Docker is installed correctly. You can do this by running the command docker --version
in your terminal. The output should display the installed version of Docker.
docker --version
# Output:
# Docker version 20.10.6, build 370c289
This confirms that Docker is installed and ready to use on your Linux system. In the next section, we’ll look at more advanced installation methods.
Installing Docker from Source Code
If you want to install Docker from source code, it gives you the flexibility to customize the installation according to your needs. Here’s how you can do it.
First, clone the Docker repository from GitHub.
git clone https://github.com/docker/docker-ce.git
# Output:
# Clones the Docker repository into a new directory called 'docker-ce'
Navigate to the cloned directory and build Docker using the make
command.
cd docker-ce
make deb
# Output:
# Builds Docker from the source code
Installing Specific Docker Versions
There may be scenarios where you need to install a specific version of Docker. This could be due to compatibility issues, or perhaps a certain version has specific features that you need.
From Source Code
To install a specific version from source, you need to checkout the specific tag representing the version in the GitHub repository.
git checkout tags/<version>
make deb
# Output:
# Builds the specified Docker version from the source code
Replace “ with the version number you want to install.
Using Package Managers
APT
If you’re using APT, you can specify the version of Docker you want to install like this:
sudo apt-get install docker-ce=<version>
# Output:
# Installs the specified Docker version
YUM
For YUM, the command would be:
sudo yum install docker-ce-<version>
# Output:
# Installs the specified Docker version
Docker Version Comparison
Different versions of Docker offer different features. Here’s a brief comparison of some Docker versions:
Version | Key Changes | Compatibility |
---|---|---|
19.03.0 | Added GPU support in Docker run | Compatible with Linux kernel 3.10 or higher |
18.09.0 | Introduced BuildKit as an opt-in feature | Compatible with Linux kernel 3.10 or higher |
17.12.0 | Introduced Kubernetes as an orchestration solution | Compatible with Linux kernel 3.10 or higher |
Basic Docker Usage and Verification
Using Docker
Once Docker is installed, you can start using it to run containers. For example, you can run a hello-world container to verify that Docker is working correctly.
docker run hello-world
# Output:
# Hello from Docker!
# This message shows that your installation appears to be working correctly.
Verifying Docker Installation
You can verify that Docker is installed and running correctly by checking its version and running the hello-world image.
docker --version
# Output:
# Docker version 20.10.6, build 370c289
This command will display the installed version of Docker, confirming that Docker is installed and ready to use on your Linux system.
Alternate Docker Installation Methods
While using package managers or compiling from source are common ways to install Docker on Linux, there are other methods that offer their own advantages. Let’s delve into some of these alternatives.
Docker Installation Using Snap
Snap is a universal package manager available on a variety of Linux distributions. It can be an easy way to install Docker, particularly on distributions where traditional package managers aren’t available or are difficult to use.
sudo snap install docker
# Output:
# Docker will be installed on your system
The advantage of using Snap is its simplicity. However, it does not allow you to choose specific Docker versions, and it may not be available on all Linux distributions.
Manual Docker Installation
In some cases, you might need to install Docker manually. This could be due to specific system requirements, or you may want to modify the installation process. Here’s how you can do it.
First, download the Docker package. You can find the link to the latest .deb or .rpm package on Docker’s GitHub page.
wget https://download.docker.com/linux/ubuntu/dists/bionic/pool/stable/amd64/docker-ce_20.10.6~3-0~ubuntu-bionic_amd64.deb
# Output:
# Docker .deb package will be downloaded
Next, install the package using dpkg or rpm, depending on your distribution.
sudo dpkg -i docker-ce_20.10.6~3-0~ubuntu-bionic_amd64.deb
# Output:
# Docker will be installed on your system
The manual installation method gives you the most control over the installation process. However, it requires more effort and technical knowledge compared to other methods.
Docker Installation Method Comparison
Here’s a quick comparison of the Docker installation methods we’ve discussed:
Method | Difficulty | Flexibility | Compatibility |
---|---|---|---|
Package Manager | Easy | Limited | High |
Source Code | Intermediate | High | High |
Snap | Easy | Limited | Medium |
Manual | Hard | High | High |
Choosing the right installation method depends on your needs and circumstances. You might prefer the simplicity of using a package manager, the flexibility of installing from source, or the control of a manual installation. Regardless of the method you choose, the important thing is that you’re able to install Docker and start containerizing your applications.
Troubleshooting Docker Installation
While installing Docker on Linux is generally straightforward, you might encounter issues depending on your specific system configuration. Let’s discuss some common problems and their solutions.
Docker Command Not Found
After installing Docker, you might find that your system does not recognize the docker
command. This could be due to Docker not being added to your system’s PATH.
echo $PATH
# Output:
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
If the output does not include /usr/bin/docker
, you need to add it. This can be done by editing the .bashrc
file in your home directory and adding the line export PATH=$PATH:/usr/bin/docker
.
Permission Denied
When running Docker commands, you might encounter a ‘Permission Denied’ error. This is usually because the Docker daemon runs as the root
user, and your user may not have the necessary permissions.
To solve this, you can add your user to the docker
group, which will grant you the necessary permissions. However, be aware that this effectively gives your user root
privileges, which can be a security risk.
sudo usermod -aG docker $USER
# Output:
# Adds your user to the docker group
Docker Service Not Starting
If the Docker service is not starting, it could be due to a variety of reasons, such as a misconfiguration or a conflict with another service. You can check the status of the Docker service with the following command:
systemctl status docker
# Output:
# ● docker.service - Docker Application Container Engine
# Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
# Active: active (running) since Tue 2021-05-18 11:23:02 PDT; 3min 13s ago
If the Docker service is not running, you can try to start it manually with sudo systemctl start docker
. If this does not solve the problem, you should check the Docker service logs for any error messages that might indicate what the issue is.
Remember, troubleshooting is a normal part of working with any technology. Don’t get discouraged if you encounter issues. With patience and persistence, you’ll be able to get Docker up and running on your Linux system.
What is Docker & Containerization
Before we delve deeper into the intricacies of Docker, it’s important to understand the core concepts underlying this powerful tool. At its core, Docker is all about containerization.
What is Containerization?
Containerization is a type of virtualization strategy that creates isolated environments, known as containers, where you can run your applications. Unlike traditional virtualization, which emulates an entire operating system for each application, containers share the host system’s OS kernel, making them lightweight and fast.
# Running a containerized application
docker run my-application
# Output:
# Your application runs isolated from the rest of the system
In the example above, my-application
runs in its own container, isolated from the rest of the system. This ensures that any changes it makes or dependencies it requires do not affect other applications.
The Role of Docker
Docker is a platform that simplifies the process of managing containers. It provides a standardized way to package applications with their dependencies into a single object known as a Docker image. This image can be distributed and run on any system that has Docker installed, ensuring consistency across different environments.
# Building a Docker image
docker build -t my-application .
# Output:
# Builds a Docker image for your application
The docker build
command creates a Docker image for your application based on a Dockerfile
, which is a text file that describes the environment your application needs to run.
Docker in Application Development and Deployment
Docker has become an essential tool in modern application development and deployment. By containerizing applications, developers can ensure that their applications will run the same way regardless of where they are deployed. This eliminates the ‘it works on my machine’ problem and makes the deployment process much smoother.
Furthermore, Docker images are lightweight and start quickly, making them ideal for scalable applications. They can easily be managed by orchestration tools like Kubernetes, allowing you to automatically scale your application based on demand.
In conclusion, Docker is more than just a tool for installing and running applications. It’s a complete platform for developing, shipping, and running applications, making it an essential part of any developer’s toolkit.
Using Docker for DevOps
Docker’s influence extends beyond containerization and into larger software development paradigms like DevOps and microservices. Understanding these connections can help you leverage Docker’s full potential.
Docker in DevOps
DevOps is a philosophy that bridges the gap between development (Dev) and operations (Ops) through principles of continuous integration and deployment, automation, and collaboration. Docker fits perfectly into this philosophy.
Docker’s containerization approach allows developers to work in replicated production environments, reducing the ‘it works on my machine’ syndrome. Moreover, Docker containers can be incorporated into CI/CD pipelines, allowing for automated testing and deployment.
Docker and Microservices
Microservices is a software development architecture where an application is structured as a collection of loosely coupled services. Docker’s containerization is a natural fit for this architecture.
Each microservice can be packaged into a Docker container, ensuring it runs in a consistent environment. This simplifies deployment and scaling, as each microservice can be deployed and scaled independently.
Exploring Docker Compose and Kubernetes
If you’re interested in Docker, you might also want to explore Docker Compose and Kubernetes. Docker Compose is a tool for defining and running multi-container Docker applications, while Kubernetes is a container orchestration platform for automating deployment, scaling, and management of containerized applications.
# Using Docker Compose to run a multi-container application
docker-compose up
# Output:
# Your multi-container application is started
In the example above, docker-compose up
starts all the services defined in a docker-compose.yml
file. This is just a glimpse of what Docker Compose and Kubernetes can do.
Further Resources for Docker Proficiency
To deepen your understanding of Docker and its related concepts, you can refer to the following resources:
- Docker’s official documentation: A comprehensive resource with detailed explanations and tutorials on Docker.
Kubernetes’ official documentation: A great place to start learning about Kubernetes, a container orchestration platform that works well with Docker.
The New Stack: An online platform offering a wealth of articles and tutorials on Docker, Kubernetes, and other cloud-native technologies.
Recap: Docker Installation on Linux
In this comprehensive guide, we’ve navigated the process of installing Docker on Linux, a critical step towards containerizing applications. We’ve covered the basics of Docker, its role in DevOps and microservices, and the importance of containerization in modern software development.
We began with the essentials, demonstrating how to install Docker using package managers like APT, YUM, and DNF. We then ventured into advanced territory, exploring how to install Docker from source code and how to install specific Docker versions. We also discussed alternative installation methods, such as using Snap and manual installation, each with its own advantages and considerations.
Along the way, we tackled common challenges you might face during Docker installation, such as ‘Docker Command Not Found’, ‘Permission Denied’, and ‘Docker Service Not Starting’, providing solutions for each issue. We also compared the different Docker installation methods, offering insights to help you choose the right method for your needs.
Method | Difficulty | Flexibility | Compatibility |
---|---|---|---|
Package Manager | Easy | Limited | High |
Source Code | Intermediate | High | High |
Snap | Easy | Limited | Medium |
Manual | Hard | High | High |
Whether you’re just starting out with Docker or looking to enhance your skills, we hope this guide has deepened your understanding of Docker installation on Linux and its surrounding concepts.
With Docker installed on your Linux system, you’re now ready to start containerizing applications. Remember, Docker is more than just a tool—it’s a platform for developing, shipping, and running applications. Happy containerizing!