- 我们知道使用一个 Dockerfile 模板文件可以定义一个单独的应用容器,如果需要定义多个容器就需要服务编排。服务编排有很多种技术方案,今天给大家介绍 Docker 官方产品 Docker-Compose。
- Dockerfile 可以定义一个单独的应用容器;而 docker-compose 则允许用户在一个模板(YAML 格式)中定义一组相关联的应用容器(被称为一个 project,即项目),例如一个 Web 服务容器再加上后端的数据库服务容器(LNMP)等。
- Docker-Compose 项目由 Python 编写,调用 Docker 服务提供的 API 来对容器进行管理。因此,只要所操作的平台支持 Docker API,就可以在其上利用 Compose 来进行编排管理。
- Docker-Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。
- Docker-Compose 将所管理的容器分为三层,分别是工程(project),服务(service)以及容器(container)。
- Docker-Compose 运行目录下的所有文件(docker-compose.yml,extends 文件或环境变量文件等)组成一个工程,若无特殊指定工程名即为当前目录名。一个工程当中可包含多个服务,每个服务中定义了容器运行的镜像、参数、依赖。一个服务当中可包括多个容器实例。
- Docker-Compose 并没有解决负载均衡的问题,因此需要借助其他工具实现服务发现及负载均衡,比如 Consul。
- Docker-Compose 的工程配置文件默认为 docker-compose.yml,可通过环境变量 COMPOSE_FILE 或 -f 参数自定义配置文件,其定义了多个有依赖关系的服务及每个服务运行的容器。
YAML 是一种标记语言,它可以很直观的展示数据序列化格式,可读性高。类似于 SML 数据描述语言,语法比 XMA 简单很多。YAML 数据结构通过缩进来表示,连续的项目通过符号 -
来表示,键值对用符号 :
分割,数组用中括号 [ ]
括起来,hash 用花括号 {}
括起来。
1.对象:键值对的字典
- animal: pets
2.数组:一组按次序排列的结果
- CAT
- DOG
- GOLDFISH
3.布尔值
- debug: true
- debug: false
示例:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 8080
[root@c7-1 ~] ................ [root@c7-1 ~] /opt/harbor/common/ ├── config │ ├── adminserver │ │ └── env │ ├── db │ │ └── env │ ├── jobservice │ │ ├── app.conf │ │ └── env │ ├── nginx │ │ ├── client_body_temp │ │ ├── conf.d │ │ ├── fastcgi_temp │ │ ├── nginx.conf │ │ ├── proxy_temp │ │ ├── scgi_temp │ │ └── uwsgi_temp │ ├── registry │ │ ├── config.yml │ │ └── root.crt │ └── ui │ ├── app.conf │ ├── env │ └── private_key.pem └── templates ├── adminserver │ └── env ├── clair │ ├── config.yaml │ ├── postgres_env │ └── postgresql-init.d │ └── README.md ├── db │ └── env ├── jobservice │ ├── app.conf │ └── env ├── nginx │ ├── nginx.http.conf │ ├── nginx.https.conf │ ├── notary.server.conf │ └── notary.upstream.conf ├── notary │ ├── mysql-initdb.d │ │ ├── initial-notaryserver.sql │ │ └── initial-notarysigner.sql │ ├── notary-signer-ca.crt │ ├── notary-signer.crt │ ├── notary-signer.key │ ├── server-config.json │ ├── signer-config.json │ └── signer_env ├── registry │ ├── config.yml │ └── root.crt └── ui ├── app.conf ├── env └── private_key.pem
Docker-Compose 是 Docker 的独立产品,因此需要安装 Docker 之后再单独安装 Docker-Compose
[root@docker ~] [root@docker ~] [root@docker ~] Active: active (running) since 三 2021-12-08 08:51:44 CST; 29s ago
[root@c7-1 ~] [root@c7-1 ~] [root@c7-1 ~] docker-compose version 1.21.1, build 5a3f1a3
[root@c7-1 ~] [root@c7-1 ~] [root@c7-1 ~] [root@c7-1 /opt/compose_nginx/nginx] [root@c7-1 /opt/compose_nginx/nginx] nginx-1.12.0.tar.gz [root@c7-1 /opt/compose_nginx/nginx] [root@c7-1 /opt/compose_nginx/nginx] FROM centos:7 MAINTAINER nginx:test RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin nginx ADD nginx-1.12.0.tar.gz /usr/local/src/ WORKDIR /usr/local/src/nginx-1.12.0 RUN ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module;make -j 4 && make install ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 EXPOSE 443 VOLUME [ "/usr/local/nginx/html/" ] CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]
[root@c7-1 /opt/compose_nginx/nginx] [root@c7-1 /opt/compose_nginx] [root@c7-1 /opt/compose_nginx] version: '3' services: nginx: hostname: nginx build: context: ./nginx dockerfile: Dockerfile ports: - 1216:80 - 1217:443 networks: - test volumes: - ./wwwroot:/usr/local/nginx/html networks: test:
参数说明:
version: '3' '//compose编排版本为3' services: '//服务:' nginx: '//服务名称' hostname: nginx '//容器主机名' build: '//创建容器过程:' context: ./nginx '//所需素材都在该目录(仓库)下' dockerfile: Dockerfile '//指定Dockerfile文件' ports: '//提供端口:' - 1216:80 '//对外提供(暴露)的端口' - 1217:443 networks: '//指定网络' - test '//网络名称(群集名称需一致)' volumes: '//数据卷' - ./wwwroot:/usr/local/nginx/html '//将宿主机该仓库内文件与nginx首页文件相关联(容器)' networks: '//对外公开需要声明networks群集名称' test:
[root@c7-1 /opt/compose_nginx] [root@c7-1 /opt/compose_nginx] REPOSITORY TAG IMAGE ID CREATED SIZE compose_nginx_nginx latest eb67824e6021 33 seconds ago 472MB centos 7 eeb6ee3f44bd 2 months ago 204MB [root@c7-1 /opt/compose_nginx] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 98fe56f04f2a compose_nginx_nginx "/usr/local/nginx/sb…" 43 seconds ago Up 41 seconds 0.0.0.0:1216->80/tcp, :::1216->80/tcp, 0.0.0.0:1217->443/tcp, :::1217->443/tcp compose_nginx_nginx_1
-f:使用特定的 compose 模板文件,默认为 docker-compose.yml
-p:指定项目名称,默认使用目录名称
-d:在后台运行
[root@c7-1 /opt/compose_nginx/wwwroot] [root@c7-1 /opt/compose_nginx/wwwroot] index.html [root@c7-1 /opt/compose_nginx/wwwroot] <h1>hello world</h1>
Dockerfile 文件详情见往期博客 — Docker 搭建 LNMP + WordPress
存放目录为:
[root@c7-1 ~] [root@c7-1 ~] [root@c7-1 ~]
文件配置:
[root@c7-1 ~] Dockerfile nginx-1.12.0.tar.gz nginx.conf wordpress-4.9.4-zh_CN.tar.gz [root@c7-1 ~] Dockerfile my.cnf mysql-boost-5.7.20.tar.gz [root@c7-1 ~] Dockerfile php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf
[root@c7-1 /opt/compose_lnmp] version: '2' services: nginx: hostname: nginx build: context: /opt/compose_lnmp/nginx/ dockerfile: Dockerfile ports: - 1111:80 - 2222:443 container_name: nginx_server networks: lnmp: ipv4_address: 172.111.0.10 mysql: hostname: mysql build: context: /opt/compose_lnmp/mysql/ dockerfile: Dockerfile ports: - 3306:3306 container_name: mysql_server networks: lnmp: ipv4_address: 172.111.0.20 php: hostname: php build: context: /opt/compose_lnmp/php/ dockerfile: Dockerfile ports: - 9000:9000 container_name: php_server networks: lnmp: ipv4_address: 172.111.0.30 networks: lnmp: driver: bridge ipam: config: - subnet: 172.111.0.0/16
[root@c7-1 /opt/compose_lnmp] ...... [root@c7-1 /opt/compose_lnmp] REPOSITORY TAG IMAGE ID CREATED SIZE compose_lnmp_php latest 86b0447ebabd About a minute ago 1.22GB compose_lnmp_mysql latest 59579ca01443 5 minutes ago 9.98GB compose_lnmp_nginx latest 82d760d5d811 17 minutes ago 528MB centos 7 eeb6ee3f44bd 2 months ago 204MB [root@c7-1 /opt/compose_lnmp] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 53c87813597f compose_lnmp_php "/bin/sh -c '/usr/lo…" About a minute ago Up About a minute 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php_server 2d671252967b compose_lnmp_nginx "/usr/local/nginx/sb…" About a minute ago Up About a minute 0.0.0.0:1111->80/tcp, :::1111->80/tcp, 0.0.0.0:2222->443/tcp, :::2222->443/tcp nginx_server 9d5428cfdf54 compose_lnmp_mysql "/usr/sbin/init" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mysql_server
[root@c7-1 /opt/compose_lnmp] [root@mysql bin] Enter password: mysql> create database wordpress; Query OK, 1 row affected (0.00 sec) mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all privileges on *.* to 'root'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
访问 http://192.168.10.20:11111/wordpress/index.php
原文链接:https://blog.csdn.net/m0_59430185/article/details/121783109?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168623078216800185866810%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168623078216800185866810&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-12-121783109-null-null.268%5Ev1%5Ekoosearch&utm_term=docker%E3%80%81wordpress%E3%80%81wordpress%E5%BB%BA%E7%AB%99%E3%80%81wordpress%E4%B8%BB%E9%A2%98%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81