docker-compose項目是docker官方的開源項目,負責實現對docker容器集群的快速編排;所謂編排我們可以理解為像ansible裡面的"劇本";我們把需要啟動的容器,每個容器之間的依賴關係,每個容器的網路、存儲等等都定義在一個配置文件中,然後通過一個工具去讀取這個配置文件,從而實現快... ...
前文我們聊了下docker私有倉庫harbor的搭建,回顧請參考https://www.cnblogs.com/qiuhom-1874/p/13061984.html;在上一篇博客的末尾,我們簡單聊了下docker-compose啟動和停止harbor;docker-compose項目是docker官方的開源項目,負責實現對docker容器集群的快速編排;所謂編排我們可以理解為像ansible裡面的"劇本";我們把需要啟動的容器,每個容器之間的依賴關係,每個容器的網路、存儲等等都定義在一個配置文件中,然後通過一個工具去讀取這個配置文件,從而實現快速部署一個應用,方便管理的一個堆docker容器;對於docker-compose來講,這個配置文件必須交docker-compose.yml,並且通過docker-compose來管理docker容器,它預設就會去找這個配置文件,所以在執行docker-compose命令時,通常我們都是在docker-compose.yml所在目錄下執行;
首先說下安裝吧,通常安裝方式有兩種,第一種方式就是把docker-compose當作python的一個應用來裝,用pip安裝docker-compose即可;第二種就是yum安裝;如下所示
1、pip安裝
[root@docker_node02 ~]# pip install docker-compose DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support Collecting docker-compose Downloading docker_compose-1.26.0-py2.py3-none-any.whl (139 kB) |████████████████████████████████| 139 kB 61 kB/s Collecting backports.shutil-get-terminal-size==1.0.0; python_version < "3.3" Downloading backports.shutil_get_terminal_size-1.0.0-py2.py3-none-any.whl (6.5 kB) Collecting python-dotenv<1,>=0.13.0 Downloading python_dotenv-0.13.0-py2.py3-none-any.whl (17 kB) Collecting distro<2,>=1.5.0 Downloading distro-1.5.0-py2.py3-none-any.whl (18 kB) Collecting texttable<2,>=0.9.0 Downloading texttable-1.6.2-py2.py3-none-any.whl (10 kB) Collecting six<2,>=1.3.0 Downloading six-1.15.0-py2.py3-none-any.whl (10 kB) Collecting cached-property<2,>=1.2.0 Downloading cached_property-1.5.1-py2.py3-none-any.whl (6.0 kB) Collecting jsonschema<4,>=2.5.1 Downloading jsonschema-3.2.0-py2.py3-none-any.whl (56 kB) |████████████████████████████████| 56 kB 18 kB/s Requirement already satisfied: backports.ssl-match-hostname<4,>=3.5; python_version < "3.5" in /usr/lib/python2.7/site-packages (from docker-compose) (3.5.0.1) Requirement already satisfied: ipaddress<2,>=1.0.16; python_version < "3.3" in /usr/lib/python2.7/site-packages (from docker-compose) (1.0.16) Collecting subprocess32<4,>=3.5.4; python_version < "3.2" Downloading subprocess32-3.5.4.tar.gz (97 kB) |████████████████████████████████| 97 kB 14 kB/s Collecting docopt<1,>=0.6.1 Downloading docopt-0.6.2.tar.gz (25 kB) Collecting dockerpty<1,>=0.4.1 Downloading dockerpty-0.4.1.tar.gz (13 kB) Collecting requests<3,>=2.20.0 Downloading requests-2.23.0-py2.py3-none-any.whl (58 kB) |████████████████████████████████| 58 kB 17 kB/s Requirement already satisfied: PyYAML<6,>=3.10 in /usr/lib64/python2.7/site-packages (from docker-compose) (5.3.1) Collecting docker[ssh]<5,>=3.7.0 Downloading docker-4.2.1-py2.py3-none-any.whl (143 kB) |████████████████████████████████| 143 kB 15 kB/s Requirement already satisfied: enum34<2,>=1.0.4; python_version < "3.4" in /usr/lib/python2.7/site-packages (from docker-compose) (1.0.4) Collecting websocket-client<1,>=0.32.0 Downloading websocket_client-0.57.0-py2.py3-none-any.whl (200 kB) |████████████████████████████████| 200 kB 13 kB/s Collecting typing; python_version < "3.5" Downloading typing-3.7.4.1-py2-none-any.whl (26 kB) Collecting functools32; python_version < "3" Downloading functools32-3.2.3-2.tar.gz (31 kB) Collecting attrs>=17.4.0 Downloading attrs-19.3.0-py2.py3-none-any.whl (39 kB) Requirement already satisfied: setuptools in /usr/lib/python2.7/site-packages (from jsonschema<4,>=2.5.1->docker-compose) (0.9.8) Collecting pyrsistent>=0.14.0 Downloading pyrsistent-0.16.0.tar.gz (108 kB) |████████████████████████████████| 108 kB 14 kB/s Collecting importlib-metadata; python_version < "3.8" Downloading importlib_metadata-1.6.1-py2.py3-none-any.whl (31 kB) Collecting certifi>=2017.4.17 Downloading certifi-2020.4.5.2-py2.py3-none-any.whl (157 kB) |████████████████████████████████| 157 kB 19 kB/s Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 Downloading urllib3-1.25.9-py2.py3-none-any.whl (126 kB) |████████████████████████████████| 126 kB 13 kB/s Collecting idna<3,>=2.5 Downloading idna-2.9-py2.py3-none-any.whl (58 kB) |████████████████████████████████| 58 kB 14 kB/s Collecting chardet<4,>=3.0.2 Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB) |████████████████████████████████| 133 kB 22 kB/s Collecting paramiko>=2.4.2; extra == "ssh" Downloading paramiko-2.7.1-py2.py3-none-any.whl (206 kB) |████████████████████████████████| 206 kB 22 kB/s Collecting contextlib2; python_version < "3" Downloading contextlib2-0.6.0.post1-py2.py3-none-any.whl (9.8 kB) Collecting pathlib2; python_version < "3" Downloading pathlib2-2.3.5-py2.py3-none-any.whl (18 kB) Collecting zipp>=0.5 Downloading zipp-1.2.0-py2.py3-none-any.whl (4.8 kB) Collecting configparser>=3.5; python_version < "3" Downloading configparser-4.0.2-py2.py3-none-any.whl (22 kB) Collecting cryptography>=2.5 Downloading cryptography-2.9.2-cp27-cp27mu-manylinux2010_x86_64.whl (2.7 MB) |████████████████████████████████| 2.7 MB 15 kB/s Collecting pynacl>=1.0.1 Downloading PyNaCl-1.4.0-cp27-cp27mu-manylinux1_x86_64.whl (964 kB) |████████████████████████████████| 964 kB 27 kB/s Collecting bcrypt>=3.1.3 Downloading bcrypt-3.1.7-cp27-cp27mu-manylinux1_x86_64.whl (59 kB) |████████████████████████████████| 59 kB 15 kB/s Collecting scandir; python_version < "3.5" Downloading scandir-1.10.0.tar.gz (33 kB) Collecting cffi!=1.11.3,>=1.8 Downloading cffi-1.14.0-cp27-cp27mu-manylinux1_x86_64.whl (387 kB) |████████████████████████████████| 387 kB 19 kB/s Collecting pycparser Downloading pycparser-2.20-py2.py3-none-any.whl (112 kB) |████████████████████████████████| 112 kB 17 kB/s Using legacy setup.py install for subprocess32, since package 'wheel' is not installed. Using legacy setup.py install for docopt, since package 'wheel' is not installed. Using legacy setup.py install for dockerpty, since package 'wheel' is not installed. Using legacy setup.py install for functools32, since package 'wheel' is not installed. Using legacy setup.py install for pyrsistent, since package 'wheel' is not installed. Using legacy setup.py install for scandir, since package 'wheel' is not installed. Installing collected packages: backports.shutil-get-terminal-size, typing, python-dotenv, distro, texttable, six, cached-property, functools32, attrs, pyrsistent, contextlib2, scandir, pathlib2, zipp, configparser, importlib-metadata, jsonschema, subprocess32, docopt, dockerpty, certifi, urllib3, idna, chardet, requests, websocket-client, pycparser, cffi, cryptography, pynacl, bcrypt, paramiko, docker, docker-compose Running setup.py install for functools32 ... done Running setup.py install for pyrsistent ... done Running setup.py install for scandir ... done Running setup.py install for subprocess32 ... error ERROR: Command errored out with exit status 1: command: /usr/bin/python2 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kRVwx0/subprocess32/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kRVwx0/subprocess32/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-qwi0Cp/install-record.txt --single-version-externally-managed --compile --install-headers /usr/include/python2.7/subprocess32 cwd: /tmp/pip-install-kRVwx0/subprocess32/ Complete output (51 lines): /usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires' warnings.warn(msg) running install running build running build_py creating build creating build/lib.linux-x86_64-2.7 copying subprocess32.py -> build/lib.linux-x86_64-2.7 running build_ext running build_configure checking for gcc... no checking for cc... no checking for cl.exe... no configure: error: in `/tmp/pip-install-kRVwx0/subprocess32': configure: error: no acceptable C compiler found in $PATH See `config.log' for more details Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-install-kRVwx0/subprocess32/setup.py", line 120, in <module> main() File "/tmp/pip-install-kRVwx0/subprocess32/setup.py", line 114, in main 'Programming Language :: Python :: Implementation :: CPython', File "/usr/lib64/python2.7/distutils/core.py", line 152, in setup dist.run_commands() File "/usr/lib64/python2.7/distutils/dist.py", line 953, in run_commands self.run_command(cmd) File "/usr/lib64/python2.7/distutils/dist.py", line 972, in run_command cmd_obj.run() File "/usr/lib/python2.7/site-packages/setuptools/command/install.py", line 53, in run return _install.run(self) File "/usr/lib64/python2.7/distutils/command/install.py", line 563, in run self.run_command('build') File "/usr/lib64/python2.7/distutils/cmd.py", line 326, in run_command self.distribution.run_command(command) File "/usr/lib64/python2.7/distutils/dist.py", line 972, in run_command cmd_obj.run() File "/usr/lib64/python2.7/distutils/command/build.py", line 127, in run self.run_command(cmd_name) File "/usr/lib64/python2.7/distutils/cmd.py", line 326, in run_command self.distribution.run_command(command) File "/usr/lib64/python2.7/distutils/dist.py", line 972, in run_command cmd_obj.run() File "/tmp/pip-install-kRVwx0/subprocess32/setup.py", line 41, in run self.run_command(command) File "/usr/lib64/python2.7/distutils/cmd.py", line 326, in run_command self.distribution.run_command(command) File "/usr/lib64/python2.7/distutils/dist.py", line 972, in run_command cmd_obj.run() File "/tmp/pip-install-kRVwx0/subprocess32/setup.py", line 26, in run raise RuntimeError(configure_command + ' failed.') RuntimeError: sh ./configure failed. ---------------------------------------- ERROR: Command errored out with exit status 1: /usr/bin/python2 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kRVwx0/subprocess32/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kRVwx0/subprocess32/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-qwi0Cp/install-record.txt --single-version-externally-managed --compile --install-headers /usr/include/python2.7/subprocess32 Check the logs for full command output. [root@docker_node02 ~]#
提示:安裝如果報以上錯誤,解決辦法yum install gcc libffi-devel python-devel openssl-devel -y;docker-compose是python語言研發的,如果你的主機上沒有pip 可以先安裝python-pip即可;安裝python-pip註意先配置好epel源;
[root@docker_node02 ~]# yum install gcc libffi-devel python-devel openssl-devel -y Loaded plugins: fastestmirror base | 3.6 kB 00:00:00 docker-ce-stable | 3.5 kB 00:00:00 epel | 4.7 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/3): updates/7/x86_64/primary_db | 2.1 MB 00:00:00 (2/3): epel/x86_64/updateinfo | 1.0 MB 00:00:03 (3/3): epel/x86_64/primary_db | 6.8 MB 00:00:05 Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package gcc.x86_64 0:4.8.5-39.el7 will be installed --> Processing Dependency: libgomp = 4.8.5-39.el7 for package: gcc-4.8.5-39.el7.x86_64 --> Processing Dependency: cpp = 4.8.5-39.el7 for package: gcc-4.8.5-39.el7.x86_64 ……省略部分內容 Installed: gcc.x86_64 0:4.8.5-39.el7 libffi-devel.x86_64 0:3.0.13-19.el7 openssl-devel.x86_64 1:1.0.2k-19.el7 python-devel.x86_64 0:2.7.5-88.el7 Dependency Installed: cpp.x86_64 0:4.8.5-39.el7 glibc-devel.x86_64 0:2.17-307.el7.1 glibc-headers.x86_64 0:2.17-307.el7.1 kernel-headers.x86_64 0:3.10.0-1127.10.1.el7 keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.15.1-46.el7 libcom_err-devel.x86_64 0:1.42.9-17.el7 libkadm5.x86_64 0:1.15.1-46.el7 libmpc.x86_64 0:1.0.1-3.el7 libselinux-devel.x86_64 0:2.5-15.el7 libsepol-devel.x86_64 0:2.5-10.el7 libverto-devel.x86_64 0:0.2.5-4.el7 mpfr.x86_64 0:3.1.1-4.el7 pcre-devel.x86_64 0:8.32-17.el7 python-rpm-macros.noarch 0:3-32.el7 python-srpm-macros.noarch 0:3-32.el7 python2-rpm-macros.noarch 0:3-32.el7 zlib-devel.x86_64 0:1.2.7-18.el7 Dependency Updated: e2fsprogs.x86_64 0:1.42.9-17.el7 e2fsprogs-libs.x86_64 0:1.42.9-17.el7 glibc.x86_64 0:2.17-307.el7.1 glibc-common.x86_64 0:2.17-307.el7.1 krb5-libs.x86_64 0:1.15.1-46.el7 libcom_err.x86_64 0:1.42.9-17.el7 libffi.x86_64 0:3.0.13-19.el7 libgcc.x86_64 0:4.8.5-39.el7 libgomp.x86_64 0:4.8.5-39.el7 libss.x86_64 0:1.42.9-17.el7 openssl.x86_64 1:1.0.2k-19.el7 openssl-libs.x86_64 1:1.0.2k-19.el7 python.x86_64 0:2.7.5-88.el7 python-libs.x86_64 0:2.7.5-88.el7 zlib.x86_64 0:1.2.7-18.el7 Complete! [root@docker_node02 ~]#
提示:安裝好以上依賴包以後,在來安裝docker-compose 就沒有問題了;
[root@docker_node02 ~]# pip install docker-compose DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support Collecting docker-compose Using cached docker_compose-1.26.0-py2.py3-none-any.whl (139 kB) Requirement already satisfied: backports.shutil-get-terminal-size==1.0.0; python_version < "3.3" in /usr/lib/python2.7/site-packages (from docker-compose) (1.0.0) Requirement already satisfied: python-dotenv<1,>=0.13.0 in /usr/lib/python2.7/site-packages (from docker-compose) (0.13.0) Requirement already satisfied: distro<2,>=1.5.0 in /usr/lib/python2.7/site-packages (from docker-compose) (1.5.0) Requirement already satisfied: texttable<2,>=0.9.0 in /usr/lib/python2.7/site-packages (from docker-compose) (1.6.2) Requirement already satisfied: six<2,>=1.3.0 in /usr/lib/python2.7/site-packages (from docker-compose) (1.15.0) Requirement already satisfied: cached-property<2,>=1.2.0 in /usr/lib/python2.7/site-packages (from docker-compose) (1.5.1) Requirement already satisfied: jsonschema<4,>=2.5.1 in /usr/lib/python2.7/site-packages (from docker-compose) (3.2.0) Requirement already satisfied: backports.ssl-match-hostname<4,>=3.5; python_version < "3.5" in /usr/lib/python2.7/site-packages (from docker-compose) (3.5.0.1) Requirement already satisfied: ipaddress<2,>=1.0.16; python_version < "3.3" in /usr/lib/python2.7/site-packages (from docker-compose) (1.0.16) Collecting subprocess32<4,>=3.5.4; python_version < "3.2" Using cached subprocess32-3.5.4.tar.gz (97 kB) Collecting docopt<1,>=0.6.1 Using cached docopt-0.6.2.tar.gz (25 kB) Collecting dockerpty<1,>=0.4.1 Using cached dockerpty-0.4.1.tar.gz (13 kB) Collecting requests<3,>=2.20.0 Using cached requests-2.23.0-py2.py3-none-any.whl (58 kB) Requirement already satisfied: PyYAML<6,>=3.10 in /usr/lib64/python2.7/site-packages (from docker-compose) (5.3.1) Collecting docker[ssh]<5,>=3.7.0 Using cached docker-4.2.1-py2.py3-none-any.whl (143 kB) Requirement already satisfied: enum34<2,>=1.0.4; python_version < "3.4" in /usr/lib/python2.7/site-packages (from docker-compose) (1.0.4) Collecting websocket-client<1,>=0.32.0 Using cached websocket_client-0.57.0-py2.py3-none-any.whl (200 kB) Requirement already satisfied: typing; python_version < "3.5" in /usr/lib/python2.7/site-packages (from python-dotenv<1,>=0.13.0->docker-compose) (3.7.4.1) Requirement already satisfied: functools32; python_version < "3" in /usr/lib/python2.7/site-packages (from jsonschema<4,>=2.5.1->docker-compose) (3.2.3.post2) Requirement already satisfied: attrs>=17.4.0 in /usr/lib/python2.7/site-packages (from jsonschema<4,>=2.5.1->docker-compose) (19.3.0) Requirement already satisfied: setuptools in /usr/lib/python2.7/site-packages (from jsonschema<4,>=2.5.1->docker-compose) (0.9.8) Requirement already satisfied: pyrsistent>=0.14.0 in /usr/lib64/python2.7/site-packages (from jsonschema<4,>=2.5.1->docker-compose) (0.16.0) Requirement already satisfied: importlib-metadata; python_version < "3.8" in /usr/lib/python2.7/site-packages (from jsonschema<4,>=2.5.1->docker-compose) (1.6.1) Collecting certifi>=2017.4.17 Using cached certifi-2020.4.5.2-py2.py3-none-any.whl (157 kB) Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 Using cached urllib3-1.25.9-py2.py3-none-any.whl (126 kB) Collecting idna<3,>=2.5 Using cached idna-2.9-py2.py3-none-any.whl (58 kB) Collecting chardet<4,>=3.0.2 Using cached chardet-3.0.4-py2.py3-none-any.whl (133 kB) Collecting paramiko>=2.4.2; extra == "ssh" Using cached paramiko-2.7.1-py2.py3-none-any.whl (206 kB) Requirement already satisfied: contextlib2; python_version < "3" in /usr/lib/python2.7/site-packages (from importlib-metadata; python_version < "3.8"->jsonschema<4,>=2.5.1->docker-compose) (0.6.0.post1) Requirement already satisfied: pathlib2; python_version < "3" in /usr/lib/python2.7/site-packages (from importlib-metadata; python_version < "3.8"->jsonschema<4,>=2.5.1->docker-compose) (2.3.5) Requirement already satisfied: zipp>=0.5 in /usr/lib/python2.7/site-packages (from importlib-metadata; python_version < "3.8"->jsonschema<4,>=2.5.1->docker-compose) (1.2.0) Requirement already satisfied: configparser>=3.5; python_version < "3" in /usr/lib/python2.7/site-packages (from importlib-metadata; python_version < "3.8"->jsonschema<4,>=2.5.1->docker-compose) (4.0.2) Collecting cryptography>=2.5 Using cached cryptography-2.9.2-cp27-cp27mu-manylinux2010_x86_64.whl (2.7 MB) Collecting pynacl>=1.0.1 Using cached PyNaCl-1.4.0-cp27-cp27mu-manylinux1_x86_64.whl (964 kB) Collecting bcrypt>=3.1.3 Using cached bcrypt-3.1.7-cp27-cp27mu-manylinux1_x86_64.whl (59 kB) Requirement already satisfied: scandir; python_version < "3.5" in /usr/lib64/python2.7/site-packages (from pathlib2; python_version < "3"->importlib-metadata; python_version < "3.8"->jsonschema<4,>=2.5.1->docker-compose) (1.10.0) Collecting cffi!=1.11.3,>=1.8 Using cached cffi-1.14.0-cp27-cp27mu-manylinux1_x86_64.whl (387 kB) Collecting pycparser Using cached pycparser-2.20-py2.py3-none-any.whl (112 kB) Using legacy setup.py install for subprocess32, since package 'wheel' is not installed. Using legacy setup.py install for docopt, since package 'wheel' is not installed. Using legacy setup.py install for dockerpty, since package 'wheel' is not installed. Installing collected packages: subprocess32, docopt, dockerpty, certifi, urllib3, idna, chardet, requests, websocket-client, pycparser, cffi, cryptography, pynacl, bcrypt, paramiko, docker, docker-compose Running setup.py install for subprocess32 ... done Running setup.py install for docopt ... done Running setup.py install for dockerpty ... done Attempting uninstall: chardet Found existing installation: chardet 2.2.1 Uninstalling chardet-2.2.1: Successfully uninstalled chardet-2.2.1 Successfully installed bcrypt-3.1.7 certifi-2020.4.5.2 cffi-1.14.0 chardet-3.0.4 cryptography-2.9.2 docker-4.2.1 docker-compose-1.26.0 dockerpty-0.4.1 docopt-0.6.2 idna-2.9 paramiko-2.7.1 pycparser-2.20 pynacl-1.4.0 requests-2.23.0 subprocess32-3.5.4 urllib3-1.25.9 websocket-client-0.57.0 [root@docker_node02 ~]# docker-compose --version docker-compose version 1.26.0, build unknown [root@docker_node02 ~]#
提示:如果能夠看到docker-compose的版本信息,就表示docker-compose安裝好了;
2、yum 安裝
[root@docker_node01 ~]# yum install -y docker-compose Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package docker-compose.noarch 0:1.18.0-4.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================= Package Arch Version Repository Size ================================================================================================= Installing: docker-compose noarch 1.18.0-4.el7 epel 222 k Transaction Summary ================================================================================================= Install 1 Package Total download size: 222 k Installed size: 1.1 M Downloading packages: docker-compose-1.18.0-4.el7.noarch.rpm | 222 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : docker-compose-1.18.0-4.el7.noarch 1/1 Verifying : docker-compose-1.18.0-4.el7.noarch 1/1 Installed: docker-compose.noarch 0:1.18.0-4.el7 Complete! [root@docker_node01 ~]# docker-compose -v docker-compose version 1.18.0, build 8dd22a9 [root@docker_node01 ~]#
提示:本人推薦使用yum安裝;從上面的過程來看yum是最方便最簡潔的;
接下來我們來構建一個docker-compose.yml配置文件,然後使用docker-compose來管理啟動
[root@docker_node01 docker_compose_project]# pwd /root/docker_compose_project [root@docker_node01 docker_compose_project]# ls docker-compose.yml [root@docker_node01 docker_compose_project]# cat docker-compose.yml version: "3" services: web1: image: nginx:1.14-alpine ports: - "80:80" [root@docker_node01 docker_compose_project]#
提示:以上docker-compose.yml表示啟動一個容器,並暴露埠;docker-compose.yml這個文件需要註意空格;每一級的縮進必須相同,否則會報錯;docker-compose.yml中主要就是兩個重要的概念,服務(services)和項目(project);所謂服務就是一個應用容器,實際上也可以包含若幹運行相同鏡像的容器示例;項目就是由一組關聯的應用容器組成的一個完整業務單元(若幹服務共同服務組成)
啟動docker-compose.yml中定義的服務
[root@docker_node01 docker_compose_project]# ls docker-compose.yml [root@docker_node01 docker_compose_project]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@docker_node01 docker_compose_project]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker_node01 docker_compose_project]# docker-compose up -d Pulling web1 (nginx:1.14-alpine)... 1.14-alpine: Pulling from library/nginx bdf0201b3a05: Pull complete 3d0a573c81ed: Pull complete 8129faeb2eb6: Pull complete 3dc99f571daf: Pull complete Digest: sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7 Status: Downloaded newer image for nginx:1.14-alpine Creating dockercomposeproject_web1_1 ... done [root@docker_node01 docker_compose_project]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.14-alpine 8a2fb25a19f5 14 months ago 16MB [root@docker_node01 docker_compose_project]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6fcb757fdace nginx:1.14-alpine "nginx -g 'daemon of…" 7 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp dockercomposeproject_web1_1 [root@docker_node01 docker_compose_project]#
提示:可以看到我們直接使用docker-compose up -d 就可以直接啟動為一個nginx容器;這裡需要註意一點的是在執行docker-compose命令必須要在docker-compose.yml所在目錄執行。因為docker-compose這個命令會在在當前目錄查找docker-compose;然後根據docker-compose.yml文件中定義的services來啟動容器;docker-compose up表示創建容器並運行 -d表示把後臺運行容器(不占據當前終端);
停止docker-compose定義的服務
[root@docker_node01 docker_compose_project]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 46b62e878cf9 nginx:1.14-alpine "nginx -g 'daemon of…" 3 minutes ago Up 8 seconds 0.0.0.0:80->80/tcp dockercomposeproject_web1_1 [root@docker_node01 docker_compose_project]# docker-compose stop Stopping dockercomposeproject_web1_1 ... done [root@docker_node01 docker_compose_project]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker_node01 docker_compose_project]#
啟動docker-compose定義的服務
[root@docker_node01 docker_compose_project]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker_node01 docker_compose_project]# docker-compose start Starting web1 ... done [root@docker_node01 docker_compose_project]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 46b62e878cf9 nginx:1.14-alpine "nginx -g 'daemon of…" 3 minutes ago Up 2 seconds 0.0.0.0:80->80/tcp dockercomposeproject_web1_1 [root@docker_node01 docker_compose_project]#
測試:用瀏覽器訪問宿主機的80,看看是否能夠訪問到容器的nginx服務?
提示:可以看到我們訪問宿主機的80是能夠正常訪問的;說明我們在docker-compose.yml文件中定義的容器和暴露的埠都已正常啟動並把對應的埠暴露給宿主機;
示例:自定義容器名稱
提示:container_name表示指定運行容器的名稱;
提示:如果預設不指定容器名稱,docker會預設命名為項目名稱_+服務名稱+第幾個容器類似這種邏輯;項目名稱就是docker-compose.yml文件所在目錄名稱,通常它會把"_","-"下劃線中橫線都去掉;
驗證:停止現有容器,重新啟動容器,看看容器名稱是否更改?
提示:更改了docker-compose.yml文件後,必須使用up 命令來重新讀取docker-compose.yml文件,根據文件中的內容重新啟動運行容器;如果使用start來啟動,我們在docker-compose.yml中新定義的內容是不會生效的;
示例:查看容器進程
提示:docker-compose ps 顯示的信息要比docker ps 顯示的信息更為精簡;
示例:從docker compose啟動多個容器
[root@docker_node01 docker_compose_project]# cat docker-compose.yml version: "3" services: web1: container_name: myweb1 image: nginx:1.14-alpine ports: - "80:80" web2: container_name: myweb2 image: nginx:1.14-alpine ports: - "81:80" [root@docker_node01 docker_compose_project]#
提示:以上docker-compose.yml文件定義了兩個服務,web1和web2;這裡需要註意一點服務名稱或id不能相同;容器的名稱也必須是唯一的;
驗證:停止現有容器,重新讀取docker-compose.yml文件,看看是否會啟動兩個nginx容器?
提示:可以看到myweb2也正常啟動了,並把容器內部的80映射為宿主機的81埠;
驗證:用瀏覽器訪問宿主機的81埠,看看是否能夠訪問得到?
提示:可以看到81埠是可以正常訪問的;
示例:定義數據捲掛載
提示:以上紅框中的內容表示把宿主機上的/data/nginx/目錄掛載到容器內部的/usr/share/nginx/html目錄下;
重啟容器
提示:可以看到容器內部已經可以訪問我們對應宿主機上的文件;
驗證:用瀏覽器訪問宿主機的80看看是否能夠訪問到我們提供的index.html頁面?
提示:用瀏覽器訪問可以正常訪問到宿主機掛載到容器內部的index.html文件內容;
示例:重啟單個服務
提示:docker-compose管理的是以服務為單位,所以我們管理單個服務需要指定服務端名稱,而非容器的名稱;
示例:重啟所有服務
提示:重啟所有服務docker-compose會自動讀取docker-compose.yml文件中定義的服務,來重啟容器;
示例:利用docker-compose一件部署wordpress
[root@docker_node01 wordpress]# cat docker-compose.yml version: '3.3' services: wordpress: depends_on: - db image: wordpress:latest volumes: - wordpress_files:/var/www/html ports: - "80:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_NAME: wordpress WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: admin123.com db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: my_db_root_password MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: admin123.com volumes: wordpress_files: db_data: [root@docker_node01 wordpress]#
提示:version表示配置文件版本,depends_on表示指定依賴的服務列表;restart指令表示重啟策略,restart:always就表示只要出現問題就重啟;environment表示定義傳給容器的環境變數;volumes:表示定義兩個存儲捲。這裡是直接定義兩個捲的名字,並沒有指定對應目錄;這種捲就是docker自身管理的捲,通常在/var/lib/docker/volumes/服務名_+捲名/_data/;以上就啟動兩個服務,wordpress依賴db服務;這裡需要註意一點,資料庫容器啟動必須要傳環境變數給容器,比如MYSQL_ROOT_PASSWORD就表示給mysql設定root密碼;
驗證:停止nginx容器,啟動wordpress
[root@docker_node01 wordpress]# pwd /root/wordpress [root@docker_node01 wordpress]# ls docker-compose.yml [root@docker_node01 wordpress]# cd [root@docker_node01 ~]# cd docker_compose_project/ [root@docker_node01 docker_compose_project]# ls docker-compose.yml [root@docker_node01 docker_compose_project]# docker-compose stop Stopping myweb2 ... done Stopping myweb1 ... done [root@docker_node01 docker_compose_project]# cd ../wordpress/ [root@docker_node01 wordpress]# pwd /root/wordpress [root@docker_node01 wordpress]# ls docker-compose.yml [root@docker_node01 wordpress]#docker-compose up -d Creating network "wordpress_default" with the default driver Creating volume "wordpress_wordpress_files" with default driver Creating volume "wordpress_db_data" with default driver Pulling db (mysql:5.7)... 5.7: Pulling from library/mysql 8559a31e96f4: Pull complete d51ce1c2e575: Pull complete c2344adc4858: Pull complete fcf3ceff18fc: Pull complete 16da0c38dc5b: Pull complete b905d1797e97: Pull complete 4b50d1c6b05c: Pull complete d85174a87144: Pull complete a4ad33703fa8: Pull complete f7a5433ce20d: Pull complete 3dcd2a278b4a: Pull complete Digest: sha256:32f9d9a069f7a735e28fd44ea944d53c61f990ba71460c5c183e610854ca4854 Status: Downloaded newer image for mysql:5.7 Pulling wordpress (wordpress:latest)... latest: Pulling from library/wordpress 8559a31e96f4: Already exists e0276193a084: Pull complete eb2d00c10344: Pull complete f54006e0dc29: Pull complete e0d3d1244592: Pull complete 3a60f364b0c5: Pull complete 3e309988c00b: Pull complete bff5c52c95f4: Pull complete c9086065e896: Pull complete f07bb8dcc770: Pull complete 7ad826bc3623: Pull complete 0f6a0743d973: Pull complete 43b190adf511: Pull complete cc02e4c247ac: Pull complete cd5c5e392dc3: Pull complete a029df01fe2d: Pull complete 6c942bd52caf: Pull complete 804bbda23122: Pull complete bb70a73ffdf7: Pull complete 9c44164d321c: Pull complete ba02d5b98827: Pull complete Digest: sha256:602ea0959040cd101e5e6923474612a15f9dd330ab45ceaec4e07dc762d674c0 Creating wordpress_db_1 ... done Creating wordpress_db_1 ... Creating wordpress_wordpress_1 ... done [root@docker_node01 wordpress]#docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cf61629c8f36 wordpress:latest "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp wordpress_wordpress_1 04cc6dcddd7d mysql:5.7 "docker-entrypoint.s…" About a minute ago Up About a minute 3306/tcp, 33060/tcp wordpress_db_1 [root@docker_node01 wordpress]#
提示:可以看到wordpress和mysql容器都跑起來了;
驗證:用瀏覽器訪問宿主機80,看看是否訪問到wordpress?
提示:可以看到wordpress是可以直接訪問的,並且不需要我們再設置數據了,因為我們在docker-compose.yml中設置好了,這裡直接安裝即可;
通過上面的演示,我們利用docker-compose就可以輕鬆部署一個應用,我們只需要在docker-compose.yml中定義好每個容器間的依賴關係,存儲捲等等一系列關係,然後通過docker-compose 直接就可以根據我們定義的關係來啟動容器;以上就是docker-compose的簡單示例和使用說明;有關docker-compose.yml中指令的說明可以參考官方文檔https://docs.docker.com/compose/compose-file/#service-configuration-reference;