linux创建redis容器,docker-compose实现redis部署及键值添加

为了简化部署过程,减少手工操作,研究出使用docker-compose方式实现redis部署并往该redis中添加键值

以下为我编写的docker-compose.yml文件的内容

#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添加键值信息

[[email protected] ~]$ 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

96bf9082a28d9ae2a7db00a512798173.png

其中deploy_rediscli_1这个容器的作用就是往deploy_redis_1容器中添加键值,完成任务即退出,通过redis客户端工具,也可以看到添加的键值成功

74e935f113394196ec19867dfd977e27.png

方法二:修改docker-compose.yml文件,使用shell脚本实现

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值