Use Sudo Inside Dockerfile (Alpine)

how to use apt/sudo in alpine-based docker image


another method of obtaining basic tools like sudo and curl?

I believe alpine uses apk instead of apt

Try something like this to install basic tools like curl and sudo

RUN apk add curl sudo

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

RUN apt-get update && \
apt-get -y install sudo

RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo

USER docker
CMD /bin/bash

correct method to create user in alpine docker container so that sudo works correctly

From man sudo:

 -s, --shell
Run the shell specified by the SHELL environment variable if it is set or the shell specified by the invoking user's password database entry.

You have neither SHELL variable set, nor correct (interactive) default shell set in /etc/passwd for user payara. This is because you are creating a system user (-S) - this user has a default shell /bin/false (which just exits with exit code 1 - you may check with echo $? after unsuccessfull sudo -s).

You may overcome this in different ways:

a) specify the SHELL variable:

bash-4.4$ SHELL=/bin/bash sudo -s
bed662af470d:~#

b) use su, which will use the default root's shell:

bash-4.4$ sudo su -
bed662af470d:~#

c) just run the required privileged commands with sudo directly, without spawning an interactive shell.

How to run bash as user root on alpine images with docker? su: must be suid to work properly

You can run a command within the container as root using --user root. To get a shell:

docker exec -it --user root kong sh


Related Topics



Leave a reply



Submit