I have the following Dockerfile:
FROM php:7-cli-alpine3.9
ARG USERID=1000
ARG GROUPID=1000
RUN apk add --no-cache --update bash bash-completion bash-doc &&\
addgroup -g ${GROUPID} developer &&\
mkdir -p /home/developer/code &&\
adduser -D -u ${USERID} -G developer -h /home/developer -s /bin/bash developer &&\
chown developer:developer -R /home/developer/code &&\
php -r "copy('https://getcomposer.org/installer', '/tmp/composer-setup.php');"&&\
php -r "if (hash_file('sha384', '/tmp/composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" &&\
php /tmp/composer-setup.php --install-dir=bin --filename=composer &&\
php -r "unlink('composer-setup.php');" &&\
chmod +x /bin/composer
VOLUME /home/developer/code
WORKDIR /home/developer/code
USER developer
ENTRYPOINT /bin/bash
And I can build it via the following docker-compose.yml:
version: '3'
services:
php:
build:
context: .
dockerfile: ./Dockerfile
args:
USERID: $USERID
GROUPID: $GROUPID
image: 'pcmagas/rover_php'
stdin_open: true
tty: true
volumes:
- './:/home/developer/code'
So via providing the values of $USERID and $GROUPID via a .env file each developer can build a custom, tailor-made image for its GNU/Linux local user running the image into his/her computer. Also, he/she can get the user and group id via the following GNU/Linux commands:
id -u # For user id
id -g # For group id
But on Microsoft windows how I can get the correct user and group id in order to map correctly the user id and group id located inside the image with the one located outside?
On Windows users belong to multiple groups, not a specific group. A user token technically has a TokenPrimaryGroup property that is probably there to provide some sort of UNIX compatibility but I don't think Windows comes with a tool to print this SID.
You can get the users SID (SecurityID) with whoami.exe on Vista+:
Batch file:
for /F "tokens=1,2 delims=: " %%A in ('WhoAmI /USER /FO LIST') do @if /I "%%~A" == "SID" echo %%B
Interactive cmd.exe:
for /F "tokens=1,2 delims=: " %A in ('WhoAmI /USER /FO LIST') do @if /I "%~A" == "SID" echo %B
A user SID identifies a user on a specific machine or domain. The last part of the SID (the RID) is the unique user part on that machine/domain.
If you absolutely need "a group" you can parse the output of WHOAMI /GROUPS /FO LIST and look for S-1-5-32-544 (Administrators) without deny only! If it is present the user is an Administrator and if not they are probably just a user (S-1-5-32-545). You can also take a look at how Samba maps groups.