更新於 2023/04/18閱讀時間約 5 分鐘

CentOS 安裝docker 和 docker-compose

簡介

  • 因常會在新機器跑container,藉此紀錄安裝過程,順便寫下腳本安裝懶人包

安裝docker

檢查系統中是否已經安裝了docker
執行:ps -ef |grep docker
如上圖所示,說明系統中並沒有安裝docker

添加docker-ce倉庫
安裝yum倉庫管理工具
sudo yum update -y sudo yum install -y yum-utils
配置docker yum源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

安裝docker engine
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

啟動docker && 設置開機啟動docker
sudo systemctl start docker.service
sudo systemctl enable docker.service

安裝docker-compose

透過curl下載docker-compose
COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep 'tag_name' | cut -d\" -f4)

sh -c "curl -L https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose"

將docker-compose變成可執行檔
chmod +x /usr/local/bin/docker-compose

建立連結
ln -s /usr/local/bin/docker-compose docker-compose

確認是否安裝成功
docker-compose -v

腳本自動化安裝docker && docker-compose

#!/bin/bash

# ----- Install docker ----- #

echo "install docker"

sudo yum update -y sudo yum install -y yum-utils

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

# start & enable docker.service

sudo systemctl start docker.service
sudo systemctl enable docker.service

# ----- Install docker-compose ----- #

echo "install docker-compose"

# get latest docker compose released tag

COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep 'tag_name' | cut -d\" -f4)

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

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

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

# Output compose version

docker-compose --version

exit 0
分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.