1. 前言
前段時間使用 Docker 部署了 SpringBoot 以及一些關聯應用,不過都是單獨部署的各個容器;如果項目需要部署到多套環境中,則每套環境中都需要重複性的啓用容器並關聯容器,這樣的部署方式就顯得很繁瑣;於是在這種情況下,就可以使用到 Docker 三劍客之 Docker-Compose。
2. 用處
Docker-compose 實則就是把所有容器描述、端口映射、配置映射以及依賴關係等系列配置寫在了一個配置文件中,也就好似一個批處理腳本,極大的提升了部署的速度。
3. 安裝
3.1 安裝 docker
3.2 安裝 python-pip
---
查看 pip 版本
---
[root@cdy ~]# pip -V
-bash: /usr/bin/pip: No such file or directory
---
安裝軟件包
---
[root@cdy ~]# yum -y install epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution
... 省略部分 ...
Verifying : epel-release-7-11.noarch 1/1
Installed:
epel-release.noarch 0:7-11
Complete!
---
安裝 pip
---
[root@cdy ~]# yum install python-pip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package python2-pip.noarch 0:8.1.2-5.el7 will be updated
---> Package python2-pip.noarch 0:8.1.2-10.el7 will be an update
--> Finished Dependency Resolution
... 省略部分 ...
Updated:
python2-pip.noarch 0:8.1.2-10.el7
Complete!
---
升級 pip
---
[root@cdy ~]# pip install --upgrade pip
Collecting pip
Downloading http://mirrors.aliyun.com/pypi/packages/30/db/9e38760b32e3e7f40cce46dd5fb107b8c73840df38f0046d8e6514e675a1/pip-19.2.3-py2.py3-none-any.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 76.6MB/s
Installing collected packages: pip
Found existing installation: pip 8.1.2
Uninstalling pip-8.1.2:
Successfully uninstalled pip-8.1.2
Successfully installed pip-19.2.3
---
再次查看 pip 版本
---
[root@cdy ~]# pip -V
pip 19.2.3 from /usr/lib/python2.7/site-packages/pip (python 2.7)
--------- 2020.5.2 更新 ---------
---
上面的操作都是在阿里雲服務器中完成的,而最近在本地虛擬機中安裝的時候出現了超時的問題,下面來設置一下阿里雲的安裝源
---
---
在用戶主目錄下創建 .pip 目錄
---
[root@cdy ~]# mkdir ~/.pip && cd ~/.pip/
---
再新建並編輯 pip.conf 配置文件
---
[root@cdy .pip]# vim pip.conf
---
粘貼如下配置信息來設置阿里雲的安裝源
---
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
---
輪流輸入命令 esc -> shift + 冒號 -> wq -> 回車,保存退出。最後再次升級 pip
---
3.3 安裝 docker-compose
---
安裝 docker-compose
---
[root@cdy ~]# pip install docker-compose==1.24.1
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting docker-compose
... 省略部分 ...
Successfully installed asn1crypto-0.24.0 backports.ssl-match-hostname-3.7.0.1 bcrypt-3.1.7 cached-property-1.5.1 cffi-1.12.3 cryptography-2.7 docker-3.7.3 docker-compose-1.24.1 docker-pycreds-0.4.0 dockerpty-0.4.1 docopt-0.6.2 enum34-1.1.6 functools32-3.2.3.post2 ipaddress-1.0.22 jsonschema-2.6.0 paramiko-2.6.0 pycparser-2.19 pynacl-1.3.0 six-1.12.0 texttable-0.9.1 websocket-client-0.56.0
---
查看 docker-compose 版本
---
[root@cdy ~]# docker-compose --version
/usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn't match a supported version!
RequestsDependencyWarning)
docker-compose version 1.24.1, build 4667896
---
最後看起來有一些版本不兼容問題,接下來卸載不兼容模塊,重新安裝
---
[root@cdy /]# pip uninstall urllib3
Uninstalling urllib3-1.22:
/usr/lib/python2.7/site-packages/urllib3-1.22.dist-info/DESCRIPTION.rst
/usr/lib/python2.7/site-packages/urllib3-1.22.dist-info/INSTALLER
/usr/lib/python2.7/site-packages/urllib3-1.22.dist-info/METADATA
/usr/lib/python2.7/site-packages/urllib3-1.22.dist-info/RECORD
/usr/lib/python2.7/site-packages/urllib3-1.22.dist-info/WHEEL
/usr/lib/python2.7/site-packages/urllib3-1.22.dist-info/metadata.json
/usr/lib/python2.7/site-packages/urllib3-1.22.dist-info/top_level.txt
... 省略部分 ...
Proceed (y/n)? y
Successfully uninstalled urllib3-1.22
[root@cdy /]# pip uninstall chardet
Uninstalling chardet-3.0.4:
/usr/bin/chardetect
/usr/lib/python2.7/site-packages/chardet-3.0.4.dist-info/DESCRIPTION.rst
/usr/lib/python2.7/site-packages/chardet-3.0.4.dist-info/INSTALLER
/usr/lib/python2.7/site-packages/chardet-3.0.4.dist-info/METADATA
/usr/lib/python2.7/site-packages/chardet-3.0.4.dist-info/RECORD
/usr/lib/python2.7/site-packages/chardet-3.0.4.dist-info/WHEEL
/usr/lib/python2.7/site-packages/chardet-3.0.4.dist-info/entry_points.txt
/usr/lib/python2.7/site-packages/chardet-3.0.4.dist-info/metadata.json
/usr/lib/python2.7/site-packages/chardet-3.0.4.dist-info/top_level.txt
... 省略部分 ...
Proceed (y/n)? y
Successfully uninstalled chardet-3.0.4
[root@cdy /]# pip install requests
Requirement already satisfied: requests in /usr/lib/python2.7/site-packages
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Downloading http://mirrors.aliyun.com/pypi/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
100% |████████████████████████████████| 143kB 68.3MB/s
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python2.7/site-packages (from requests)
Collecting urllib3<1.23,>=1.21.1 (from requests)
Downloading http://mirrors.aliyun.com/pypi/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)
100% |████████████████████████████████| 133kB 62.6MB/s
Requirement already satisfied: idna<2.7,>=2.5 in /usr/lib/python2.7/site-packages (from requests)
Installing collected packages: chardet, urllib3
Found existing installation: chardet 2.2.1
Uninstalling chardet-2.2.1:
Successfully uninstalled chardet-2.2.1
Successfully installed chardet-3.0.4 urllib3-1.22
---
再次查看 docker-compose 版本
---
[root@cdy /]# docker-compose --version
docker-compose version 1.24.1, build 4667896
4. 常用命令
- 查看版本信息:docker-compose --version
以下命令需要在有 docker-compose.yaml 文件的目錄下執行;在寫法上與 docker 命令類似
- 啓動:docker-compose up -d
- 停止:docker-compose stop
- 停止並刪除:docker-compose down
- 查看構建的鏡像:docker-compose images
- 查看運行的容器:docker-compose ps -a
5. 擴展
希望能夠幫助到你
over