Skip to content

Commit

Permalink
DIP_CURRENT_USER added (#168)
Browse files Browse the repository at this point in the history
* Expose current user UID as an environment variable

* Update README

* Add an example of using the current user in a docker container

* Change up the example
  • Loading branch information
willtcarey authored Feb 28, 2024
1 parent 112d8fe commit 3d32d14
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,27 @@ dip run bash -c pwd

returned is `/app/sub-project-dir`.

#### $DIP_CURRENT_USER

Exposes the current user ID (UID). It is useful when you need to run a container with the same user as the host machine. For example:

```yml
# dip.yml (1)
environment:
UID: ${DIP_CURRENT_USER}
```

```yml
# docker-compose.yml (2)
services:
app:
image: ruby
user: ${UID:-1000}
```

The container will run using the same user ID as your host machine.


### dip run

Run commands defined within the `interaction` section of dip.yml
Expand Down
6 changes: 5 additions & 1 deletion lib/dip/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Dip
class Environment
VAR_REGEX = /\$\{?(?<var_name>[a-zA-Z_][a-zA-Z0-9_]*)\}?/.freeze
SPECIAL_VARS = %i[os work_dir_rel_path].freeze
SPECIAL_VARS = %i[os work_dir_rel_path current_user].freeze

attr_reader :vars

Expand Down Expand Up @@ -63,5 +63,9 @@ def find_os
def find_work_dir_rel_path
@find_work_dir_rel_path ||= Pathname.getwd.relative_path_from(Dip.config.file_path.parent).to_s
end

def find_current_user
@find_current_user ||= Process.uid
end
end
end

0 comments on commit 3d32d14

Please sign in to comment.