5. Docker Compose讲解+网站部署实战

Docker Compose 是什么

docker compose 通过docker-compose.yml是将多个服务(即容器)构建成一个项目,来完成某个需求。

Compose 的默认管理对象是项目,通过子命令对项目中的一组容器进行便捷地生命周期管理。

Docker Compose 安装

安装epl 软件源

yum install -y epel-release

安装python-pip

yum install -y python-pip

安装Docker Compose

pip install docker-compose

查看Docker Compose 版本

docker-compose version
root@MiWiFi-R3-srv ~]# docker-compose version                                     
docker-compose version 1.22.0, build f46880f
docker-py version: 3.5.0
CPython version: 2.7.5
OpenSSL version: OpenSSL 1.0.2k-fips  26 Jan 2017

Docker Compose 项目构建

通过Docker compose file 构建 wordpress 应用

编写 wordpress docker compose file 文件
新建wordpress 文件夹,并切换进入wordpress文件夹

[root@localhost ~]# mkdir wordpress
[root@localhost ~]# cd wordpress/
[root@localhost wordpress]# ll
总用量 0
[root@localhost wordpress]# pwd
/root/wordpress

新建wordpress docker compose file 文件
文件名:docker-compose.yml

version: "3"
services:

   db:
     image: mysql:latest
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "80:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress

volumes:
    db_data:

Docker compose file 字段解析

version:声明构建项目的版本号

services:声明此项目有几个服务(容器)构成

db、wordpress: 服务名称

image: 镜像名称(默认来自于docker官方镜像仓库)

mysql image 官方地址

wordpress image 官方地址

volumes: 挂载主机目录到此服务中并给此目录命名
使用命名卷必须先在首层docker file 文件中声明

restart: 服务异常后的重启方式

environment: 需要添加什么变量根据官方镜像说明添加

image

image

ports: 主机端口和服务端口的映射关系

启动wordpress docker compose 项目构建

docker-compose up -d 

构成成功后的wordpress

image

wordpress 安装成功后的界面

image

Docker compose 命令使用

Docker compose 指令必须在含有docker-compose.yml或者 docker-compose.yaml的目录执行

查看compose 文件含有的镜像

docker-compose images 
[root@localhost wordpress]# docker-compose images 
      Container         Repository    Tag       Image Id      Size 
-------------------------------------------------------------------
wordpress_db_1          mysql        latest   29e0ae3b69b9   462 MB
wordpress_wordpress_1   wordpress    latest   e2c4085bbc2b   389 MB

列出项目中所有的容器

docker-compose ps
[root@localhost wordpress]# docker-compose ps
        Name                       Command               State          Ports       
------------------------------------------------------------------------------------
wordpress_db_1          docker-entrypoint.sh mysqld      Up      3306/tcp, 33060/tcp
wordpress_wordpress_1   docker-entrypoint.sh apach ...   Up      0.0.0.0:80->80/tcp 

停止项目中正在运行的容器

docker-compose stop
[root@localhost wordpress]# docker-compose stop
Stopping wordpress_wordpress_1 ... done
Stopping wordpress_db_1        ... done

删除项目中所有停止的容器

docker-compose rm
[root@localhost wordpress]# docker-compose rm
Going to remove wordpress_wordpress_1, wordpress_db_1
Are you sure? [yN] y
Removing wordpress_wordpress_1 ... done
Removing wordpress_db_1        ... done

构建项目镜像并启动

docker-compose up -d
[root@localhost wordpress]# docker-compose up -d
Creating wordpress_db_1 ... done
Creating wordpress_wordpress_1 ... done

启动compose 项目中的容器

docker-compose start
[root@localhost wordpress]# docker-compose start
Starting db        ... done
Starting wordpress ... done

指定项目中容器的启动数量

docker-compose scale wordpress=3 db=2
[root@localhost wordpress]# docker-compose scale wordpress=3 db=2
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Creating wordpress_db_1 ... done
Creating wordpress_db_2 ... done
Creating wordpress_wordpress_1 ... done
Creating wordpress_wordpress_2 ... done
Creating wordpress_wordpress_3 ... done

查看项目中服务对应绑定的端口号

docker-compose port wordpress 80
[root@localhost wordpress]# docker-compose port wordpress 80
0.0.0.0:80

查看项目中服务的日志信息

docker-compose logs -f wordpress
[root@localhost wordpress]# docker-compose logs -f wordpress
Attaching to wordpress_wordpress_1
wordpress_1  | WordPress not found in /var/www/html - copying now...
wordpress_1  | Complete! WordPress has been successfully copied to /var/www/html
wordpress_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.20.0.3. Set the 'ServerName' directive globally to suppress this message
wordpress_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.20.0.3. Set the 'ServerName' directive globally to suppress this message
wordpress_1  | [Sun Aug 19 07:13:26.743777 2018] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.2.8 configured -- resuming normal operations
wordpress_1  | [Sun Aug 19 07:13:26.743842 2018] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

5. Docker Compose讲解+网站部署实战

全文结束