# Installing Docker, Docker Compose, and Portainer on Debian
In this article, we will walk through a bash script that automates the process of installing Docker, Docker Compose, and Portainer on a Debian-based system. Docker is a popular platform for developing, shipping, and running applications using containerization. Docker Compose is a tool for defining and running multi-container Docker applications. Portainer is a lightweight management UI for Docker environments.
Let's dive into the script:
#!/bin/bash
# Function to uninstall old Docker version if found
uninstall_old_docker() {
sudo apt-get purge -y docker-ce docker-ce-cli containerd.io
sudo rm -rf /etc/apt/sources.list.d/docker.list
}
# Check if curl is installed
if ! [ -x "$(command -v curl)" ]; then
echo "Installing curl..."
sudo apt-get update
sudo apt-get install -y curl
fi
# Check if Docker is already installed
if [ -x "$(command -v docker)" ]; then
echo "Old Docker installation found. Uninstalling..."
uninstall_old_docker
echo "Old Docker installation removed."
fi
# Install Docker dependencies
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates gnupg-agent software-properties-common
# Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/debian/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/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker Engine
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
# Install Docker Compose
if ! [ -x "$(command -v docker-compose)" ]; then
echo "Installing Docker Compose..."
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
else
echo "Docker Compose is already installed."
fi
# Install Portainer
docker volume create portainer_data
docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ee:latest
Steps Explained:
-
Uninstall Old Docker (if present): This script first checks if an older version of Docker is installed. If found, it uninstalls it.
-
Install curl: It checks if
curl
is installed. If not, it installs it.curl
is required for downloading files from the internet. -
Install Docker Dependencies: Installs necessary dependencies for Docker.
-
Add Docker GPG Key and Repository: Adds Docker's official GPG key for package verification and adds the Docker repository to the system's package sources.
-
Install Docker Engine: Updates package lists and installs Docker Engine, Docker CLI, and Containerd.
-
Install Docker Compose: Checks if Docker Compose is installed. If not, it downloads the latest version and sets the necessary permissions.
-
Install Portainer: Creates a Docker volume for Portainer data, then runs Portainer as a Docker container with the necessary configurations.
Conclusion:
This script automates the installation process of Docker, Docker Compose, and Portainer on Debian-based systems, making it easier to set up a containerized environment for development or production use. Make sure to review and understand the script before executing it on your system.