nick
nick

Reputation: 105

Run Ubuntu Docker container as User with sudo access

I'm trying to write a Dockerfile that creates a user with a home directory who is part of sudoers group and that launches the container as this user.

The problem I'm facing is that, from within the container, every command needs to be prepended sudo, which obviously creates permission issues for every file that's created.

My reasoning behind doing this is that I want a container that mimics a clean linux environment from which I can write install scripts for users.

Here is a copy of my Dockerfile so far:

FROM ubuntu:20.04
 
# Make user home
RUN mkdir -p /home/nick

# Create a nick  user
RUN useradd -r -d /home/nick -m -s /sbin/nologin -c "Docker image user" nick
# Add to sudoers
RUN usermod -a -G sudo nick
# Change ownership of home directory
RUN chown -R nick:nick $HOME
# Set password
RUN echo "nick:******" | chpasswd
# Install sudo
RUN apt-get -y update && apt-get -y install sudo

ENV HOME=/home/nick
WORKDIR $HOME

USER nick

Upvotes: 4

Views: 3008

Answers (1)

Charlie Parker
Charlie Parker

Reputation: 5209

I don't understand why this doesn't work:

FROM continuumio/miniconda3
# FROM --platform=linux/amd64 continuumio/miniconda3

MAINTAINER Brando Miranda "[email protected]"

RUN apt-get update \
  && apt-get install -y --no-install-recommends \
    ssh \
    git \
    m4 \
    libgmp-dev \
    opam \
    wget \
    ca-certificates \
    rsync \
    strace \
    gcc \
    rlwrap \
    sudo

# https://github.com/giampaolo/psutil/pull/2103

RUN useradd -m bot
# format for chpasswd user_name:password
RUN echo "bot:bot" | chpasswd
RUN adduser bot sudo

WORKDIR /home/bot
USER bot

# CMD /bin/bash

Upvotes: 1

Related Questions