Docker-compose管理容器(七)

前言

之前已经写好了lnmp+memcached+mongodb+redis的dockerfile,接下来使用docker-compose来管理这些容器,使他们能串联起来同时为我们提供服务。

iSWmfs.png

一、创建相关目录

1、创建配置文件目录
1
2
3
4
5
6
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的日志目录了)
1
2
3
4
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、创建数据目录
1
2
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网站根目录
1
mkdir -p /data/docker/lnmp-test/dockerfile/nginx/www

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

1
2
cd /data/docker/lnmp-test/dockerfile/
vim docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
1
vim /data/docker/lnmp-test/dockerfile/mysql/conf/my.cnf
2、创建php的php.ini
1
vim /data/docker/lnmp-test/dockerfile/php/conf/php.ini
3、创建nginx的nginx.conf
1
vim /data/docker/lnmp-test/dockerfile/nginx/conf/nginx.conf
4、创建mongodb的mongodb.conf
1
vim /data/docker/lnmp-test/dockerfile/mongodb/conf/mongodb.conf
5、创建redis的redis.conf
1
vim /data/docker/lnmp-test/dockerfile/redis/conf/redis.conf

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

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

iSWKlq.png

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

方法一:docker ps

iSWM60.png

方法二:
1
2
cd /data/docker/lnmp-test/dockerfile/
docker-compose ps

iSWth9.png

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

1、创建文件
1
2
cd /data/docker/lnmp-test/dockerfile/nginx/www/
vim index.php
1
2
3
<?php
phpinfo();
?>
2、修改nginx配置文件
1
2
cd /data/docker/lnmp-test/dockerfile/nginx/conf/
vim nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
        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、浏览器访问验证

iSWBnK.png

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

1、修改配置文件
1
2
cd /data/docker/lnmp-test/dockerfile/php/conf/
vim php.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[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

iSWr7D.png

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

iSW6tH.png

iSWchd.png

iSW29A.png

iSW4nf.png

iSWTAg.png

iSW7NQ.png

iSWL3n.png

iSWXj0.png

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

iSWvuV.png

iSWzHU.png

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

1
reboot

iSfpEF.png

博主QQ:1012405802
技术交流QQ群:830339411
版权声明:网站内容有原创和转载,如有侵权,请联系删除,谢谢!!
感谢打赏,93bok因你们而精彩!!(支付宝支持花呗)
0%