我有一个无法应对负载的 Docker 容器。
我需要增加值,/proc/sys/net/core/somaxconn
但我无法这样做,因为容器没有在特权模式下运行。
自创建 docker 文件以来,已经对 nignx 和 php 配置进行了几次调整。
是否可以以特权模式重新启动容器而不丢失我已经做的配置更改?
答案1
容器配置位于 /var/lib/docker/containers/<id>/hostconfig.json - 您可以编辑它并重新启动容器,但编辑它时 docker 不应该正在运行。
# docker run -ti --name test fedora:25 /bin/bash
# echo 512 > /proc/sys/net/core/somaxconn # in docker
bash: /proc/sys/net/core/somaxconn: Read-only file system
# exit # exit docker, back to host
# systemctl stop docker # or stop it with whatever servicemanager you're using
# cd /var/lib/docker/containers/b48fcbce0ab29749160e5677e3e9fe07cc704b47e84f7978fa74584f6d9d3c40/
# cp hostconfig.json{,.bak}
# cat hostconfig.json.bak | jq '.Privileged=true' | jq '.SecurityOpt=["label=disable"]' > hostconfig.json
# systemctl start docker
# docker start test
test
# docker exec -ti test /bin/bash
# echo 512 > /proc/sys/net/core/somaxconn # in docker, now works
当您进行更改时,这当然会关闭所有容器。
答案2
不,您不应该直接配置容器。这样做会导致环境难以维护(您已经发现了这一点)。根据需要,将您的配置包含在 docker-compose.yml、附加卷或 Dockerfile 中。这样您就可以通过替换容器来更新容器。
作为参考,docker 允许您在正在运行的容器上更新的唯一设置如下:
$ docker update --help
Usage: docker update [OPTIONS] CONTAINER [CONTAINER...]
Update configuration of one or more containers
Options:
--blkio-weight uint16 Block IO (relative weight), between 10
and 1000, or 0 to disable (default 0)
--cpu-period int Limit CPU CFS (Completely Fair Scheduler)
period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler)
quota
--cpu-rt-period int Limit the CPU real-time period in microseconds
--cpu-rt-runtime int Limit the CPU real-time runtime in
microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--help Print usage
--kernel-memory bytes Kernel memory limit
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap:
'-1' to enable unlimited swap
--restart string Restart policy to apply when a container exits