0

Set up a docker instance via pull ubuntu and then via base-image/docker, and then successfully installed node.js on top of this.

However, when I attempt to pull the repo of a node.js app that I'm working on, I get to an npm install action and then run into trouble because that action expects NOT to be run as root, and I have instantiated it via

docker run -name="{name}" -t -i {my custom docker container mirroring base-image) /bin/bash 

which has logged me in as root. Is there a way to run docker not as root?

fox
  • 15,428
  • 20
  • 55
  • 85

1 Answers1

3

Yes -- you'll need to create the other user account inside the container according to whatever your container's Linux distro expects (here is an Ubuntu example).

Once you've got the user account set up, you can use the Dockerfile USER parameter to run the remaining commands in the Dockerfile as that user. Please see the PostgreSQL example for a full use case.

Where did the postgre user come from in that example? Debian packages create any users they need when they are installed. If you would like to create your own user you could add RUN useradd to your Dockerfile. For a full example, you could look at the Jira Dockerfile in this Atlassian Blog

As the operator you can also decide the user account to use at docker runtime, using the -u parameter. This would override the USER chosen in the Dockerfile.

Community
  • 1
  • 1
Andy
  • 35,844
  • 6
  • 43
  • 50
  • cool, thanks -- is there a way to do this user creation inside a `Dockerfile`? I've gotten everything running as you describe by creating a sudo `ubuntu` account that ran everything, but then translating this into a working `Dockerfile` script has been a headache because I can't create that account -- would that really just be a matter of the right `RUN useradd` functions? – fox Feb 20 '14 at 11:21
  • 1
    Thanks -- I've added the useradd info more explicitly to the original answer. – Andy Feb 20 '14 at 18:54
  • so, sorry, I have one additional follow-up: I was able to do something like the following: `1. RUN /usr/sbin/useradd --create-home --home-dir /usr/local/{user} --shell /bin/bash {user} 2. RUN /usr/sbin/adduser {user} sudo 3. USER {user}`, but then I am running into trouble running `RUN sudo chown -R $USER /src` to take ownership of the directory. Is there a correct way to do this so that it doesn't exit out with a pw request? – fox Feb 20 '14 at 21:32
  • nm, I bumped the `chown` lines up to be run under root, but now I'm getting an `Error: Attempt to unlock {package@version}, which hasn't been locked` error on all packages. – fox Feb 20 '14 at 23:02
  • Sorry, at that point I think you need help from a Node/npm ninja. – Andy Feb 21 '14 at 19:50