Centos7安裝docker、docker-compose實踐過程
一、卸載舊版Docker
1、查詢是否安裝過舊版
yum list installed | grep docker
沒有出現(xiàn)內容就表示沒有安裝過docker

出現(xiàn)以下內容代表安裝過docker,若需要安裝最新版則進行卸載

2、查詢Docker狀態(tài),目前是運行狀態(tài)
systemctl status docker #查詢docker狀態(tài)
當前docker正在運行需要停止后再進行卸載

systemctl stop docker #停止docker
停止docker服務后,出現(xiàn)以下警告信息,docker在關閉狀態(tài)下被訪問會觸發(fā)自動喚醒機制,下次再執(zhí)行任意的docker命令會直接啟動docker服務

再次查詢docker狀態(tài)為停止狀態(tài)

3、刪除安裝過Docker的相關包
#刪除命令如下 yum -y remove containerd.io.x86_64 \ docker-buildx-plugin.x86_64 \ docker-ce.x86_64 \ docker-ce-cli.x86_64 \ docker-ce-rootless-extras.x86_64 \ docker-compose-plugin.x86_64

#刪除docker相關軟件包 yum remove -y docker*

4、刪除Docker相關的鏡像和容器
docker相關的鏡像和容器,在 /var/lib 下

rm -rf /var/lib/docker #刪除/var/lib下的docker文件夾
運行后再次查詢已經沒有docker文件夾了

二、安裝Docker
1、安裝yum-utils工具包并設置阿里云鏡像
#安裝yum-utils工具 yum install -y yum-utils device-mapper-persistent-data lvm2

如果安裝yum-utils工具時顯示以下內容時,說明安裝失敗,國外鏡像源封鎖加強,需要更換國內鏡像源后才能安裝 點擊更改鏡像源
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的錯誤"
One of the configured repositories failed (未知),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:
1. Contact the upstream for the repository and get them to fix the problem.
2. Reconfigure the baseurl/etc. for the repository, to point to a working
upstream. This is most often useful if you are using a newer
distribution release than is supported by the repository (and the
packages for the previous distribution release still work).
3. Run the command with the repository temporarily disabled
yum --disablerepo=<repoid> ...
4. Disable the repository permanently, so yum won't use it by default. Yum
will then just ignore the repository until you permanently enable it
again or use --enablerepo for temporary usage:
yum-config-manager --disable <repoid>
or
subscription-manager repos --disable=<repoid>
5. Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
compromise:
yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
Cannot find a valid baseurl for repo: base/7/x86_64
設置阿里云鏡像
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2、查看所有Docker版本
yum list docker-ce --showduplicates | sort -r

3、安裝指定版本Docker
例如安裝26.0.2-1.el7版本

注意版本號只要"3:"后面的那部分
yum install -y docker-ce-26.0.2-1.el7 docker-ce-cli-26.0.2-1.el7 containerd.io
4、安裝最新版Docker
yum install -y docker-ce docker-ce-cli containerd.io
在命令行中 --allowerasing 參數(shù)允許替換沖突的軟件包,可以不加
yum install -y --allowerasing docker-ce docker-ce-cli containerd.io

三、Docker使用前準備
1、啟動Docker服務
systemctl start docker
2、停止Docker服務
systemctl stop docker

停止docker服務后,出現(xiàn)以上警告信息,docker在關閉狀態(tài)下被訪問會觸發(fā)自動喚醒機制,下次再執(zhí)行任意的docker命令會直接啟動docker服務,如果希望docker不會觸發(fā)訪問自動喚醒機制,停止docker服務時執(zhí)行以下命令
systemctl stop docker.socket
3、查看Docker服務狀態(tài)
systemctl status docker
以下是docker服務運行狀態(tài)

以下是docker服務停止狀態(tài)

4、設置Docker服務開機自啟動
systemctl enable docker
四、docker-compose安裝
1、下載Docker Compose二進制文件
# 下載Docker Compose二進制文件 sudo curl -L "https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose # 賦予執(zhí)行權限 sudo chmod +x /usr/local/bin/docker-compose
或者使用以下離線安裝
2、先到GitHub下載docker-compose

3、將下載好的文件上傳到Linux
將docker-compose文件上傳到/usr/local/bin下,并改名為docker-compose

給docker-compose添加可執(zhí)行權限
chmod +x /usr/local/bin/docker-compose
4、查看版本號
docker-compose version

總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Docker 拉取鏡像超時(親測 3 個有效鏡像源 + 避坑指南)
Docker 默認鏡像源在國外,國內訪問經常抽風,常規(guī)操作應該是換國內鏡像源,本文就來介紹一下三個國內穩(wěn)定源,具有一定的參考價值,感興趣的可以了解一下2025-08-08
使用Docker run的選項以覆蓋Dockerfile中的設置詳解
今天小編就為大家分享一篇關于使用Docker run的選項以覆蓋Dockerfile中的設置詳解,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
Linux執(zhí)行Docker命令提示Get Permission Denied的解決方案
這篇文章主要介紹了Linux執(zhí)行Docker命令提示Get Permission Denied的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-06-06
Docker Compose 與 Harbor 私有倉庫詳解
本文將探討Docker Compose多容器編排工具與Harbor企業(yè)級私有倉庫的核心技術,幫助讀者掌握從容器管理到鏡像安全存儲的完整解決方案,感興趣的朋友一起看看吧2025-06-06

