Skip to content

Running as a non-root user #641

Closed
Closed

Description

@achew22

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.

Affected builder image

gcr.io/cloud-builders/bazel

Expected Behavior

The tests aren't running as the root user

Actual Behavior

The tests are running as the root user

Steps to Reproduce the Problem

  1. Create a cloudbuild.yaml
steps:
- name: gcr.io/cloud-builders/bazel
  entrypoint: whoami
  1. See that it prints out root

Activity

  1. bendory commented on Mar 25, 2020

    @bendory
    Member

    To run the container as a non-root user, just build your own bazel container with a USER directive in the Dockerfile. For example:

    FROM gcr.io/cloud-builders/bazel
    RUN adduser bendory
    USER bendory
    

    Commands run from the resulting container will run as user bendory.

  2. mrmeku commented on Mar 25, 2020

    @mrmeku

    @bendory Running bazel as non root leads to permission denied errors when bazel attempts to create directories under the /builders/home directory.

  3. bendory commented on Mar 25, 2020

    @bendory
    Member

    Please work with your Support Engineer to sort out the details beyond the general guidance I've given above.

  4. mrmeku commented on Mar 26, 2020

    @mrmeku

    Step #1: FATAL: mkdir('/builder/home/.cache/bazel/_bazel_stairwell'): (error: 13): Permission denied

  5. achew22 commented on Mar 26, 2020

    @achew22
    Author

    @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.

      - name: 'ubuntu'
        entrypoint: 'bash'
        args:
          [
            '-c',
            'chmod -R 777 /builder',
          ]
    

    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.

  6. aednichols commented on Nov 24, 2020

    @aednichols

    As of November 2020, it seems that a slight alteration is required, to

    [
      '-c',
      'chmod -R 777 /workspace',
    ]
    
  7. debkanchan commented on Jan 19, 2024

    @debkanchan

    the bazel builder image should tackle this by iteself

  8. debkanchan commented on Jan 21, 2024

    @debkanchan

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions