title: Getting permission denied even as root inside the docker container
body: <p>Connecting to running docker container as a root still gets Operation not permitted error when trying to <code>apt-get update</code>, yet I can still see sensitive file like <code>/etc/passwd</code>. Below are my configurations and also the error message from <code>apt-get update</code>. My host operating system is <code>Ubuntu 18.04.3</code>. My docker version is <code>Docker version 19.03.5, build 633a0ea838</code></p>
  
  <p>I create a container with the following Dockerfile</p>
  
  <pre><code>FROM python:3.8-slim-buster
  RUN useradd -ms /bin/bash andrej
  WORKDIR /home/andrej
  COPY . /home/andrej/
  
  RUN apt-get update &amp;&amp; \
      apt-get install -y gcc &amp;&amp; \
      pip install -r requirements.txt &amp;&amp; \
      apt-get remove -y gcc &amp;&amp; apt-get -y autoremove
  
  RUN chown andrej:andrej pycurl &amp;&amp; \
      chmod 0744 pycurl
  
  USER andrej
  ENTRYPOINT ["uwsgi"]
  CMD ["--ini", "uwsgi.ini"]
  
  </code></pre>
  
  <p>starting it with docker compose looking like this:</p>
  
  <pre><code>version: "3.3"
  
  services:
  
    andrej-cv:
      build: ./andrej_cv
      container_name: andrej-cv
      restart: always
      security_opt:
        - no-new-privileges
      expose:
        - 5000
      healthcheck:
        test: ./pycurl --host=127.0.0.1 --port=5050 --uri=/health_check
        interval: 1m30s
        timeout: 10s
        retries: 3
  </code></pre>
  
  <p>My docker daemon config:</p>
  
  <pre><code>{
      "icc": false,
      "userns-remap": "default",
      "log-driver": "syslog",
      "live-restore": true,
      "userland-proxy": false,
      "no-new-privileges": true
  }
  
  </code></pre>
  
  <p>I connect to the container with following command (as root):
  <code>docker exec -it -u root &lt;container_hash&gt; /bin/bash</code> but when I try to update I got the following:</p>
  
  <pre><code>root@ed984abff684:/home/andrej# apt-get update 
  E: setgroups 65534 failed - setgroups (1: Operation not permitted)
  E: setegid 65534 failed - setegid (1: Operation not permitted)
  E: seteuid 100 failed - seteuid (1: Operation not permitted)
  E: setgroups 0 failed - setgroups (1: Operation not permitted)
  Hit:1 http://deb.debian.org/debian buster InRelease
  Ign:2 http://deb.debian.org/debian buster-updates InRelease
  Err:4 http://deb.debian.org/debian buster-updates Release
    Could not open file /var/lib/apt/lists/partial/deb.debian.org_debian_dists_buster-updates_Release - open (13: Permission denied) [IP: 151.101.36.204 80]
  Hit:3 http://security-cdn.debian.org/debian-security buster/updates InRelease
  rm: cannot remove '/var/cache/apt/archives/partial/*.deb': Permission denied
  Reading package lists... Done
  W: chown to _apt:root of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
  W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
  W: chown to _apt:root of directory /var/lib/apt/lists/auxfiles failed - SetupAPTPartialDirectory (1: Operation not permitted)
  W: chmod 0700 of directory /var/lib/apt/lists/auxfiles failed - SetupAPTPartialDirectory (1: Operation not permitted)
  E: setgroups 65534 failed - setgroups (1: Operation not permitted)
  E: setegid 65534 failed - setegid (1: Operation not permitted)
  E: seteuid 100 failed - seteuid (1: Operation not permitted)
  W: Download is performed unsandboxed as root as file '/var/lib/apt/lists/partial/deb.debian.org_debian_dists_buster_InRelease' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)
  E: setgroups 0 failed - setgroups (1: Operation not permitted)
  W: Problem unlinking the file /var/lib/apt/lists/partial/deb.debian.org_debian_dists_buster_InRelease - PrepareFiles (13: Permission denied)
  W: Problem unlinking the file /var/lib/apt/lists/partial/deb.debian.org_debian_dists_buster-updates_InRelease - PrepareFiles (13: Permission denied)
  W: Problem unlinking the file /var/lib/apt/lists/partial/deb.debian.org_debian_dists_buster-updates_Release - PrepareFiles (13: Permission denied)
  E: The repository 'http://deb.debian.org/debian buster-updates Release' no longer has a Release file.
  N: Updating from such a repository can't be done securely, and is therefore disabled by default.
  N: See apt-secure(8) manpage for repository creation and user configuration details.
  W: Problem unlinking the file /var/lib/apt/lists/partial/security.debian.org_debian-security_dists_buster_updates_InRelease - PrepareFiles (13: Permission denied)
  </code></pre>
  
  <p>In the <strong>container</strong> <code>/etc/subuid</code> and <code>/etc/subgid</code> look like this (both):</p>
  
  <pre><code>andrej:100000:65536
  </code></pre>
  
  <p>On the <strong>host</strong> <code>/etc/subuid</code> and <code>/etc/subgid</code> look like this (both):</p>
  
  <pre><code>andrej:100000:65536
  dockremap:165536:65536
  </code></pre>
  
  <p>Apparmor is running on Ubuntu host with following status (only <code>docker-default</code> profile):</p>
  
  <pre><code>andrej@machine:/etc/apparmor.d$ sudo aa-status 
  apparmor module is loaded.
  38 profiles are loaded.
  36 profiles are in enforce mode.
     /sbin/dhclient
     /snap/core/8268/usr/lib/snapd/snap-confine
     /snap/core/8268/usr/lib/snapd/snap-confine//mount-namespace-capture-helper
     /usr/bin/evince
     /usr/bin/evince-previewer
     /usr/bin/evince-previewer//sanitized_helper
     /usr/bin/evince-thumbnailer
     /usr/bin/evince//sanitized_helper
     /usr/bin/man
     /usr/lib/NetworkManager/nm-dhcp-client.action
     /usr/lib/NetworkManager/nm-dhcp-helper
     /usr/lib/connman/scripts/dhclient-script
     /usr/lib/cups/backend/cups-pdf
     /usr/lib/snapd/snap-confine
     /usr/lib/snapd/snap-confine//mount-namespace-capture-helper
     /usr/sbin/cups-browsed
     /usr/sbin/cupsd
     /usr/sbin/cupsd//third_party
     /usr/sbin/ippusbxd
     /usr/sbin/tcpdump
     docker-default
     libreoffice-senddoc
     libreoffice-soffice//gpg
     libreoffice-xpdfimport
     man_filter
     man_groff
     snap-update-ns.core
     snap-update-ns.gnome-calculator
     snap-update-ns.gnome-characters
     snap-update-ns.gnome-logs
     snap-update-ns.gnome-system-monitor
     snap.core.hook.configure
     snap.gnome-calculator.gnome-calculator
     snap.gnome-characters.gnome-characters
     snap.gnome-logs.gnome-logs
     snap.gnome-system-monitor.gnome-system-monitor
  2 profiles are in complain mode.
     libreoffice-oopslash
     libreoffice-soffice
  17 processes have profiles defined.
  14 processes are in enforce mode.
     docker-default (1101) 
     docker-default (1102) 
     docker-default (1111) 
     docker-default (1600) 
     docker-default (1728) 
     docker-default (1729) 
     docker-default (1730) 
     docker-default (1731) 
     docker-default (1732) 
     docker-default (1798) 
     docker-default (1799) 
     docker-default (1800) 
     docker-default (1801) 
     docker-default (1802) 
  0 processes are in complain mode.
  3 processes are unconfined but have a profile defined.
     /sbin/dhclient (491) 
     /usr/sbin/cups-browsed (431) 
     /usr/sbin/cupsd (402) 
  </code></pre>
  
  <p>Selinux seems to be disabled as there is not <code>/etc/selinux/config</code> file and <code>getenfoce</code> and <code>sestatus</code> command are not available.</p>
  
  <p>Also <code>su andrej</code> command run as root (where andrej is unprivileged user in the container) errors out with <code>su: cannot set groups: Operation not permitted</code></p>