You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a build rule in Bazel that brings up an in-memory Postgres server. When running this test, it has to run postgresql's initdb command. postgresql's initdb command has a hardcoded requirement that you can't run as the root user. There doesn't seem to be a flag or hook or configuration option that allows me to execute as another user either in the base image, or in the Cloud Builder yaml.
@bendory, thank you so much for your reply. Unfortunately it looks like the solution you propose has an extra layer, but definitely set me off on the right path. Thanks! To document what I did for future users:
The advice was to add an adduser directive and switch users. This is definitely first step on the path to having it work. The new user is created by the RUN command and USER means that the next layer in the docker stack starts up as the new user, in my case I chose bazel as the user.
The bazel user is created and given a UID inside of your custom Docker container. For the sake of simplicity, let's call this the ci-image. The UID from the ci-image is determined at runtime by Linux and generally results in the same number for me (9/10 tries it had the same UID).
It appears that there is a directory /builder that is mounted by the Cloud Builder machinery. This seems to be a directory functions as the home directory and the scratch for the user that does building. When you change the user that Docker is expecting to invoke, the user you run in place of root receives the same environment variables (I believe they are being set by the Cloud Builder machinery).
Now these threads converge. /builder is owned by UID:GID 0:0, but the new user does not have that UID. In order to work around this I inserted a new step in the pipeline that runs in a standard ubuntu docker container.
By invoking that before running the bazel test invocation (which happens inside the ci-image), I am able to get the correct set of permissions on the /builder directory which allows bazel test to succeed. This is very much a hack, but I'm not sure how else to work around it given the instability of the UID adduser'd user and the absence of any hooks in the cloudbuild.yaml that lets you specify either the creation and suing of a user or the ability to force /builder to be owned by a specific UID according to this documentation.
@bendory, thank you so much for being so helpful during this process, I greatly appreciate you going above and beyond what you needed to do.
Reacted by Jonas Jensen, Adam Nichols, Fernando Nava, Moe Sy, matihost, Anthony Oleinik and Alexey Gronskiy
Implemented all the steps mentioned here but still getting the following error
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/images/load?quiet=1": dial unix /var/run/docker.sock: connect: permission denied
Activity
bendory commentedon Mar 25, 2020
To run the container as a non-root user, just build your own
bazelcontainer with aUSERdirective in the Dockerfile. For example:Commands run from the resulting container will run as user
bendory.mrmeku commentedon Mar 25, 2020
@bendory Running bazel as non root leads to permission denied errors when bazel attempts to create directories under the
/builders/homedirectory.bendory commentedon Mar 25, 2020
Please work with your Support Engineer to sort out the details beyond the general guidance I've given above.
mrmeku commentedon Mar 26, 2020
Step #1: FATAL: mkdir('/builder/home/.cache/bazel/_bazel_stairwell'): (error: 13): Permission denied
achew22 commentedon Mar 26, 2020
@bendory, thank you so much for your reply. Unfortunately it looks like the solution you propose has an extra layer, but definitely set me off on the right path. Thanks! To document what I did for future users:
The advice was to add an
adduserdirective and switch users. This is definitely first step on the path to having it work. The new user is created by theRUNcommand andUSERmeans that the next layer in the docker stack starts up as the new user, in my case I chosebazelas the user.The
bazeluser is created and given a UID inside of your custom Docker container. For the sake of simplicity, let's call this theci-image. The UID from theci-imageis determined at runtime by Linux and generally results in the same number for me (9/10 tries it had the same UID).It appears that there is a directory
/builderthat is mounted by the Cloud Builder machinery. This seems to be a directory functions as the home directory and the scratch for the user that does building. When you change the user that Docker is expecting to invoke, the user you run in place of root receives the same environment variables (I believe they are being set by the Cloud Builder machinery).Now these threads converge.
/builderis owned byUID:GID 0:0, but the new user does not have that UID. In order to work around this I inserted a new step in the pipeline that runs in a standard ubuntu docker container.By invoking that before running the
bazel testinvocation (which happens inside theci-image), I am able to get the correct set of permissions on the/builderdirectory which allowsbazel testto succeed. This is very much a hack, but I'm not sure how else to work around it given the instability of the UIDadduser'd user and the absence of any hooks in thecloudbuild.yamlthat lets you specify either the creation andsuing of a user or the ability to force/builderto be owned by a specific UID according to this documentation.@bendory, thank you so much for being so helpful during this process, I greatly appreciate you going above and beyond what you needed to do.
aednichols commentedon Nov 24, 2020
As of November 2020, it seems that a slight alteration is required, to
debkanchan commentedon Jan 19, 2024
the bazel builder image should tackle this by iteself
debkanchan commentedon Jan 21, 2024
Implemented all the steps mentioned here but still getting the following error
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/images/load?quiet=1": dial unix /var/run/docker.sock: connect: permission denied