使用docker部署redis

本文详细介绍了如何在Docker容器中部署Flask应用和Redis缓存服务,探讨了在同一容器内运行两者的局限性,并提供了通过不同容器及网络设置实现连接的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近一个小项目用到了flask+redis做数据可视化,其中redis作为数据缓存,加速数据读取速度。
可以使用传统的直接安装的方式进行部署:

直接安装

>>> sudo apt install redis-server
>>> pip3 install redis

经测试该方案可行√

考虑到架构不复杂,想尝试把这套系统全部放在一个container中:

安装在一个container中

>>> docker run -p 5000:5000 --name wuhan -h wuhan --restart=always -d ubuntu_arm64:basic_env
>>> docker exec -it wuhan /bin/zsh
>>> sudo apt install redis-server
>>> pip3 install redis
>>> service redis-server status
 * redis-server is not running
>>> redis-server
252:C 14 Feb 17:49:32.579 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
252:C 14 Feb 17:49:32.579 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=252, just started
252:C 14 Feb 17:49:32.580 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 252
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

252:M 14 Feb 17:49:32.583 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
252:M 14 Feb 17:49:32.583 # Server initialized
252:M 14 Feb 17:49:32.583 # 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.
252:M 14 Feb 17:49:32.584 * Ready to accept connections
  • -p 5000:5000: 将物理的5000端口映射到container的5000端口上
  • –name wuhan: 将container命名为wuhan
  • -h wuhan: 将启动的系统hostname设置为wuhan
  • –restart=always: docker重启后自动启动container
  • -d: 为容器重新分配一个伪输入终端、后台运行容器并返回容器ID

可以看到,redis-server无法以守护进程的方式启动,只能在前台启动,因而目前没有办法将redis和flask放在一个container中。如果有办法,恳请告诉我~~

由于无法在同一个container同时运行flask和redis,因而就尝试将两者分别放在两个container中:

分别安装在两个container中

官方提供有redis的images,直接run就会自动pull下来

>>> docker run --name my-redis-container -d redis

以link的方式连接到redis container(推荐)

>>> docker run -p 5000:5000 --name wuhan --link my-redis-container:redis -h wuhan --restart=always -d wuhan_bigdata

注意,在使用redis的python接口时,要把地址换成redis的container名

>>> import redis
>>> pool = redis.ConnectionPool(host='my-redis-container', port=6379, decode_responses=True)
>>> rd = redis.Redis(connection_pool=pool)

否则在写入的时候会报错:

  File "/usr/local/lib/python3.6/dist-packages/redis/client.py", line 1766, in set
    return self.execute_command('SET', *pieces)
  File "/usr/local/lib/python3.6/dist-packages/redis/client.py", line 875, in execute_command
    conn = self.connection or pool.get_connection(command_name, **options)
  File "/usr/local/lib/python3.6/dist-packages/redis/connection.py", line 1185, in get_connection
    connection.connect()
  File "/usr/local/lib/python3.6/dist-packages/redis/connection.py", line 557, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 99 connecting to localhost:6379. Cannot assign requested address.

参考:
https://www.ionos.com/community/hosting/redis/using-redis-in-docker-containers/

设置docker network方式(不推荐)

该方式将两个container置于同一个网络中,因而可以连接到redis,否则依旧是上面那个错误,具体设置方式可以看这个stackoverflow,精髓就是

>>> docker network create xxxx
>>> docker run --network=xxxx ...

当然,也需要将localhost换成redis container名

Redis在容器中, 在容器外(非容器)访问

# 创建redis.conf
>>> vim /Users/microfat/Git/test/redis/redis.conf
...
#bind 127.0.0.1 -::1
bind 0.0.0.0
...
>>> docker run --rm -p 6379:6379 -v /Users/microfat/Git/test/redis:/usr/local/etc/redis --name myredis redis redis-server /usr/local/etc/redis/redis.conf
>>> import redis
>>> rd = redis.Redis(host='0.0.0.0', port=6379, db=0)
>>> rd.set('foo', 'bar', ex=10)

在这里插入图片描述
参考:https://stackoverflow.com/a/53259929/7151777

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值