Introduction
Docker is a powerful platform that enables developers to build, package, and distribute applications as containers. Containers are lightweight, portable, and can run consistently across various environments. In this article, we will walk you through the process of setting up Docker on an Ubuntu system using a Bash script. This script automates the installation and configuration steps for Docker and Docker Compose.
Prerequisites
Before you begin, you need:
An Ubuntu-based Linux system.
Superuser (sudo) privileges or access to a user with sudo privileges.
The Bash Script
Below is the Bash script that automates the installation and setup of Docker on your Ubuntu system. This script also installs Docker Compose, a tool for defining and running multi-container Docker applications.
#!/bin/bash
#debug mode
set -x # Enable debug mode
# Update package list
sudo apt update
# Install dependencies
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
# Add Docker GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Add Docker repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# Update package list again
sudo apt update
# Install Docker
sudo apt install -y docker-ce
# Start and enable Docker service
sudo systemctl start docker
sudo systemctl enable docker
# Add your user to the docker group (optional, to run Docker without sudo)
sudo usermod -aG docker $USER
# Install Docker Compose (optional)
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Test Docker installation
docker --version
# Test Docker Compose installation
docker-compose --version
set +x # Disable debug mode
script explains:
#!/bin/bash
: This line specifies that the script is to be run in the Bash shell.sudo apt update
: It updates the list of available packages.sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
: This command installs various dependencies required for Docker installation.curl -fsSL
https://download.docker.com/linux/ubuntu/gpg
| sudo apt-key add -
: It adds the GPG key for Docker.sudo add-apt-repository "deb [arch=amd64]
https://download.docker.com/linux/ubuntu
$(lsb_release -cs) stable"
: This line adds the Docker repository to the apt sources.sudo apt update
: The package list is updated again, this time with the Docker repository added.sudo apt install -y docker-ce
: It installs Docker on the system.sudo systemctl start docker
: Initiates the Docker service.sudo systemctl enable docker
: Ensures that the Docker service starts automatically on system boot.sudo usermod -aG docker $USER
: This command adds your user to the Docker group, allowing you to use Docker without using 'sudo' (optional step).To install Docker Compose, it downloads the Docker Compose binary and moves it to /usr/local/bin while granting it execution permissions.
The script verifies that Docker and Docker Compose are correctly installed by using the 'docker --version' and 'docker-compose --version' commands.
Executing the Bash Script
Open a terminal on your Ubuntu system.
Create a new file using a text editor and paste the script into it. Save the file with a ".sh" extension, for example, "install_docker.sh".
Make the script executable by running the following command:
sudo chmod +x install_docker.sh
Execute the script with superuser privileges:
sudo ./install_docker.sh
The script will automatically update your system, install Docker and its dependencies, and add your user to the Docker group (if you choose the optional step).
Verifying Docker Installation
To ensure that Docker and Docker Compose were installed successfully, you can run the following commands:
docker --version
docker-compose --version
You should see version information for both Docker and Docker Compose.
if u contact me :