容器編排工具之Docker-compose

来源:https://www.cnblogs.com/qiuhom-1874/archive/2020/06/13/13121678.html
-Advertisement-
Play Games

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


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 系列文章 基於 abp vNext 和 .NET Core 開發博客項目 - 使用 abp cli 搭建項目 基於 abp vNext 和 .NET Core 開發博客項目 - 給項目瘦身,讓它跑起來 基於 abp vNext 和 .NET Core 開發博客項目 - 完善與美化,Swagger登場 ...
  • 前言 在本章中,主要是藉機這個C#基礎篇的系列整理過去的學習筆記、歸納總結並更加理解透徹。 在上一篇文章,我們已經對委托有了進一步瞭解,委托相當於用方法作為另一方法參數,同時,也可以實現在兩個不能直接調用的方法中做橋梁。 下麵我們來回顧一下委托的例子。 public delegate void Ex ...
  • 前言 從題目我們可以看的出,今天是五部曲的第三部,你可能會好奇,為啥沒有見到前兩部呢?這裡我簡單說下: 1、跨平臺第一部曲:MySql 如果你看我的所有開源項目,應該能發現我已經全部遷移到了Mysql資料庫了,這裡不是說SqlServer就不是不跨平臺了,只是MySql用著可能更好些,當然,如果你是 ...
  • 前言 寫這篇博客主要目的是記錄一下自己的學習過程,只能是簡單入門級別的,因為水平有限就寫到哪算哪吧,寫的不對之處歡迎指正。 代碼放在:https://github.com/xiajingren/NetCoreMicroserviceDemo 什麼是微服務? 關於微服務的概念解釋網上有很多... 個人 ...
  • 一:背景 1. 講故事 好消息,.NET 5.0 終於在2020年6月10日發佈了第五個預覽版,眼尖的同學一定看到了在這個版本中終於支持了 C# 9.0,此處有掌聲,太好了!!! .Net5官方鏈接 可以看到目前的C#9還是預覽版,實現了一部分新語法供開發者提前嘗鮮,從github的roslyn倉庫 ...
  • 之前說了很多關於 Nginx 模塊的內容,還有一部分非常重要的內容,那就是 Nginx 的變數。變數在 Nginx 中可以說無處不在,認識瞭解這些變數的作用和原理同樣是必要的,下麵幾乎囊括了關於 Nginx 的所有變數,單獨看起來可能比較枯燥,放心,後面依然有實戰內容。 Nginx 變數的運行原理 ...
  • 一、zabbix簡介 1、簡介 zabbix([`zæbiks])是一個基於WEB界面的提供分散式系統監視以及網路監視功能的企業級的開源解決方案。zabbix能監視各種網路參數如:CPU負荷、記憶體使用、磁碟使用、網路狀況、埠監視、日誌監視等。zabbix主要由2部分構成,zabbix server ...
  • 使用win32 API製作美觀按鈕,當滑鼠移入/移出按鈕時改變按鈕背景顏色,類似HTML網頁中的效果,告別win32 API編程中預設的灰色按鈕,效果圖見下麵動圖和視頻。 下載地址: 按鈕效果(win32編程).zip https://files.cnblogs.com/files/techinge ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...