前文我们聊了下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('"'"' '"'"', '"'"' '"'"');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('"'"' '"'"', '"'"' '"'"');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;