docker-compose 通过docker-compose.yml 安装

 

一、创建相关目录

1、创建配置文件目录

mkdir -p /data/docker/lnmp-test/dockerfile/nginx/conf
mkdir -p /data/docker/lnmp-test/dockerfile/nginx/conf/conf.d
mkdir -p /data/docker/lnmp-test/dockerfile/php/conf
mkdir -p /data/docker/lnmp-test/dockerfile/mysql/conf
mkdir -p /data/docker/lnmp-test/dockerfile/mongodb/conf
mkdir -p /data/docker/lnmp-test/dockerfile/redis/conf

2、创建日志目录(mysql的日志文件存放在数据目录下了,所以这里就不创建mysql的日志目录了)

mkdir -p /data/docker/lnmp-test/dockerfile/nginx/log
mkdir -p /data/docker/lnmp-test/dockerfile/php/log
mkdir -p /data/docker/lnmp-test/dockerfile/mongodb/log
mkdir -p /data/docker/lnmp-test/dockerfile/redis/log

3、创建数据目录

mkdir -p /data/docker/lnmp-test/dockerfile/mysql/data
mkdir -p /data/docker/lnmp-test/dockerfile/mongodb/data
mkdir -p /data/docker/lnmp-test/dockerfile/redis/data

4、创建nginx网站根目录

mkdir -p /data/docker/lnmp-test/dockerfile/nginx/www

 

二、创建compose文件(其中- /etc/localtime:/etc/localtime:rw是为了同步宿主机和容器的时间)

cd /data/docker/lnmp-test/dockerfile/
vim docker-compose.yml
version: "3"
services:
 
#### mysql
  mysql:
      image: centos6:mysql
      container_name: lianni-mysql
      ports:
        - "3306:3306"
      volumes:
        - ./mysql/conf/my.cnf:/etc/mysql/my.cnf:rw
        - ./mysql/data/:/data/mysql/:rw
        - /etc/localtime:/etc/localtime:rw
      networks:
        - net-lianni
      restart: always
 
 
#### php
  php:
      image: centos6:php
      container_name: lianni-php
      ports:
        - "9000:9000"
      volumes:
        - /a01/docker/lianni/dockerfile/nginx/www/:/usr/local/nginx/html/:rw
        - ./php/conf/php.ini:/etc/php/php.ini:rw
        - ./php/log/:/usr/local/php/var/log/:rw
        - /etc/localtime:/etc/localtime:rw
      networks:
        - net-lianni
      restart: always
 
 
#### nginx
  nginx:
      image: centos6:nginx
      container_name: lianni-nginx
      ports:
        - "80:80"
      volumes:
        - ./nginx/www/:/usr/local/nginx/html/:rw
        - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf:rw
        - ./nginx/conf/conf.d/:/etc/nginx/conf.d/:rw
        - ./nginx/log/:/var/log/nginx/:rw
        - /etc/localtime:/etc/localtime:rw
      networks:
        - net-lianni
      restart: always
 
 
#### memcached
  memcached:
      image: centos6:memcached
      container_name: lianni-memcached
      ports:
        - "11211:11211"
      volumes:
        - /etc/localtime:/etc/localtime:rw
      networks:
        - net-lianni
      restart: always
 
 
#### mongodb
  mongodb:
      image: centos6:mongodb
      container_name: lianni-mongodb
      ports:
        - "27017:27017"
      volumes:
        - ./mongodb/conf/mongodb.conf:/etc/mongodb/mongodb.conf:rw
        - ./mongodb/data/:/mongodb/data/:rw
        - ./mongodb/log:/mongodb/log/:rw
        - /etc/localtime:/etc/localtime:rw
      networks:
        - net-lianni
      restart: always
 
 
#### redis
  redis:
      image: centos6:redis
      container_name: lianni-redis
      ports:
        - "6379:6379"
      volumes:
        - ./redis/conf/redis.conf:/etc/redis/redis.conf:rw
        - ./redis/data/:/data/redis/:rw
        - ./redis/log/:/data/log/:rw
        - /etc/localtime:/etc/localtime:rw
      networks:
        - net-lianni
      restart: always
 
#### networks
networks:
  net-lianni:

 

三、创建相关配置文件(这些配置文件根据自己实际环境进行创建即可,这里我就不贴出来了)

1、创建mysql的my.cnf

vim /data/docker/lnmp-test/dockerfile/mysql/conf/my.cnf

2、创建php的php.ini

vim /data/docker/lnmp-test/dockerfile/php/conf/php.ini

3、创建nginx的nginx.conf

vim /data/docker/lnmp-test/dockerfile/nginx/conf/nginx.conf

4、创建mongodb的mongodb.conf

vim /data/docker/lnmp-test/dockerfile/mongodb/conf/mongodb.conf

5、创建redis的redis.conf

vim /data/docker/lnmp-test/dockerfile/redis/conf/redis.conf

 

四、运行docker-compose(如果之前创建过和docker-compose配置的一样的容器名称,请先把之前创建的容器删除)

docker-compose -f /data/docker/lnmp-test/dockerfile/docker-compose.yml up -d

 

五、查看一下docker-compose是否都创建好了容器

方法一:docker ps

方法二:

cd /data/docker/lnmp-test/dockerfile/
docker-compose ps

 

六、创建phpinfo验证php和nginx的整合

1、创建文件

cd /data/docker/lnmp-test/dockerfile/nginx/www/
vim index.php
<?php
        phpinfo();
?>

2、修改nginx配置文件

cd /data/docker/lnmp-test/dockerfile/nginx/conf/
vim nginx.conf
location / {
            root   html;
            index index.php index.html index.htm;
        }
……
 
        location ~ \.php$ {                                                                                                                                                                                                                                  
            root           html;                                                                                                                                                                                                                             
            fastcgi_pass   lnmptest-php:9000;                                                                                                                                                                                                                
            fastcgi_index  index.php;                                                                                                                                                                                                                        
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;                                                                                                                                                                       
            include        fastcgi_params;                                                                                                                                                                                                                   
        } 

3、浏览器访问验证

 

七、dockerfile创建镜像的时候,已经把php的各种扩展安装上了,现在我们来启用一下这些扩展

1、修改配置文件

cd /data/docker/lnmp-test/dockerfile/php/conf/
vim php.ini
[igbinary]
extension=igbinary.so
 
[memcached]
extension=memcached.so
 
[redis]
extension=redis.so
 
[imagick]
extension=imagick.so
 
[bcmath]
extension=bcmath.so
 
[apcu]
extension=apcu.so
 
[phalcon]
extension=phalcon.so
 
[mongodb]
extension = mongodb.so

2、浏览器访问是否已经启用那些插件

 

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

 

八、验证使用客户端是否能连接上我们的数据库

技术分享图片

技术分享图片

 

九、重启服务器,验证这些容器是否会自动启动

reboot

 

posted @ 2021-06-07 21:40  Mr_伍先生  阅读(470)  评论(0编辑  收藏  举报