16

I trying to pass the current user id into docker-compose.yml

How it looks in docker-compose.yml

version: '3.4'

services:
    app:
        build:
            context: ./
            target: "php-${APP_ENV}"
        user: "${CURRENT_UID}"

Instead of CURRENT_UID=$(id -u):$(id -g) docker-compose up -d I've wrote makefile

#!/usr/bin/make

SHELL = /bin/sh

up: 
    export CURRENT_UID=$(id -u):$(id -g)
    docker-compose up -d

But CURRENT_UID still empty when I run make up

Is there a possible export uid in makefile?

Serhii Shliakhov
  • 2,631
  • 3
  • 19
  • 37

3 Answers3

33

Here is my solution

#!/usr/bin/make

SHELL = /bin/sh

CURRENT_UID := $(shell id -u)
CURRENT_GID := $(shell id -g)

export CURRENT_UID
export CURRENT_GID

up: 

    docker-compose up -d
Serhii Shliakhov
  • 2,631
  • 3
  • 19
  • 37
  • 1
    This solution solved for me. Thanks! I just changed by setting like this: `docker run -u ${CURRENT_UID}:${CURRENT_UID} ... ` – ricoms Jul 19 '19 at 23:05
  • 1
    What does the SHELL variable do? Bash scripting is case sensitive after all? – goonerify Jun 11 '20 at 18:05
  • 1
    Yes! Been looking for this for over an hour! thanks. Works directly in a Makefile w/o the SHELL = /bin/sh part – newms87 Apr 19 '21 at 16:00
  • This answer https://stackoverflow.com/a/589300/4175647 says what does SHELL variable do – Nick Roz Jun 10 '21 at 16:42
  • What happens if the user has changed their group with `newgrp`. Using `$(shell id -g)` will create a new sub-shell and return the default group not the current group, won't it? – dmbailey Jun 29 '21 at 15:46
  • 1
    @ricoms there is a typo on your comment, it should be ${CURRENT_UID}:${CURRENT_**G**ID} – André Ricardo Aug 01 '23 at 12:05
  • @andré-ricardo, thanks for correction, you are right – ricoms Aug 02 '23 at 13:33
4

Another option is to use env:

Makefile:

SHELL=/bin/bash

UID := $(shell id -u)

up:
    env UID=${UID} docker-compose up -d
Eddie
  • 1,436
  • 5
  • 24
  • 42
0

You need to use 2 dollar signs ($$) before (id -u) and (id -g).

#!/usr/bin/make

SHELL = /bin/sh

up: 
    export CURRENT_UID=$$(id -u):$$(id -g)
    docker-compose up -d
Aidan Gallagher
  • 173
  • 1
  • 7