我正在本地 Windows 计算机上托管的 docker 容器上运行我的 Redis 服务器,但由于此警告,我无法存储数据
WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. 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.
有什么方法可以启用此功能吗?
version: '3'
services:
redis:
image: "redis"
ports:
- "6379:6379"
networks:
- my-service
networks:
my-service:
external: true
首先,它与Redis镜像或容器本身无关,并且没有任何直接的方法可以通过docker-compose或docker run命令来解决它。它仅与主机操作系统相关,在 Windows 情况下是 Linux 虚拟机管理程序,docker 在其之上运行。
如果您像我一样使用 WSL2 后端在 Windows 上运行 docker,则只需启动 WSL 发行版并运行即可临时更改它:
sudo sysctl -w vm.overcommit_memory=1
顺便也不会持久。不幸的是,处理这个问题需要对 Linux 有很好的了解,以应对用户权限、奇怪的访问拒绝、包管理器等,对于我来说,来自 Windows 背景,进一步深入研究这个问题是一项艰巨的任务。
我通过在默认值中添加一行来解决它
entrypoint.sh
仅当 redis 命令参数移至环境变量时才有效
REDIS_ARGS
#!/usr/bin/dumb-init /bin/sh
sysctl vm.overcommit_memory=1
并以
privileged
模式启动容器
这是 Dockerfile
FROM redis/redis-stack-server:7.2.0-v8
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x entrypoint.sh
CMD ["/entrypoint.sh"]
构建新镜像cmd
sudo docker buildx build -t redis/redis-stack-server:7.2.0-v8-fixed .
现在我们可以在例如中使用固定图像compose.yml 文件
这是我的 compose.yml 文件
services:
redis:
image: redis/redis-stack-server:7.2.0-v8-fixed # sysctl vm.overcommit_memory=1
container_name: redis
hostname: redis
restart: unless-stopped
network_mode: host
privileged: true
environment:
REDIS_ARGS: ${REDIS_ARGS}
REDISCLI_AUTH: ${REDIS_PASSWORD}
....