12

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?

matchish
  • 2,172
  • 3
  • 15
  • 32

2 Answers2

22

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
matchish
  • 2,172
  • 3
  • 15
  • 32
  • 1
    This solution solved for me. Thanks! I just changed by setting like this: `docker run -u ${CURRENT_UID}:${CURRENT_UID} ... ` – Ricardo M S 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
3

Another option is to use env:

Makefile:

SHELL=/bin/bash

UID := $(shell id -u)

up:
    env UID=${UID} docker-compose up -d
Eddie
  • 1,266
  • 5
  • 22
  • 40