Install docker and docker compose | LinuxGist
This article will provide details on how we can easily install docker and docker compose in Linux.
- Introduction to Docker
- Installation on Fedora 40
- Installation on Ubuntu 24.04
- Step 1: Update System
- Step 2: Install Required Packages
- Step 3: Add Docker’s Official GPG Key
- Step 4: Set Up the Stable Repository
- Step 5: Update APT Package Index
- Step 6: Install Docker CE (Community Edition)
- Step 7: Start and Enable Docker Service
- Step 8: Verify Installation
- Additional Configuration (Optional)
- Docker Compose
- Summary
Introduction to Docker
Docker is a platform that uses operating system-level virtualization to build, share, and run container applications. Containers allow you to deploy, run, and manage applications across any environment without considering the underlying infrastructure.
Key Features of Docker
- Isolation: Each container runs as an isolated process, ensuring that your application has all the dependencies it needs and does not interfere with other processes.
- Portability: You can package an application along with its entire runtime, including libraries and settings, into a single image, which you can easily share and run on any Docker-supported platform.
- Performance: Containers are lightweight and start up quickly, thanks to the use of kernel-level virtualization (like Linux namespaces and cgroups).
- Resource Management: You can control how much CPU, memory, disk I/O, and network bandwidth each container uses.
- Scalability: Easily scale applications by running multiple containers on a single host or across multiple hosts.
Installation on Fedora 40
Docker can be installed on Fedora using the official Docker repository. Here’s a step-by-step guide to installing Docker on Fedora 40:
Step 1: Update System
First, update your system packages:
1
sudo dnf update -y
Step 2: Install Required Packages
Docker requires certain dependencies. Install these using dnf
:
1
sudo dnf install -y yum-utils device-mapper-persistent-data lvm2
Step 3: Add Docker Repository
Use the yum-config-manager
tool to add the official Docker repository:
1
sudo yum-config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
Step 4: Install Docker CE (Community Edition)
Install Docker Community Edition using dnf
:
1
sudo dnf install docker-ce docker-ce-cli containerd.io -y
Step 5: Start and Enable Docker Service
Start the Docker service and enable it to start at boot:
1
2
sudo systemctl start docker
sudo systemctl enable docker
Step 6: Verify Installation
Verify that Docker is installed correctly by running a test container:
1
sudo docker run hello-world
This command downloads a test image and runs it in a container. If everything is set up correctly, you should see a message indicating that Docker is working.
Additional Configuration (Optional)
You may want to add your user to the docker
group to avoid using sudo
for running Docker commands:
1
sudo usermod -aG docker $USER
After adding your user to the docker
group, you will need to log out and log back in for the changes to take effect.
Installation on Ubuntu 24.04
Installing Docker on Ubuntu 24.04 (also known as Jammy Jellyfish) is straightforward. Here’s a step-by-step guide to help you through the installation process:
Step 1: Update System
First, update your system packages to ensure you have the latest software versions:
1
2
sudo apt-get update
sudo apt-get upgrade -y
Step 2: Install Required Packages
Docker requires some dependencies. Install these using apt
:
1
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
Step 3: Add Docker’s Official GPG Key
Add the official Docker GPG key to ensure the downloads are valid:
1
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Step 4: Set Up the Stable Repository
Set up the stable Docker repository:
1
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 > /dev/null
Step 5: Update APT Package Index
Update the apt
package index again to include Docker packages from the newly added repository:
1
sudo apt-get update
Step 6: Install Docker CE (Community Edition)
Install Docker Community Edition using apt
:
1
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
Step 7: Start and Enable Docker Service
Start the Docker service and enable it to start at boot:
1
2
sudo systemctl start docker
sudo systemctl enable docker
Step 8: Verify Installation
Verify that Docker is installed correctly by running a test container:
1
sudo docker run hello-world
This command downloads a test image and runs it in a container. If everything is set up correctly, you should see a message indicating that Docker is working.
Additional Configuration (Optional)
You may want to add your user to the docker
group to avoid using sudo
for running Docker commands:
1
sudo usermod -aG docker $USER
After adding your user to the docker
group, you will need to log out and log back in for the changes to take effect.
By following these steps, you should have Docker installed and running on Ubuntu 24.04. You can now use Docker to containerize and run applications on your system.
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services, networks, and volumes. Then, with a single command, you create and start all the services from your configuration.
Key Features of Docker Compose
- Service Definition: Define services in a
docker-compose.yml
file, specifying images, ports, environment variables, dependencies, and more. - Multi-Container Deployment: Easily manage multiple containers that make up a complex application.
- Network Configuration: Automatically creates networks for the services to communicate with each other.
- Volume Management: Manage data persistence using Docker volumes.
- Dependency Management: Define dependencies between services so they start in the correct order.
Example of a docker-compose.yml
File
1
2
3
4
5
6
7
8
9
10
11
12
version: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
Installation
You can install Docker Compose using pip3
, the Python package installer. Here’s how you can do it:
Step 1: Install Python
Ensure that Python is installed on your system. You can check this by running:
1
python3 --version
If Python is not installed, you can install it using your package manager. For example, on Ubuntu, you can use:
1
2
sudo apt-get update
sudo apt-get install -y python3
Step 2: Install pip3
Ensure that pip3
is installed. You can check this by running:
1
pip3 --version
If pip3
is not installed, you can install it using the following command:
1
sudo apt-get install -y python3-pip
Step 3: Install Docker Compose Using pip3
Install Docker Compose using pip3
:
1
sudo pip3 install docker-compose
Step 4: Verify Installation
Verify that Docker Compose is installed correctly by running the following command:
1
docker-compose --version
You should see output similar to this, indicating the version of Docker Compose you have installed:
1
docker-compose version 1.29.2, build 5becea4c
1
sudo usermod -aG docker $USER
After adding your user to the docker
group, you will need to log out and log back in for the changes to take effect. By following these steps, you have successfully installed Docker Compose using pip3
. This allows you to manage multi-container applications easily with a single command.
How to Use Docker Compose
- Install Docker: Ensure Docker is installed on your system.
- Create a
docker-compose.yml
File: Define your application’s services in this file. - Navigate to the Directory Containing
docker-compose.yml
:1
cd /path/to/your/docker-compose/file
- Start the Application:
1
docker-compose up -d
- Stop the Application:
1
docker-compose down
Summary
Docker simplifies the deployment and management of containerized applications. Docker and Docker Compose provide a robust solution for developing, deploying, and scaling complex applications in a consistent manner across different environments.