Use a shell script to install Docker on Ubuntu.

Ankita LunawatAnkita Lunawat
3 min read

A shell script is a computer program designed to be run by a Unix shell, like Bash. It's a sequence of commands that can automate tasks, making your work more efficient.

Docker and shell scripting are powerful tools that can be used together to automate and streamline various tasks. By effectively combining Docker and shell scripting, you can significantly improve your development and deployment processes.

Prerequisites

  • AWS Account with EC2 Instance.

  • Basic knowledge of shell scripting and Docker is required.

Create a file in Ubuntu

command to create a new file.

vi Docker.sh

Write a shell script to install Docker on a Linux server.

Write the script to install Docker in the file.

Here 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

Save the file and exit the editor to automate installing Docker on a Linux server.

Running the script installs Docker, allowing you to deploy containerized applications and services.

Explanation of the script

  1. Shebang (#!/bin/bash): This line instructs the system to use the Bash shell to execute the script.

  2. Update Package Index: It's like refreshing your app store to check for new software versions and install necessary tools.

  3. Add Docker’s GPG Key: This step ensures the software you're installing is legitimate and safe by obtaining a special seal of approval from Docker.

  4. Add Docker Repository: This step involves directing your computer to the official source to find and download the latest Docker software.

  5. Install Docker: It's like installing any other app, but here it's Docker, a tool for running and managing containers.

  6. Add User to Docker Group: This step grants you special access to Docker, allowing you to use it without needing permission each time, making it more convenient.

  7. Start and Enable Docker: This ensures Docker is always ready by starting it now and setting it to start automatically with your computer.

  8. Print Docker Version: This is a simple message to confirm Docker is installed and to show which version you have, similar to checking if an app is up-to-date.

Make the file executable

Use the chmod command to change the file permissions and make it executable.

chmod +x docker.sh

Execute the script

Run the script with the command below.

./Docker.sh

Using a shell script to install Docker on a Linux server simplifies the installation and ensures it is consistent on multiple servers.

0
Subscribe to my newsletter

Read articles from Ankita Lunawat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ankita Lunawat
Ankita Lunawat

Hi there! I'm a passionate AWS DevOps Engineer with 2+ years of experience in building and managing scalable, reliable, and secure cloud infrastructure. I'm excited to share my knowledge and insights through this blog. Here, you'll find articles on: AWS Services: Deep dives into core AWS services like EC2, S3, Lambda, and more. DevOps Practices: Best practices for CI/CD, infrastructure as code, and automation. Security: Tips and tricks for securing your AWS environments. Serverless Computing: Building and deploying serverless applications. Troubleshooting: Common issues and solutions in AWS. I'm always eager to learn and grow, and I hope this blog can be a valuable resource for fellow DevOps enthusiasts. Feel free to connect with me on [LinkedIn/Twitter] or leave a comment below!