When using the Docker executor, the container runs as whatever user the image creators defined, which is almost always
root; the gitlab runner doesn't set the user to
root. There have been requests to make this configurable in the step configuration, but a maintainer pointed out that you can create a custom image based on the image you need.
So for example, let's say the user defined for the image
root, but I need to run something as the
mysql user. Here's an example Dockerfile:
This image is build on top of
mysql:8, but we change the user it runs as. You can build the image by running
docker build mysql_custom_image. If successful, at the end of the output you'll get something like
Successfully built h47841j5285. Then you can give it a more friendly name:
docker tag h47841j5285 my_org/mysql_custom_image.
If you're using Gitlab's built-in registry, you can then push it up there, or to hub.docker.com. If not, you can copy the Dockerfile to your runners and build it on each of them.
Your new .gitlab-ci.yml file would look something like: