- 我们知道使用一个 Dockerfile 模板文件可以定义一个单独的应用容器,如果需要定义多个容器就需要服务编排。服务编排有很多种技术方案,今天给大家介绍 Docker 官方产品 Docker-Compose
- Dockerfile 可以定义一个单独的应用容器;而 docker-compose 则允许用户在一个模板(YAML 格式)中定义一组相关联的应用容器(被称为一个 project,即项目),例如一个 Web 服务容器再加上后端的数据库服务容器(LNMP)等
- Docker-Compose 项目由 Python 编写,调用 Docker 服务提供的 API 来对容器进行管理。因此,只要所操作的平台支持 Docker API,就可以在其上利用 Compose 来进行编排管理
使用 Docker Compose 可以轻松、高效的管理容器,它是一个用于定义和运行多容器 Docker 的应用程序工具
Docker Compose 使用 YML 文件来配置应用程序所需要的的所有服务,非常适合组合使用多个容器进行开发的场景,且不再需要使用 shell 脚本来启动容器
YML(重点):
1.YAML是一种标记语言很直观的数据序列化格式,通常以.yml或.yaml为后缀的文件,阅读性很友好
2.不支持制表符tab键缩进,需要使用空格缩进,使用缩进表示层级关系
3.通常开头缩进 2 个空格,缩进的空格数不重要,只要相同层级的元素左对齐即可
4.字符后缩进一个空格,如冒号、逗号、横杆,如果包含特殊字符用单引号引起来
5.用#号表示注释
1.Docker-Compose 配置常用字段
2.Docker-Compose 常用命令
'命令格式:' docker-compose [选项] [命令] [参数] '常用选项:' -verbose:输出更多调试信息 -version:打印版本并退出 -f:使用特定的compose模板文件,默认为docker-compose.yml -p:指定项目名称,默认使用目录名称
3.Docker-Compose 文件结构
[root@docker ~] [root@docker ~] /opt/compose_lnmp/ ├── docker-compose.yml ├── mysql │ ├── Dockerfile │ ├── my.cnf │ └── mysql-boost-5.7.20.tar.gz ├── nginx │ ├── Dockerfile │ ├── nginx-1.12.0.tar.gz │ ├── nginx.conf │ └── wordpress-4.9.4-zh_CN.tar.gz └── php ├── Dockerfile ├── php-7.1.10.tar.bz2 ├── php-fpm.conf ├── php.ini └── www.conf
Docker-Compose 是 Docker 的独立产品,因此需要安装 Docker 之后再单独安装 Docker-Compose
[root@localhost ~] [root@localhost bin] [root@localhost bin] % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 664 100 664 0 0 323 0 0:00:02 0:00:02 --:--:-- 323 100 10.3M 100 10.3M 0 0 1414k 0 0:00:07 0:00:07 --:--:-- 2607k [root@localhost bin] docker-compose [root@localhost bin] [root@localhost bin] docker-compose [root@localhost bin] docker-compose version 1.21.2, build a133471
1. 准备依赖文件
[root@localhost ~] [root@localhost ~] [root@localhost nginx] [root@localhost nginx] nginx-1.12.2.tar.gz [root@localhost nginx] [root@localhost nginx] FROM centos:7 RUN yum -y update RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make RUN useradd -M -s /sbin/nologin nginx ADD nginx-1.12.2.tar.gz /usr/local/src WORKDIR /usr/local/src/nginx-1.12.2 RUN ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module RUN make -j 4 && make install RUN ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ EXPOSE 80 EXPOSE 443 RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf CMD ["/usr/local/nginx/sbin/nginx"]
2. 编写配置文件 docker-compose.yml
[root@localhost ~] [root@localhost nginx] [root@localhost compose_nginx] [root@localhost compose_nginx] version: '3' services: nginx: hostname: nginx build: context: ./nginx dockerfile: Dockerfile ports: - 1216:80 - 1216:443 networks: - xjj volumes: - ./wwwroot:/usr/local/nginx/html networks: xjj:
- 参数说明:
version: '3' services: nginx: hostname: nginx build: context: ./nginx dockerfile: Dockerfile ports: - 1216:80 - 1217:443 networks: - xjj volumes: - ./wwwroot:/usr/local/nginx/html networks: xjj:
3. 生成镜像容器
[root@localhost nginx] ... ... Successfully built 4e35d880d9ca Successfully tagged compose_nginx_nginx:latest ———————————————— [root@localhost compose_nginx] REPOSITORY TAG IMAGE ID CREATED SIZE compose_nginx_nginx latest 4e35d880d9ca 49 seconds ago 729MB centos 7 eeb6ee3f44bd 2 months ago 204MB [root@localhost compose_nginx] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 063f07eec655 compose_nginx_nginx "/usr/local/nginx/sb…" 56 seconds ago Created compose_nginx_nginx_1
4. 访问测试
[root@localhost compose_nginx] docker-compose.yml nginx wwwroot [root@localhost compose_nginx] [root@localhost compose_nginx] <h1>hello world</h1>
5.查看结构
[root@localhost compose_nginx] [root@localhost compose_nginx] ./ ├── docker-compose.yml ├── nginx │ ├── Dockerfile │ └── nginx-1.12.2.tar.gz └── wwwroot └── index.html 2 directories, 4 files
1. 编写 Dockerfile 文件
Dockerfile 文件详情见往期博客
- 存放目录为:
[root@localhost ~] [root@localhost ~] [root@localhost ~]
- 文件配置:
[root@localhost ~] Dockerfile nginx-1.12.0.tar.gz nginx.conf wordpress-4.9.4-zh_CN.tar.gz [root@localhost ~] Dockerfile my.cnf mysql-boost-5.7.20.tar.gz [root@localhost ~] Dockerfile php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf
2. 编写配置文件 docker-compose.yml
[root@localhost ~] 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
3. 生成镜像容器
[root@localhost /opt/compose_lnmp] ...... [root@localhost /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@localhost /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
4. 启动 wordpress 服务
[root@localhost /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.126.12:11111/wordpress/index.php
原文链接:https://blog.csdn.net/weixin_53560205/article/details/121732395?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168994567316782425116482%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168994567316782425116482&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-10-121732395-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















