Docker-compose.yml文件示例
1、mysql主从复制的docker-compose.yml文件
# cat docker-compose.yml
version: ‘2‘ # 这个version是指dockerfile解析时用的版本,不是给我们自己定义版本号用的.
services:
m1: # master
build: ./master # ./master文件下需要有Dockerfile文件,并且build属性和image属性不能一起使用
container_name: m1 # 容器名
volumes: # 挂载 下边每行前边的`-`代表这个东西是数组的一个元素.就是说volumes属性的值是一个数组
- /home/ssab/config/mysql-master/:/etc/mysql/:ro # 注意改下映射关系
- /etc/localtime:/etc/localtime:ro
- /home/ssab/config/hosts:/etc/hosts:ro # 注意改下映射关系
networks: # 网络
mysql: # 见跟services平级的networks,在最下边
ipv4_address: 172.18.0.2 # 设置静态ipv4的地址
ulimits: # 操作系统限制
nproc: 65535
hostname: m1 # hostname
mem_limit: 1024m # 最大内存使用不超过1024m,我在本地机器上测试,才只写了1024m,生产上需要根据自己的服务器配置,以及docker容器数进行调优.
restart: always # 容器重启策略
environment: # 设置环境变量
MYSQL_ROOT_PASSWORD: m1test
s1: # slave1
build: ./s1
container_name: s1
volumes:
- /home/ssab/config/mysql-s1/:/etc/mysql/:ro
- /etc/localtime:/etc/localtime:ro
- /home/ssab/config/hosts:/etc/hosts:ro
networks:
mysql:
ipv4_address: 172.18.0.3
links:
- m1
ulimits:
nproc: 65535
hostname: s1
mem_limit: 1024m
restart: always
environment:
MYSQL_ROOT_PASSWORD: s1test
s2:# slave2
build: ./s2
container_name: s2
volumes:
- /home/ssab/config/mysql-s2/:/etc/mysql/:ro
- /etc/localtime:/etc/localtime:ro
- /home/ssab/config/hosts:/etc/hosts:ro
links:
- m1
networks:
mysql:
ipv4_address: 172.18.0.4
ulimits:
nproc: 65535
hostname: s2
mem_limit: 1024m
restart: always
environment:
MYSQL_ROOT_PASSWORD: s2test
networks: # docker网络设置
mysql: # 自定义网络名称
driver: bridge # 桥接
ipam: # 要使用静态ip必须使用ipam插件
driver: default
config:
- subnet: 172.18.0.0/24
gateway: 172.18.0.1
2、mycat+mysql读写分离的docker-compose.yml文件:
# cat dicker-compose.yml
version:‘2‘
services:
m1:
build:
./master
container_name:
m1
volumes:
-
/home/ssab/config/mysql-master/
:/etc/mysql/:ro
-
/etc/localtime:/etc/localtime:ro
-
/home/ssab/config/hosts:/etc/hosts:ro
ports:
-
"3309:3306"#暴露mysql的端口
networks:
mysql:
ipv4_address:
172.18
.
0.
2
ulimits:
nproc:
65535
hostname:
m1
mem_limit:
1024
m
restart:
always
environment:
MYSQL_ROOT_PASSWORD:
m1test
s1:
build:
./s1
container_name:
s1
volumes:
-
/home/ssab/config/mysql-s1/
:/etc/mysql/:ro
-
/etc/localtime:/etc/localtime:ro
-
/home/ssab/config/hosts:/etc/hosts:ro
ports:
-
"3307:3306"
networks:
mysql:
ipv4_address:
172.18
.
0.
3
links:
- m1
ulimits:
nproc:
65535
hostname:
s1
mem_limit:
1024
m
restart:
always
environment:
MYSQL_ROOT_PASSWORD:
s1test
s2:
build:
./s2
container_name:
s2
volumes:
-
/home/ssab/config/mysql-s2/
:/etc/mysql/:ro
-
/etc/localtime:/etc/localtime:ro
-
/home/ssab/config/hosts:/etc/hosts:ro
ports:
-
"3308:3306"
links:
- m1
networks:
mysql:
ipv4_address:
172.18
.
0.
4
ulimits:
nproc:
65535
hostname:
s2
mem_limit:
1024
m
restart:
always
environment:
MYSQL_ROOT_PASSWORD:
s2test
mycat:
# 设置mycat
build:
./mycat
container_name:
mycat
volumes:
-
/home/ssab/config/mycat/:/mycat/conf/:ro# mycat配置文件
-
/home/ssab/config/mycat-logs/
:/mycat/logs/:rw# mycat日志文件
-
/etc/localtime:/etc/localtime:ro
-
/home/ssab/config/hosts:/etc/hosts:ro
ports:
-
"8066:8066"# 暴露mycat服务端口
-
"9066:9066"# 暴露mycat管理端口
links:
# mycat可以连接m1 s1 s2
- m1
- s1
- s2
networks:
mysql:
ipv4_address:
172.18
.
0.
5
ulimits:
nproc:
65535
hostname:
mycat
mem_limit:
1024
m
restart:
always
networks:
mysql:
driver:
bridge
ipam:
driver:
default
config:
-
subnet:172.18
.
0.
0/
24
gateway:
172.18
.
0.
1
mysql的Dockerfile样例:
FROMmysql:5.7.17
MAINTAINER<
ssabwork_wjj@163.com>
EXPOSE 3306
CMD ["mysqld"]
mycat的Dockerfile样例:
FROM java:
8-jre
MAINTAINER
<ssab [email protected]
163.com
>
LABEL Description
="使用mycat做mysql数据库的读写分离"
ENV mycat
-versionMycat
-server-1.6-RELEASE-20161028204710-linux.tar
.gz
USER root
COPY
./Mycat
-server-1.6-RELEASE-20161028204710-linux.tar
.gz
/
RUN tar
-zxf/Mycat
-server-1.6-RELEASE-20161028204710-linux.tar
.gz
ENV MYCAT_HOME
=/mycat
ENV PATH
=$PATH:
$MYCAT_HOME/bin
WORKDIR
$MYCAT_HOME/bin
RUN chmod u
+x
./mycat
EXPOSE
80669066
CMD
["./mycat",
"console"]