title: Vagrant user doesn't have root permissions even with sudo?
body: <p>Disclaimer: I'm looking for solutions that are programmatic. i.e. <code>vagrant up</code> and everything works without any special configuration on the end-user's machine. In other words, all the config should be done on my part in the Vagrantfile, docker-compose, provisioning scripts, etc.</p>
  
  <hr>
  
  <p>Update: I believe, I've located a more primitive manifestation of this problem. If I <code>mkdir somedir</code> I get a directory owned by my user. If I then <code>sudo mkdir someotherdir</code>, it's owned by root. But inside <code>vagrant ssh</code> the same two commands always give a directory owned by vagrant and never root. How can this be worked around?</p>
  
  <hr>
  
  <p>I am trying to get a nextcloud/nginx/mariadb/redis arrangement running with each service inside its own Docker container. The whole thing should be contained inside a virtual machine set up through Vagrant.</p>
  
  <p>Here's my project directory (located someplace inside my home folder):</p>
  
  <pre><code>-Vagrantfile
  -bootstrap.sh
  -nextc/
  |-docker-compose.yml
  |-nginx.conf
  </code></pre>
  
  <p>If I cd into <code>nextc/</code> and run <code>docker-compose up</code>, a directory called <code>persist</code> is created in the root of the project directory (see the compose file below for details).</p>
  
  <pre><code>ls -l
  
  ...
  drwxr-xr-x 5 root     root      4096 авг 12 19:06 persist
  ...
  </code></pre>
  
  <p>The <code>persist</code> contains all stateful stuff, like the configs of nextcloud, database files, and is created due to the docker compose file below:</p>
  
  <pre><code>version: '2.3'
  
  services:
    db:
      image: mariadb
      restart: always
      volumes:
        - ../persist/db/:/var/lib/mysql
      environment:
        - MYSQL_ROOT_PASSWORD=
        - MYSQL_PASSWORD=password
        - MYSQL_DATABASE=nextcloud
        - MYSQL_USER=nextcloud
        - MYSQL_ALLOW_EMPTY_PASSWORD=true
  
    redis:
      image: redis
      volumes: 
        - ../persist/redis/:/data
      expose: 
        - 6379
      restart: always
  
    app:
      image: nextcloud:fpm
      links:
        - db
        - redis
      volumes:
        - ../persist/nextcloud/:/var/www/html
      restart: always
  
    web:
      image: nginx
      ports:
        - "8081:80"
      links:
        - app
        - redis
      volumes:
        - ./nginx.conf:/etc/nginx/nginx.conf:ro
      volumes_from:
        - app
      restart: always
  </code></pre>
  
  <p>Importantly, it is created as belonging to the root user, while some subdirectories inside there belong either to www-data or root. All works as expected and I can access nextcloud over localhost:8081.</p>
  
  <p>Next I set up my <code>Vagrantfile</code> as follows:</p>
  
  <pre><code>Vagrant.configure("2") do |config|
    config.vm.box = "ubuntu/bionic64"
    config.vm.provision :shell, path: "bootstrap.sh"
    config.vm.network "forwarded_port", guest: 8081, host: 8080
  end
  </code></pre>
  
  <p>and <code>bootstrap.sh</code> is</p>
  
  <pre><code>#!/bin/bash
  
  sudo apt-get update
  
  sudo apt-get install -y \
       apt-transport-https \
       ca-certificates \
       curl \
       software-properties-common
  
  curl -fsSL https://download.docker.com/linux/ubuntu/gpg &gt; key
  sudo apt-key add - &lt; key
  rm key
  
  sudo add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
        $(lsb_release -cs) \
        stable"
  
  sudo apt-get update
  
  sudo apt-get install -y docker-ce
  
  sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
  chmod +x /usr/local/bin/docker-compose
  
  cd /vagrant/nextc
  
  sudo docker-compose up -d
  </code></pre>
  
  <p>Since this didn't work as expected, I ssh'd into the vagrant machine and had a look at the output of <code>docker-compose up</code>.</p>
  
  <p>It was <code>MariaDB</code> complaining: many error messages such as </p>
  
  <pre><code>chown: changing ownership of '/var/lib/mysql/ib_logfile0': Operation not permitted
  </code></pre>
  
  <p>or </p>
  
  <pre><code>mkdir cannot create directory '/var/lib/mysql//mysql' permission denied
  </code></pre>
  
  <p>The <code>persist</code> directory is created not as belonging to <code>root</code>, but belonging to Vagrant (viewed inside the VM) or belonging to my user (as seen from the host machine). Clearly this seems to be some kind of permissions issue.</p>
  
  <p>How can I make sure that Vagrant works the same as the host machine?</p>