DEV Community

Arief Warazuhudien
Arief Warazuhudien

Posted on

Automated Installation of Docker and Rancher with Docker Compose

Introduction:

In this article, we will explore an automated installation script that installs Docker, sets up a Docker Compose configuration, and deploys the Rancher management platform. This script streamlines the process of installing these tools and allows you to quickly set up a Rancher environment for managing your Docker containers. Let's dive into the details of the installation script.

Automated Installation Script:

The following script automates the installation process:

#!/bin/bash

# Update package lists
sudo apt-get update

# Install required packages
sudo apt-get install -y ca-certificates curl gnupg

# Create directory for keyrings
sudo install -m 0755 -d /etc/apt/keyrings

# Download Docker GPG key and save it to the keyring directory
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Set appropriate permissions for the GPG key
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add Docker repository to APT sources
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

# Update package lists with the new Docker repository
sudo apt-get update

# Install Docker packages
sudo apt-get install -y docker-ce docker-ce-cli containerd.io

# Add the current user to the docker group
sudo groupadd docker
sudo usermod -aG docker $USER

# Start a new shell with the docker group membership
newgrp docker

# Create a new Docker Compose configuration file
echo "version: '3'
services:
  rancher:
    image: rancher/rancher:latest
    restart: unless-stopped
    ports:
      - 80:80
      - 443:443
    privileged: true" > docker-compose.yaml

# Start the Rancher service defined in the Docker Compose file
docker-compose up -d

# Display command history
history
Enter fullscreen mode Exit fullscreen mode

Explanation:

The installation script performs the following steps:

  1. Updates the package lists and installs the necessary packages for the installation process.
  2. Creates a directory for the Docker GPG key and downloads the key from the Docker repository.
  3. Adds the Docker repository to the APT sources.
  4. Updates the package lists to include the Docker repository.
  5. Installs Docker packages.
  6. Adds the current user to the docker group for Docker access.
  7. Starts a new shell with the docker group membership.
  8. Creates a Docker Compose configuration file named docker-compose.yaml with a Rancher service configuration.
  9. Starts the Rancher service using Docker Compose, which deploys the Rancher management platform.
  10. Displays the command history to track the executed commands.

Conclusion:

Automating the installation of Docker and Rancher using a script not only saves time but also ensures consistency in the setup process. By following the steps outlined in this article and using the provided installation script, you can easily set up a Rancher environment to manage your Docker containers. Feel free to customize the script further to meet your specific requirements and streamline your container management workflow.

Top comments (0)