使用compose构建Docker多容器应用(Nginx+PHP+MySQL+Redis)

docker的三剑客: 

docker-compose : 就是通过一个.yml配置文件,将所有的容器的部署方法、文件映射、容器连接等等一系列的配置写在一个配置文件里,最后只需要执行docker-compose up命令就会像执行脚本一样的去一个个安装容器并自动部署他们,极大的便利了复杂服务的部署。
docker-machine : 用于在各种平台上快速创建具有docker服务的虚拟机的技术;   创建阿里云服务器实列
docker-swarm : swarm是基于docker平台实现的集群技术,他可以通过几条简单的指令快速的创建一个docker集群,接着在集群的共享网络上部署应用,最终实现分布式的服务。

在这里我们使用 docker-composer, 首先安装docker-composer

#下载并移动到/usr/local/bin目录下
curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose
#赋予可执行权限
chmod +x /usr/local/bin/docker-compose
#使用docker-compose --versionn 能查看到版本说明安装成功
[root@VM_0_15_centos /]# docker-compose --version                                                                            
docker-compose version 1.16.1, build 6d1ac21

创建必要的目录

# .yml配置文件目录
[root@VM_0_15_centos /]# mkdir docker_test
[root@VM_0_15_centos /]# cd docker_test
#php的Dockerfile存放目录
[root@VM_0_15_centos docker_test]# mkdir php
#nginx的Dockerfile存放目录
[root@VM_0_15_centos docker_test]# mkdir nginx
#和容器共享的目录
[root@VM_0_15_centos docker_test]# mkdir /www/share

创建Nginx的Docker

[root@VM_0_15_centos docker_test]# cd nginx
[root@VM_0_15_centos nginx]# vim Dockerfile
#在Dockerfile添加如下配置
FROM nginx:latest  #使用nginx为基础镜像                                                                                      
COPY default.conf /etc/nginx/conf.d/default.conf #将宿主机的文件拷贝到容器中

#创建test.conf文件
[root@VM_0_15_centos nginx]# vim default.conf
#添加以下配置,以下内容仅供参考,请按实际环境定制                                                                                          
server /docker-test {                                                                                                        
    listen       80;                                                                                                         
    server_name  localhost;                                                                                                  
    root   /usr/share/nginx/html;                                                                                            
    index  index.html index.htm index.php;                                                                                   
    location ~ \.php$ {                                                                                                      
        fastcgi_pass   php-fpm:9000;                                                                                         
        fastcgi_index  index.php;                                                                                            
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/$fastcgi_script_name;                                          
        include        fastcgi_params;                                                                                       
    }                                                                                                                        
}

创建PHP的Docker

[root@VM_0_15_centos docker_test]# cd php                                                                                 
[root@VM_0_15_centos php]# vim Dockerfile
#添加以下配置
FROM php:7.3-fpm
RUN docker-php-ext-install pdo pdo_mysql mysqli \
&& pecl install redis-4.2.0 && docker-php-ext-enable redis  #安装常用扩展

创建docker-compose.yml文件

[root@VM_0_15_centos php]# cd ..
[root@VM_0_15_centos docker_test]# vim docker-compose.yml
#添加以下配置
networks: #创建网络                                                                                                          
  dev-net: #网络名称,方便下面使用                                                                                            
    driver: bridge  #使用桥接模式                                                                                            
services:                                                                                                                    
 mysql: #容器名                                                                                                              
    image: mysql:latest #使用镜像,先从本地获取镜像,没有再从远程仓库中拉去                                                    
    command: --default-authentication-plugin=mysql_native_password #执行默认的命令,重置mysql密码                             
    restart: always #重新启动容器                                                                                            
    environment: #是在容器中的环境变量通过env查看所有环境变量                                                                
      MYSQL_ROOT_PASSWORD: root                                                                                              
    ports:                                                                                                                   
      - 3304:3306                                                                                                            
    networks:                                                                                                                
     - dev-net #使用网络的名称 ip会自动分配,也可以手动指定                                                                   
 nginx:                                                                                                                      
    build: nginx #使用Dockerfile构建容器                                                                                     
    restart: always                                                                                                          
    ports:                                                                                                                   
      - 7010:80                                                                                                              
    volumes:                                                                                                                 
    - /www/share/:/usr/share/nginx/html   #宿主机和容器共享                                                                  
    depends_on: #依赖关系                                                                                                    
      - mysql                                                                                                                
      - redis                                                                                                                
      - php-fpm                                                                                                              
    networks:                                                                                                                
     - dev-net                                                                                                               
 php-fpm:                                                                                                                    
    build: php                                                                                                               
    volumes:                                                                                                                 
    - /www/share/:/usr/share/nginx/html                                                                                      
    networks:                                                                                                                
     - dev-net                                                                                                               
 redis:                                                                                                                      
    image: redis:latest                                                                                                      
    networks:                                                                                                                
     - dev-net  
#进入共享共享目录创建访问文件                                                                 
[root@VM_0_15_centos docker_test]# cd /www/share
[root@VM_0_15_centos share]# ls
index.html  index.php

#执行docker-composer
[root@VM_0_15_centos share]# cd /docker_test
[root@VM_0_15_centos docker_test]# docker-compose up -d

MyAnswer博客



MyAnswer博客

提示

执行完后查看下容器状态, 可以通过 docker logs 容器id 查看错信息

容器之间可以使用内网ip进行访问

MyAnswer博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论