docker-compose

安装

yum install docker
yum install python-pip -y
yum -y install epel-release
yum install python-pip
pip install --upgrade pip
pip install docker-compose

安装完成测试

docker-compose --version
docker-compose version 1.24.1, build 4667896

创建 docker-compose.yml

version: "3"
services:
  redis:
    image: redis
    container_name: my_redis
    privileged: true
    command: redis-server --requirepass 123456   --appendonly yes
    ports:
      - "6379:6379"
    volumes:
      - ./redis/data:/data
  db:
    image: mysql
    container_name: mysql-db
    privileged: true
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci 
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_USER: test  
      MYSQL_PASSWORD: test
    ports:
      - '3306:3306'
    volumes:
      - "./db/data:/var/lib/mysql"   
  zoo1:
    image: zookeeper
    restart: always
    hostname: zoo1
    container_name: zookeeper_1
    #domainname: 
    privileged: true
    ports:
      - 2181:2181
    volumes:
      - ./zookeeper/zoo1/data:/data
      - ./zookeeper/zoo1/datalog:/datalog
      - ./zookeeper/conf:/conf
    environment:
      ZOO_MY_ID: 1
      ZOO_PORT: 2181
      ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

  zoo2:
    image: zookeeper
    restart: always
    hostname: zoo2
    container_name: zookeeper_2
    privileged: true
    ports:
      - 2182:2181
    volumes:
      - ./zookeeper/zoo2/data:/data
      - ./zookeeper/zoo2/datalog:/datalog
      - ./zookeeper/conf:/conf
    environment:
      ZOO_MY_ID: 2
      ZOO_PORT: 2181
      ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

  zoo3:
    image: zookeeper
    restart: always
    hostname: zoo3
    container_name: zookeeper_3
    privileged: true
    ports:
      - 2183:2181
    volumes:
      - ./zookeeper/zoo3/data:/data
      - ./zookeeper/zoo3/datalog:/datalog
      - ./zookeeper/conf:/conf
    environment:
      ZOO_MY_ID: 3
      ZOO_PORT: 2181
      ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
  broker1:
    image: wurstmeister/kafka
    restart: always
    container_name: broker1
    ports:
      - 9091:9092
    depends_on:
      - zoo1
      - zoo2
      - zoo3
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ADVERTISED_HOST_NAME: broker1
      KAFKA_ADVERTISED_PORT: 9092
      KAFKA_HOST_NAME: broker1
      KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
      KAFKA_LISTENERS: PLAINTEXT://broker1:9092
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker1:9092
  broker2:
    image: wurstmeister/kafka
    restart: always
    container_name: broker2
    ports:
      - 9092:9092
    depends_on:
      - zoo1
      - zoo2
      - zoo3
    environment:
      KAFKA_BROKER_ID: 2
      KAFKA_ADVERTISED_HOST_NAME: broker2
      KAFKA_ADVERTISED_PORT: 9092
      KAFKA_HOST_NAME: broker2
      KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
      KAFKA_LISTENERS: PLAINTEXT://broker2:9092
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker2:9092
  broker3:
    image: wurstmeister/kafka
    restart: always
    container_name: broker3
    ports:
      - 9093:9092
    depends_on:
      - zoo1
      - zoo2
      - zoo3
    environment:
      KAFKA_BROKER_ID: 3
      KAFKA_ADVERTISED_HOST_NAME: broker3
      KAFKA_ADVERTISED_PORT: 9092
      KAFKA_HOST_NAME: broker3
      KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
      KAFKA_LISTENERS: PLAINTEXT://broker3:9092
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker3:9092
  kafka-manager:
    container_name: kafka-manager
    image: sheepkiller/kafka-manager
    restart: always
    ports:
      - 9000:9000
    environment:
      KM_VERSION: 1.3.3.18
      ZK_HOSTS: zoo1:2181,zoo2:2182,zoo3:2183

镜像
image: redis
容器名
container_name: my_redis
centos中的selinux会报没有权限的问题,给docker给个特殊权限
privileged: true
启动redis的参数
command: redis-server --requirepass 123456 --appendonly yes
端口映射
ports:
- “6379:6379”
将宿主机目录挂载到容器中的文件夹中
volumes:
- ./redis/data:/data
其中zk启动的时候配置ZOO_PORT: 2181没有生效,需要手动配置zk配置文件
cat zookeeper/conf/zoo.cfg

dataDir=/data
dataLogDir=/datalog
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=60
standaloneEnabled=true
admin.enableServer=true
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888
clientPort=2181
4lw.commands.whitelist=*

由于覆盖了/conf下面的文件,故需要把log4j文件复制出来
cat zookeeper/conf/log4j.properties


zookeeper.root.logger=INFO, CONSOLE

zookeeper.console.threshold=INFO

zookeeper.log.dir=.
zookeeper.log.file=zookeeper.log
zookeeper.log.threshold=INFO
zookeeper.log.maxfilesize=256MB
zookeeper.log.maxbackupindex=20

zookeeper.tracelog.dir=${zookeeper.log.dir}
zookeeper.tracelog.file=zookeeper_trace.log

log4j.rootLogger=${zookeeper.root.logger}

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold}
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n

log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold}
log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}
log4j.appender.ROLLINGFILE.MaxFileSize=${zookeeper.log.maxfilesize}
log4j.appender.ROLLINGFILE.MaxBackupIndex=${zookeeper.log.maxbackupindex}
log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n

log4j.appender.TRACEFILE=org.apache.log4j.FileAppender
log4j.appender.TRACEFILE.Threshold=TRACE
log4j.appender.TRACEFILE.File=${zookeeper.tracelog.dir}/${zookeeper.tracelog.file}

log4j.appender.TRACEFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.TRACEFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L][%x] - %m%n

启动的时候会遇到docker防火墙的问题
解决: 修改/etc/sysconfig/docker里的OPTIONS增加–iptables=false,重启docker服务,再启动redis,顺利开启。
常用命令:
docker-compose up -d 创建并且启动容器
docker-compose stop 停止所有服务
docker-compose rm 删除所有容器
docker-compose start(stop) 后面加容器名可开始或者停止某个服务(跟docker启停没有什么区别)

你可能感兴趣的:(docker)