How to set uid and gid in Docker Compose?

BashShellDockerDocker Compose

Bash Problem Overview


I can execute a docker run command as such ...

docker run --rm  --user $(id -u):$(id -g) -e MYDATA=/some/path/to/data -e USER=$USER -p 8883-8887:8883-8887 ...

However, in Docker Compose, when I write out the following ...

version: '3.7'
services:
  container_name: some-server
  image: some:img
  user: $(id -u):$(id -g) 
  ... 

... it does not work.

I understand I am asking docker-compose up to perform sub shell command substitution, and it cannot.

Is there any way to do this?

Bash Solutions


Solution 1 - Bash

Try this

So, you need to put:

user: "${UID}:${GID}"

in your docker compose and provide UID and GID as docker-compose parameter

UID=${UID} GID=${GID} docker-compose up

(or define UID and GID as environment variables).

Solution 2 - Bash

This can be done as well

In your docker-composer.yml

user: $DOCKER_USER

In the command line

echo 'export DOCKER_USER="$(id -u):$(id -g)"' >> ~/.bash_profile

source ~/.bash_profile

docker-compose up

Created a DOCKER_USER variable and added it in the bash_profile for persistency. The source will help the shell to recognize changes of .bash_profile on demand

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioningyhereView Question on Stackoverflow
Solution 1 - BashEdward AungView Answer on Stackoverflow
Solution 2 - BashAmitesh BhartiView Answer on Stackoverflow