How to Install Docker on Ubuntu 24.04 with Shell Script

In this article, we will learn how to install Docker on a Linux server using shell scripting. Docker is a popular platform for developing, shipping, and running applications in containers. By automating the installation process with a shell script, we can streamline the setup of Docker across multiple servers or ensure consistent deployments.

Prerequisites

  • AWS Account with Ubuntu 24.04 LTS EC2 Instance.
  • Basic knowledge of Shell scripting and Docker.

Step #1:Create a file in Ubuntu

Open the terminal and use the nano command to create a new file.

nano Docker.sh
How to Install Docker on Ubuntu 24.04 with Shell Script 1

Step #2:Write Shell script to Install Docker on Linux Server

Write the script to install docker into the file.

Following is a shell script to install docker on a linux server.

#!/bin/bash

# Update package index and install required dependencies
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

# Add Docker's official 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=$(dpkg --print-architecture) 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

# Install Docker
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

# Add the current user to the 'docker' group 
sudo usermod -aG docker $USER

# Start and enable Docker service
sudo systemctl start docker
sudo systemctl enable docker

# Print Docker version
docker --version
How to Install Docker on Ubuntu 24.04 with Shell Script 2

Save the file and exit the editor. This script automates the installation process, making it easy to set up Docker on a Linux server. After running the script, Docker will be installed and ready to use, allowing you to deploy containerized applications and services.

Explanation of the script:

  1. Shebang (#!/bin/bash): This line tells the system to use the Bash shell to interpret and run the script.
  2. Update Package Index: Think of it as refreshing your app store. It checks for new versions of software and installs any required supporting tools.
  3. Add Docker’s GPG Key: It’s like getting a special seal of approval from Docker to ensure the software you’re installing is legit and safe.
  4. Add Docker Repository: This step is like telling your computer where to find the latest Docker software. It’s adding a link to the official list of places to download stuff.
  5. Install Docker: Like installing any other app, but here it’s Docker, which is software for running and managing containers.
  6. Add User to Docker Group: It’s like giving yourself special access to Docker so you don’t have to ask for permission every time you want to use it. It’s more convenient.
  7. Start and Enable Docker: This is like turning on the engine of Docker and making sure it starts up automatically whenever your computer starts. You want Docker to be ready whenever you need it.
  8. Print Docker Version: Just a friendly message to confirm that Docker is installed and tell you which version you’ve got. It’s like checking the version of an app to see if it’s up-to-date.

Step #3:Make file executable

Change the file permissions to make it executable using the chmod command.

chmod +x Docker.sh
How to Install Docker on Ubuntu 24.04 with Shell Script 3

Step #4:Run the script

Run the script by executing the following command.

./Docker.sh
How to Install Docker on Ubuntu 24.04 with Shell Script 4

Output:

How to Install Docker on Ubuntu 24.04 with Shell Script 5

as you can see it’s displaying the installed version of docker which confirms the installation of docker.

Conclusion:

In conclusion, using a shell script to install Docker on a Linux server streamlines the installation process and ensures consistency across multiple servers. By automating the installation steps outlined in the script, you can rapidly set up Docker for various purposes. The script provided simplifies the Docker installation process by handling package updates, dependencies, repository configuration, and service management. With Docker installed, you gain access to a powerful containerization platform that enhances flexibility, scalability, and efficiency in deploying and managing applications.

Related Articles:

Create Microsoft SQL Server RDS in AWS

Reference:

Bash reference manual

Prasad Hole

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap