#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添加键值信息
[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
其中deploy_rediscli_1这个容器的作用就是往deploy_redis_1容器中添加键值,完成任务即退出,通过redis客户端工具,也可以看到添加的键值成功
方法二:修改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
原文:https://blog.51cto.com/8355320/2467212