'Docker run --group-add not working when trying to add user to dialout group

I have a USB serial device that I need to access from a docker container (running Ubuntu + ROS).

I have added visibility to the devices by setting the following in my docker run command

--privileged \
-v /dev:/dev \

I can then manually use the following commands to add the user to the dialout group.

usermod -a -G dialout user

This then allows me to communicate with the serial port as expected. BUT when I launch the container next time (every time is done via a make command and I don't have control over changing that process) the user will be created without the group permissions (as expected). So my question is how to i control adding the user to the group within the docker run command.

The user is created by getting the user from and produces a string like "1000:1000"

UID_MAP=$$UID:$(shell id -g $$USER)

I have done a search for this issue and found this answer however I have tried to implement it but do not get the desired results.

--user=${UID_MAP} --group-add=dialout \
newgrp dialout

When I make the changes suggested in the answer above It just adds an additional group called dialout rather than adding the user to the group

groups

gives

user dialout

groups user

gives

user root



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source