How to set copy all environment variables from root user to another specific user

In my docker container I am running a command as a specific user like this from entrypoint.sh:

sudo -u appuser "$@"

This works fine, however, it doesn't set any of the environment variables that get created by using the --link option while running the container.


Is it possible to set all environment variables that exist for a root user to some other specific user (in this example appuser)

Note: related question to this discussion. This is the reason I can't just use the USER command How to give non-root user in Docker container access to a volume mounted on the host


The sudo command, because it is designed as a tool for privilege escalation, intentionally sanitizes the environment before switching to a new user id. If you take a look at the sudo man page, you'll find:

-E, --preserve-env Indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the user does not have permission to preserve the environment.

So instead of sudo -u appuser somecommand, just use sudo -E -u appuser somecommand.

The runuser command is provided by the util-linux package in recent versions of Ubuntu, and does not perform any environment initialization by default. For example:

$ docker pull ubuntu $ docker run -it --rm ubuntu /bin/bash root@ded49ffde72e:/# runuser --help Usage: runuser [options] -u <user> <command> runuser [options] [-] [<user> [<argument>...]] [...]

This is with Ubuntu Xenial (but the runuser command also appears to be available on ubuntu:vivid, but is not available under ubuntu:trusty).

So your options are:

    <li>Use sudo -E, or</li> <li>Use a more recent Ubuntu image</li> </ul>


  • What is the difference between Google Developers Console project and a Google **Play** Developers Co
  • Best style for iterating over a small number of items in Python?
  • How to mount volume into the source code of the app?
  • does the FROM directive in a dockerfile allways pull the latest version of an image
  • Change behaviour of Print button in ReportViewer C#
  • Creating new docker-machine instance always fails validating certs using openstack driver
  • How to update docker-compose on Docker for Windows?
  • How do I include screenshots of the full page in my serenity report (and not only of the viewport) u
  • Trying to get the char code of ENTER key
  • Why cepheus don't send int without quotes to orion?
  • Using Sax parsing to edit and write XML in VB6
  • JBoss External Properties Files in Classpath
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Debug.DrawLine not showing in the GameView
  • htaccess add www if not subdomain, if subdomain remove www
  • Yii2: Config params vs. const/define
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Linq Objects Group By & Sum
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Optimizing database types to compact database (SQLite)
  • Validaiting emails with Net.Mail MailAddress
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • Apache 2.4 - remove | delete | uninstall
  • Arrays break string types in Julia
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Buffer size for converting unsigned long to string
  • Benchmarking RAM performance - UWP and C#
  • how does django model after text[] in postgresql [duplicate]
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Cant find why the layout is getting smaller
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • How can i traverse a binary tree from right to left in java?
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF