DEV Community

Nipu Chakraborty
Nipu Chakraborty

Posted on

Automating the installation of Docker on Linux using a shell script

Create a .sh file file by opening your terminal

touch docker-auto-install.sh

after that open the file

nano docker-auto-install.sh
Enter fullscreen mode Exit fullscreen mode

and Write this code

#!/bin/bash

echo "Updating linux...."
sudo apt-get update
echo "Installing ca-certificates curl and gnupg..."
sudo apt-get install ca-certificates curl gnupg
echo "Add Docker's official GPG key:"
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

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

echo "Update package index again to include Docker repository"
apt update

echo "Installing Docker Engine..."
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

echo "Starting and enabling Docker service"
systemctl start docker
systemctl enable docker

echo "Docker installed and started successfully."
Enter fullscreen mode Exit fullscreen mode

After complete save this file by ctrl+s
and for exit press ctrl + x

Make the script executable: chmod +x docker-auto-install.sh
Run the script with root privileges: sudo ./docker-auto-install.sh

fiverr

Top comments (0)