Linux – How to set environment variables for a different user

dockerlinuxUbuntu

I'm trying to get 2 docker containers started and linked together with a rails app. This app relies on these environment variables:

POSTGRES_PORT_5432_TCP_ADDR
POSTGRES_PORT_5432_TCP_PORT

When I do env as root I'm getting:

POSTGRES_PORT_5432_TCP_PORT=5432
POSTGRES_PORT_5432_TCP_ADDR=172.17.0.2

When I run it under root I'm not getting any problem and it works. However the application runs as app user. When I do env as app:

root@f80f94948393:/home/app# sudo -u app env

I'm not seeing those variables. I googled around and have altered these files already:

/etc/environment

POSTGRES_PORT_5432_TCP_ADDR="172.17.0.2"
POSTGRES_PORT_5432_TCP_PORT="5432"

/home/app/.profile

POSTGRES_PORT_5432_TCP_PORT="5432";export POSTGRES_PORT_5432_TCP_PORT
POSTGRES_PORT_5432_TCP_ADDR="172.17.0.2";export POSTGRES_PORT_5432_TCP_ADDR

/home/app/.bashrc

POSTGRES_PORT_5432_TCP_PORT="5432";export POSTGRES_PORT_5432_TCP_PORT
POSTGRES_PORT_5432_TCP_ADDR="172.17.0.2";export POSTGRES_PORT_5432_TCP_ADDR

But nothing seems to help. The variables aren't showing up with app user.

What am I missing here?

Best Answer

I do not know how you run the program as non-root, bu when you use sudo, you may use -E (see: man sudo) that should preserve env.

Otherwise you may add those variables into system-wide .bashrc (/etc/bash.bashrc) if you use bash at all.