Bash: Script to check docker installation

Akarsh Seggemu
3 min readFeb 29, 2024

When you are working on a project where the project is containerised using docker. You need to make sure that docker is installed on the system where the project docker container is used. In order to simplify the docker checking process you can create a script file that checks if Docker is installed and prints a message to the console indicating whether it is installed or not. If Docker is not installed, the script exits with an error status.

Script to check docker installation

The code in the script file check_docker_installation.sh:

echo "Checking docker installation"
if command -v docker &> /dev/null; then
echo "Docker installation found"
else
echo "Docker installation not found. Please install docker."
exit 1
fi

The code is a simple shell script that checks if Docker is installed on the system. Here’s a breakdown of what each part of above code does:

  • echo "Checking docker installation": This line simply prints the message "Checking docker installation" to the console.
  • if command -v docker &> /dev/null; then: This line checks if the docker command is available in the system's PATH. The command -v docker command checks if the docker command is available, and the &> /dev/null part suppresses any output (both stdout and stderr).
  • echo "Docker installation found": If the docker command is found, this line prints "Docker installation found" to the console, indicating that Docker is installed.
  • else: If the docker command is not found, the script executes the following lines.
  • echo "Docker installation not found. Please install docker.": This line prints the message "Docker installation not found. Please install docker." to the console, indicating that Docker is not installed.
  • exit 1: This line exits the script with an exit status of 1, indicating that the script encountered an error.

How to create the script check_docker_installation.sh file in your project?

In your console, execute the following commands,

$touch check_docker_installation.sh
$nano check_docker_installation.sh
$chmod +x check_docker_installation.sh
$ls -al
-rwxr-xr-x 1 akarshseggemu check_docker_installation.sh

This above commands are executed in a console. The first command creates a shell script file called “check_docker_installation.sh” and makes it executable.

Here’s a breakdown of the commands:

  • $touch check_docker_installation.sh: This creates a new file named "check_docker_installation.sh".
  • $nano check_docker_installation.sh: This opens the file in the text editor "nano" to allow for editing the script.
  • $chmod +x check_docker_installation.sh: This gives the file execute permission, allowing it to be run as a script.
  • $ls -al: This lists all the files in the directory, showing their permissions. In this case, it confirms that "check_docker_installation.sh" has execute permissions.

How to execute the the script check_docker_installation.sh file in your project?

In your console, execute the following command,

$./check_docker_installation.sh 
Checking docker installation
Docker installation found

In the above command the script file is executed, and it prints “Checking docker installation” followed by “Docker installation found”.

Here’s a breakdown of the command:

  • $./check_docker_installation.sh: This executes the script, which then prints "Checking docker installation" and "Docker installation found".

Summary

The script checks for the installation of Docker and prints the output to console if Docker is installed on the system or not.

Hope this article helped you to understand how to create and execute the script check_docker_installation.sh file in your project.

If you like my articles, please follow me on Medium and you can also support me by buying me a coffee.

--

--

Akarsh Seggemu

IT Team Lead | MSc in Computer Science from Technische Universität Berlin | Writing articles to Empower Software Engineers and IT Leaders