如何快速安装Docker和Docker-Compose

  • A+
所属分类:VPS教程

Docker的使用非常简单。直接使用命令来进行安装,命令行如下:

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

或者

curl -sSL https://get.daocloud.io/docker | sh

安装完成后,启用并开机自启

systemctl enable docker
systemctl start docker

运行以下命令以下载 Docker Compose 的当前稳定版本:

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

要安装其他版本的 Compose,请替换 1.24.1。将可执行权限应用于二进制文件:

$ sudo chmod +x /usr/local/bin/docker-compose

创建软链:

$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

测试是否安装成功:

$ docker-compose --version

如果还嫌麻烦下边提供一种更简单的脚本,代码如下,保存在x.sh,然后在其目录bash ./x.sh即可。

包括Docker和Docker-Compose的安装和卸载功能,带交互。

#!/bin/bash

# Check if Docker is installed
if command -v docker &>/dev/null; then
  echo "Docker is already installed."
  docker_version=$(docker -v | awk '{print $3}')
  echo "Docker version: $docker_version"
else
  echo "Docker is not installed."
  docker_installed=false
fi

# Check if Docker Compose is installed
if command -v docker-compose &>/dev/null; then
  echo "Docker Compose is already installed."
  docker_compose_version=$(docker-compose -v | awk '{print $3}')
  echo "Docker Compose version: $docker_compose_version"
else
  echo "Docker Compose is not installed."
  docker_compose_installed=false
fi

if [[ "$docker_installed" == false || "$docker_compose_installed" == false ]]; then
  echo "What would you like to do?"
  echo "1. Install Docker"
  echo "2. Install Docker Compose"
  echo "3. Uninstall Docker"
  echo "4. Uninstall Docker Compose"
  echo "5. Install both Docker and Docker Compose"
  read -p "Enter your choice [1-5]: " choice

  case "$choice" in
    1)
      echo "Installing Docker..."
      sudo yum install -y docker
      if command -v docker &>/dev/null; then
        echo "Docker installation succeeded."
        docker_version=$(docker -v | awk '{print $3}')
        echo "Docker version: $docker_version"
      else
        echo "Docker installation failed."
      fi
      ;;
    2)
      echo "Installing Docker Compose..."
      sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
      sudo chmod +x /usr/local/bin/docker-compose
      if command -v docker-compose &>/dev/null; then
        echo "Docker Compose installation succeeded."
        docker_compose_version=$(docker-compose -v | awk '{print $3}')
        echo "Docker Compose version: $docker_compose_version"
      else
        echo "Docker Compose installation failed."
      fi
      ;;
    3)
      echo "Uninstalling Docker..."
      sudo yum remove -y docker
      if command -v docker &>/dev/null; then
        echo "Docker uninstallation failed."
      else
        echo "Docker uninstallation succeeded."
      fi
      ;;
    4)
      echo "Uninstalling Docker Compose..."
      sudo rm /usr/local/bin/docker-compose
      if command -v docker-compose &>/dev/null; then
        echo "Docker Compose uninstallation failed."
      else
        echo "Docker Compose uninstallation succeeded."

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: