Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use sudo inside Dockerfile (Alpine)

I have this Dockerfile ...

FROM keymetrics/pm2:latest-alpine

RUN apk update && \
    apk upgrade && \
    apk add \
       bash

COPY . ./

EXPOSE 1886 80 443

CMD pm2-docker start --auto-exit --env ${NODE_ENV} ecosystem.config.js

How can I execute the CMD command using sudo ?

I need to do this because the port 443 is allowed only for sudo user.

like image 201
ridermansb Avatar asked Mar 11 '18 22:03

ridermansb


People also ask

Can we use Sudo in Dockerfile?

By default that Unix socket is owned by the user root and other users can only access it using sudo . The Docker daemon always runs as the root user. If you don't want to preface the docker command with sudo , create a Unix group called docker and add users to it.

How do I use Sudo in alpine?

Check if an user has sudo access in Alpine Linux. As you can see, the user "ostechnix" can run all commands in my Alpine Linux box. Let us switch to the new sudo user and verify if he can able to run sudo tasks. Run any sudo operation to verify if the user really has sudo permissions.

What is alpine in Dockerfile?

Alpine Linux is a Linux distribution built around musl libc and BusyBox. The image is only 5 MB in size and has access to a package repository that is much more complete than other BusyBox based images. This makes Alpine Linux a great image base for utilities and even production applications.


1 Answers

The su-exec can be used in alpine. Do add it the package, if not already available, add the following to your Dockerfile

RUN apk add --no-cache su-exec

Inside your scripts you'd run inside docker you can use the following to become another user:

exec su-exec <my-user> <my command>

Alternatively, you could add the more familiair sudo package while building your docker-file Add the following to your Dockerfile that's FROM alpine

RUN set -ex && apk --no-cache add sudo

After that you can use sudo

sudo -u <my-user> <my command>
like image 117
Gerbrand Avatar answered Sep 19 '22 19:09

Gerbrand