docker-compose實現redis部署及鍵值添加

爲了簡化部署過程,減小手工操做,研究出使用docker-compose方式實現redis部署並往該redis中添加鍵值
如下爲我編寫的docker-compose.yml文件的內容redis

#vi /opt/docker-compose.yml
version: '3'
services:
  redis:
    image:  redis:4.0
    ports:
      - "16379:6379"
    environment:
      - TZ="Asia/Shanghai"
    volumes:
      - /opt/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /opt/redis/data:/data
  rediscli:
    image:  redis:4.0
    command: redis-cli -h redis -p 6379  -c hset REDIS_USER:admin REDIS_USER admin
    depends_on:
      - redis

networks:
  redis_net:
    ipam:
      driver: default

如下爲操做過程執行的docker-compose的log輸出:
倒數第二行的內容rediscli_1 | 1便是往redis容器中操做redis-cli添加鍵值信息docker

[root@192-83 ~]$ docker-compose -f /opt/docker-compose.yml up
WARNING: Some networks were defined but are not used by any service: redis_net
Creating network "deploy_default" with the default driver
Creating deploy_redis_1 ... done
Creating deploy_rediscli_1 ... done
Attaching to deploy_redis_1, deploy_rediscli_1
redis_1     | 1:C 16 Jan 03:30:23.404 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1     | 1:C 16 Jan 03:30:23.405 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1     | 1:C 16 Jan 03:30:23.405 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1     | 1:M 16 Jan 03:30:23.407 * Running mode=standalone, port=6379.
redis_1     | 1:M 16 Jan 03:30:23.407 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1     | 1:M 16 Jan 03:30:23.407 # Server initialized
redis_1     | 1:M 16 Jan 03:30:23.407 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1     | 1:M 16 Jan 03:30:23.408 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will
 create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
 redis_1     | 1:M 16 Jan 03:30:23.408 * Ready to accept connections
rediscli_1  | 1
deploy_rediscli_1 exited with code 0

docker-compose實現redis部署及鍵值添加
其中deploy_rediscli_1這個容器的做用就是往deploy_redis_1容器中添加鍵值,完成任務即退出,經過redis客戶端工具,也能夠看到添加的鍵值成功
docker-compose實現redis部署及鍵值添加shell

方法二:修改docker-compose.yml文件,使用shell腳本實現ide

#vi /opt/docker-compose.yml
version: '3'
services:
  redis:
    image:  redis:4.0
    ports:
      - "16379:6379"
    environment:
      - TZ="Asia/Shanghai"
    volumes:
      - /opt/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /opt/redis/data:/data
    command: /usr/local/bin/redis-server /usr/local/etc/redis/redis.conf

networks:
  redis_net:
    ipam:
      driver: default
#vi /opt/setkey.sh 
#!/bin/sh
docker-compose -f /opt/docker-compose.yml up -d
if [ $? -eq 0 ]; then
   docker exec -i redis redis-cli -a foobared -c hset REDIS_USER:admin REDIS_USER admin
   if [ $? -eq 0 ];then
     echo "insert success"
   else
     echo "failed"
   fi
else
  echo "comopose up failed"
    docker-compose -f /opt/docker-compose.yml down
fi
相關文章
相關標籤/搜索