How to use sudo inside a docker container?
Just got it. As regan pointed out, I had to add the user to the sudoers group. But the main reason was I'd forgotten to update the repositories cache, so apt-get couldn't find the sudo package. It's working now. Here's the completed code:
FROM ubuntu:12.04RUN apt-get update && \ apt-get -y install sudoRUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudoUSER dockerCMD /bin/bash
When neither sudo nor apt-get is available in container, you can also jump into running container as root user using command
docker exec -u root -t -i container_id /bin/bash
The other answers didn't work for me. I kept searching and found a blog post that covered how a team was running non-root inside of a docker container.
Here's the TL;DR version:
RUN apt-get update \ && apt-get install -y sudoRUN adduser --disabled-password --gecos '' dockerRUN adduser docker sudoRUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoersUSER docker# this is where I was running into problems with the other approachesRUN sudo apt-get update
I was using FROM node:9.3
for this, but I suspect that other similar container bases would work as well.