Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set uid and gid in Docker Compose?

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?

like image 318
ingyhere Avatar asked Jul 02 '19 03:07

ingyhere


People also ask

What is UID and GID in docker?

Understanding how user names, group names, user IDs (uid), and group IDs (gid) map between the processes running in the container and the host system is important for building a secure system.


3 Answers

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).

like image 114
Edward Aung Avatar answered Oct 17 '22 21:10

Edward Aung


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

like image 7
Amitesh Bharti Avatar answered Oct 17 '22 21:10

Amitesh Bharti


add following command line arguments on docker build image:

docker build --build-arg UID="$(id -u)" --build-arg GID="$(id -g)" --build-arg UNAME="$(whoami)" . -t tagname -f docker.recipe

append following lines at the begin of the docker recipe:

FROM ubuntu:18.04 AS yoursystem

ARG UID
ARG GID
ARG UNAME
RUN groupadd -g ${GID} -o ${UNAME}
RUN useradd -m -u ${UID} -g ${GID} -o -s /bin/bash ${UNAME}
like image 1
Oleg Kokorin Avatar answered Oct 17 '22 20:10

Oleg Kokorin