Many articles mention that there is one more way to run a container as a different user: using docker-compose, a tool that instruments Docker on how to run our containers. In short: instead of command line parameters, we use a structured config file that can look like this:
version: '3'
services:
my_service:
image: my_image
command: /bin/bash
We can start our service by running:
$ docker-compose run my_service
Which is equivalent to:
$ docker run -it my_image /bin/bash
Specifying a UID is just a one more line in the config file:
version: '3'
services:
my_service:
image: my_image
user: $UID:$GID
command: /bin/bash
Unfortunately, bash does not set GID by default, so we need to do it before running docker-compose. Using the id command inside a config file won’t work as the file is not pre-processed in any way.
$ GID=$(id -g) docker-compose run my_service
$$ id
uid=1000 gid=1000 groups=1000
Conclusion
Docker-compose is a nice tool that wraps docker run command and allows to make a configuration part of the project. It can be useful when creating build environments, automated tests, or complex run configurations.