docker搭建NSQ

413 阅读1分钟

Description

docker 创建 nsq 时需要使用实际的 IP,否则使用会有问题,但是每次都修改 compose 里面的地址比较麻烦。因此,使用 shell 脚本动态获取 IP 和创建 compose。

Create docker compose - docekr-compose-base.yml

version: "3"
services:
  nsqlookupd:
    image: nsqio/nsq
    container_name: lookupd
    command: /nsqlookupd
    ports:
      - '4160-4161:4160-4161'
  nsqd:
    image: nsqio/nsq
    container_name: nsqd
    command: /nsqd --broadcast-address=localhost --lookupd-tcp-address=localhost:4160
    depends_on:
      - nsqlookupd
    ports:
      - '4150-4151:4150-4151'
  nsqadmin:
    image: nsqio/nsq
    container_name: nsqadmin
    command: /nsqadmin --lookupd-http-address=localhost:4161
    ports:
      - '4170-4171:4170-4171'

Create start shell - start.sh

#!/bin/bash

IP=`ifconfig en0 | grep inet | grep broadcast | cut -d ' ' -f 2`

if [ $IP ]
then
    echo $IP
else
    IP=`ifconfig en4 | grep inet | grep broadcast | cut -d ' ' -f 2`
    echo $IP
fi

sed -e "s/localhost/$IP/g" docker-compose-base.yml > docker-compose.yml

docker-compose up -d

Create stop shell - stop.sh

#!/bin/bash

docker-compose down

rm docker-compose.yml