How to set user in docker image?

Article ID:218583777
1 minute readKnowledge base

Issue

  • Running tasks on docker images that work correctly on the command line results in permission issues.

    • Eg. Running sh 'npm install package_blah' results in something similar to: npm ERR! Error: Invalid name: "@adsk/lmv".

    • Eg. Running sh 'pip install package_blah' on the python image results in something similar to: error: [Errno 13] Permission denied: '/usr/local/lib/python/site-packages/version.py'

Resolution

First determine that the commands you are running work using docker without the plugin (command line).

Assuming everything works natively, then determine what user is running these commands for the image (command line):

docker run -it <image> whoami

Specify that user in the Docker Pipeline job to ensure that the same user it used.

The below example shows how to run pip install with the python:2.7 docker image:

docker.image('python:2.7').withRun('-u my_user') {
    sh 'pip install version'
}
This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.