Skip to content

Instantly share code, notes, and snippets.

@pangyuteng
Last active February 5, 2025 02:13
Show Gist options
  • Save pangyuteng/20526ac488b56886b6bdb64e107fe9e5 to your computer and use it in GitHub Desktop.
Save pangyuteng/20526ac488b56886b6bdb64e107fe9e5 to your computer and use it in GitHub Desktop.
prevent docker containers running as root

prevent docker containers running as root

OPTION A. [WIP] use apparmor

  • install
sudo apt install apparmor-profiles

OPTION B. [SORTA WORKING] enable userns-remap for docker

groupadd -g 1001 testuser
useradd testuser -u 1001 -g 1001 -m -s /bin/bash
  • verify user records exists
grep testuser /etc/subuid
grep testuser /etc/subgid
{
  "userns-remap": "testuser"
}
  • restart docker deamon
systemctl restart docker 
  • confirm docker is running as default user

run.py

import time
while True:
    print('ok')
    time.sleep(5)
docker run -w /opt -v ./run.py:/opt/run.py  python:3.10-bullseye python run.py
docker run -u 1000:1000 --userns=host -w /opt -v ./run.py:/opt/run.py  python:3.10-bullseye python run.py
  • check that python is running as default user
ps aux | grep python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment