Docker學習之安裝docker-compose命令(採用Python-pip命令安裝)

本機系統環境介紹

Ubuntu系統環境介紹介紹
Ubuntu版本:18.04.1
系統配置: 64位
Linux內核:5.3.0-42-generic
Docker版本: 19.03.8

Docker Compose簡介

Docker Compose:允許用戶通過一個單獨的docker-compose.yml來定義一組相關聯的應用容器,也就是可以定義和運行多個Docker容器,負責實現對Docker容器集羣的快速編排
Docker Compose有兩個重要的概念
服務(Service):一個應用容器,實際上可以包含若干運行相同鏡像的容器實例
項目(Porject):有一組關聯的應用容器組成的一個完整的業務單元,在docker-compose.yml重定義,比如定義redis集羣
重點:docker compose的默認管理對象是項目,通過子命令對項目中的一組容器進行便捷的生命週期管理
備註:Compose 支持 Linux、macOS、Windows 10 三大平臺。

使用Python-pip命令進行安裝

第一步:環境檢查

## 查看是否安裝過Python-pip
acestang@acestang:~$ sudo pip -V
sudo: pip:找不到命令
## 先查看是否安裝過docker-compose如果安裝過,先卸載
acestang@acestang:~$ sudo docker-compose version
sudo: docker-compose:找不到命令

第二步:安裝Python-pip

# 在CentOS中需要先安裝 epel-release 主要是這個軟件包會自動配置yum的軟件倉庫,我們是Ubuntu系統所以不需要,直接安裝python-pip
acestang@acestang:~$ sudo apt-get install -y python-pip
# 測試是否安裝成功
acestang@acestang:~$ sudo pip -V
# 顯示出pip的版本號,說明安裝成功
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)

第三步:安裝docker-compose

acestang@acestang:~$sudo pip install docker-compose
Collecting docker-compose
  Downloading https://files.pythonhosted.org/packages/ec/35/1dfbb8e6b2ce5d290622a49cae0a7f3cf09cdc4341380a600aee00530881/docker_compose-1.25.5-py2.py3-none-any.whl (139kB)
    100% |████████████████████████████████| 143kB 2.0kB/s 
Collecting docopt<1,>=0.6.1 (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/a2/55/8f8cab2afd404cf578136ef2cc5dfb50baa1761b68c9da1fb1e4eed343c9/docopt-0.6.2.tar.gz
Collecting six<2,>=1.3.0 (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/65/eb/1f97cb97bfc2390a276969c6fae16075da282f5058082d4cb10c6c5c1dba/six-1.14.0-py2.py3-none-any.whl
Collecting backports.ssl-match-hostname<4,>=3.5; python_version < "3.5" (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/ff/2b/8265224812912bc5b7a607c44bf7b027554e1b9775e9ee0de8032e3de4b2/backports.ssl_match_hostname-3.7.0.1.tar.gz
Collecting PyYAML<6,>=3.10 (from docker-compose)
  Downloading https://files.pythonhosted.org/packages/64/c2/b80047c7ac2478f9501676c988a5411ed5572f35d1beff9cae07d321512c/PyYAML-5.3.1.tar.gz (269kB)
    72% |███████████████████████▏        | 194kB 3.3kB/s eta 0:00:23Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 353, in run
    wb.build(autobuilding=True)
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 620, in _prepare_file
    session=self.session, hashes=hashes)
  File "/usr/lib/python2.7/dist-packages/pip/download.py", line 821, in unpack_url
# 悲催啊,安裝到這裏突然出錯,可以看到

分析產生上述問題的原因

分析上面的問題,可能是默認安裝python-compose使用的是python3而系統環境是Python2,所以安裝失敗
解決思路:把系統的默認Python2.7 升級到Python3
升級方法參考:鏈接: Linux常用命令(3)升級系統默認python2.7到python3最新版本.
升級後會出現pip命令不能使用的問題
解決pip命令不能使用參考:鏈接: Linux常用命令(4)解決升級系統默認python2.7到python3最新版本後pip命令不能使用的問題.

繼續安裝

acestang@acestang:~$ sudo pip install docker-compose
The directory '/home/acestang/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/acestang/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting docker-compose
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out. (read timeout=15)",)': /simple/docker-compose/
  Downloading https://files.pythonhosted.org/packages/ec/35/1dfbb8e6b2ce5d290622a49cae0a7f3cf09cdc4341380a600aee00530881/docker_compose-1.25.5-py2.py3-none-any.whl (139kB)
    100% |████████████████████████████████| 143kB 9.2kB/s 
Requirement already satisfied: six<2,>=1.3.0 in /usr/lib/python3/dist-packages (from docker-compose)
Collecting docopt<1,>=0.6.1 (from docker-compose)
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', ConnectionResetError(104, '連接被對方重設'))': /simple/docopt/
  Could not find a version that satisfies the requirement docopt<1,>=0.6.1 (from docker-compose) (from versions: )
No matching distribution found for docopt<1,>=0.6.1 (from docker-compose)
# 又悲催了,安裝又失敗

分析產生上述問題的原因

問題展示:
分析問題:這個問題是在安裝軟件過程中,鏈接國外的鏡像源導致下載失敗
解決這個問題可以參考鏈接: Linux常用命令(4)解決升級系統默認python2.7到python3最新版本後pip命令不能使用的問題.

解決問題繼續安裝

acestang@acestang:~$ sudo pip install -U docker-compose
The directory '/home/acestang/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/acestang/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting docker-compose
  Url '#/docker-compose/' is ignored. It is either a non-existing path or lacks a specific scheme.
  .......
Installing collected packages: docker-compose
Successfully installed docker-compose-1.25.5
# 終於安裝過程沒有報錯,進行下一步

第四步:檢查是否安裝成功

# 安裝成功
acestang@acestang:~$ docker-compose --version
docker-compose version 1.25.5, build unknown

第五步:通過pip命令卸載docker-compose

acestang@acestang:~$ pip install docker-compose

小記

小記:雖然安裝過程很艱辛,但是遇到問題不要慌,分析問題,找到問題的原因,總能夠解決的.如果你在安裝過程中遇到了問題,直接給我留言,大家一起分析問題解決!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章