brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker run --sysctl sets a kernel parameter for one container only

Docker
~/tips/tip-docker-run-sysctl

docker run --rm --sysctl net.ipv4.ip_forward=1 alpine cat /proc/sys/net/ipv4/ip_forward

Changes a namespaced kernel setting inside a single container, without touching the host's settings.

Some images tell you to tweak a kernel setting, and my first reflex is to edit /etc/sysctl.conf on the host. That changes it for everything. For a few settings Docker can scope it to one container instead.

docker run --rm --sysctl net.ipv4.ip_forward=1 alpine cat /proc/sys/net/ipv4/ip_forward

The --sysctl flag takes key=value and can be repeated. In a Compose file it’s a sysctls entry on the service, which the Compose reference allows either as a list of key=value strings or as a map. Typical uses are network settings such as net.ipv4.ip_forward for a container that routes traffic, or net.ipv4.ping_group_range for ping without root.

The caveat is that only namespaced sysctls are allowed. That means the net.* family and a few IPC ones like some kernel.sem and kernel.shm* entries. Settings that are global to the host, like vm.swappiness, are refused, and those still need to be set on the host. The flag also can’t be combined with --network=host or --ipc=host, because then the container would share the host’s namespace and would be changing the host’s value.

This example was not run against a live Docker daemon. It follows the Docker documentation, so no output is shown; check it on your own host first.

Further reading: docker run, runtime constraints and sysctl

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link