我正在使用 Docker Compose 运行 MongoDB 容器,并遇到与 tcmalloc 和 vm.max_map_count 相关的启动警告。我已经在主机上正确设置了这些值,但容器似乎没有继承它们。
我的 Docker Compose 文件:
mongo:
build: image
container_name: mongo
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: TO_CHANGE
ports:
- 0.0.0.0:27017:27017
volumes:
- mongo_data:/data/db
- mongo_ssl_data:/etc/letsencrypt
- keys:/keys
command: >
mongod --tlsMode allowTLS
--tlsCertificateKeyFile /etc/letsencrypt/live/db0.starknet.id/cert.pem
--tlsCAFile /etc/letsencrypt/live/db0.starknet.id/fullchain.pem
--tlsAllowConnectionsWithoutCertificates
--replSet rs0
--keyFile /keys/mongo_keyfile
depends_on:
certbot:
condition: service_healthy
我的 Dockerfile:
FROM mongo:8.0.0
ENV GLIBC_TUNABLES=glibc.pthread.rseq=0
我在我的主机(ubuntu 24)上设置了以下内容:
root@ashur ~ # sysctl vm.max_map_count
vm.max_map_count = 262144
root@ashur ~ # cat /sys/kernel/mm/transparent_hugepage/enabled
always [madvise] never
root@ashur ~ # cat /proc/sys/vm/swappiness
0
The server generated these startup warnings when booting
2024-09-21T09:22:31.052+00:00: For customers running the tcmalloc-google memory allocator, we suggest setting the contents of sysfsFile to 'always'
2024-09-21T09:22:31.052+00:00: For customers running the updated tcmalloc-google memory allocator, we suggest setting the contents of sysfsFile to 'defer+madvise'
2024-09-21T09:22:31.052+00:00: We suggest setting the contents of sysfsFile to 0.
2024-09-21T09:22:31.052+00:00: vm.max_map_count is too low
为什么这些设置没有被容器继承,我该如何解决这些警告?我尝试在 Dockerfile 中设置它们,但这似乎不是合理的解决方案。
您遇到的问题是由于主机上的某些内核参数和系统设置不会自动被 Docker 容器继承。
添加 sysctls 来设置 vm.max_map_count 以及在 dockercompose 文件上禁用透明大页的命令
sysctls:
- vm.max_map_count=262144
检查正在运行的容器内的值。
docker exec -it mongo sysctl vm.max_map_count
docker exec -it mongo cat /sys/kernel/mm/transparent_hugepage/enabled
注意: Docker Compose 3.8 及更高版本支持 sysctls 选项。