Skip to content
Snippets Groups Projects
Commit 37ecd745 authored by nimrod's avatar nimrod
Browse files

A single version of runas using gosu.

parent 44c7c161
No related branches found
No related tags found
No related merge requests found
...@@ -23,9 +23,9 @@ The trivial solution is to run the container with the correct user id, like so ...@@ -23,9 +23,9 @@ The trivial solution is to run the container with the correct user id, like so
gid="$(id -g)" gid="$(id -g)"
docker run -v "$PWD:/volume" --user "$uid:$gid" buildimage make docker run -v "$PWD:/volume" --user "$uid:$gid" buildimage make
I personally find it a tiresome after the 3rd time I had to rebuild the I personally find it a tiresome after the 3rd time I had to `sudo chown` the
project because I forgot to specify the uid and gid and it's a (low) barrier project because I forgot to specify the uid and gid and it's a (low) barrier
to entry for new users. of entry for new users.
A better solution A better solution
----------------- -----------------
...@@ -38,32 +38,36 @@ commands. ...@@ -38,32 +38,36 @@ commands.
#!/bin/sh #!/bin/sh
set -eu set -eu
command -v sudo > /dev/null || { echo "Can't find sudo, exiting."; exit 1; } [ "$(id -u)" = "0" ] || { echo "Not running as root, continuing as the current user."; eval exec "$@"; }
uid="$(stat . --format '%u')" command -v stat > /dev/null || { echo "Can't find stat, exiting."; exit 1; }
gid="$(stat . --format '%g')" command -v gosu > /dev/null || { echo "Can't find gosu, exiting."; exit 1; }
groupadd --force --non-unique --gid "$gid" builder uid="$(stat . -c '%u')"
useradd --non-unique --uid "$uid" --gid "$gid" --home-dir "$PWD" --no-create-home --shell /bin/bash builder gid="$(stat . -c '%g')"
sudo -Eu "#$uid" -g "#$gid" -- "$@" eval exec gosu "$uid:$gid" "$@"
The script is also available for `download The script is also available for `download
<https://www.shore.co.il/blog/static/runas-gnu>`_. The only dependency is <https://www.shore.co.il/blog/static/runas>`_. The only dependency is
:code:`sudo`. There's also a `version `gosu <https://github.com/tianon/gosu>`_. You can download and check it to
<https://www.shore.co.il/blog/static/runas-busybox>`_ for images using BusyBox your VCS and incorporate it into your Dockerfile, or download it via the
(like Alpine). You can download and check it to your VCS and incorporate it :code:`ADD` directive, like so:
into your Dockerfile, or download it via the :code:`ADD` directive, like so:
.. code:: shell .. code:: shell
FROM debian:stable FROM buildpack-deps
RUN apt-get update && \ RUN curl -fsSL https://github.com/tianon/gosu/releases/download/1.10/gosu-amd64 -o gosu-amd64 && \
DEBIAN_FRONTEND=noninteractive apt-get install -y sudo build-essential install -o root -g root -m 755 gosu-amd64 /usr/local/bin/gosu && \
ADD [ "https://www.shore.co.il/blog/static/runas-gnu", "/entrypoint" ] rm gosu-amd64 && \
ENTRYPOINT [ "/bin/sh", "/entrypoint" ] curl -fsSL https://www.shore.co.il/blog/static/runas -o runas && \
install -o root -g root -m 755 runas /entrypoint && \
rm runas
ENTRYPOINT [ "/entrypoint" ]
VOLUME /volume VOLUME /volume
WORKDIR /volume WORKDIR /volume
ENV HOME /volume ENV HOME /volume
And then finally, to build run Setting the home directory to the mounted volume will result in some files (like
package managers cache) to be created there, which you may or may not want. And
then finally, to build run
.. code:: shell .. code:: shell
......
runas-gnu
\ No newline at end of file
#!/bin/sh
set -eu
[ "$(id -u)" = "0" ] || { echo "Not running as root, continuing as the current user."; eval exec "$@"; }
command -v stat > /dev/null || { echo "Can't find stat, exiting."; exit 1; }
command -v gosu > /dev/null || { echo "Can't find gosu, exiting."; exit 1; }
uid="$(stat . -c '%u')"
gid="$(stat . -c '%g')"
eval exec gosu "$uid:$gid" "$@"
#!/bin/sh
set -eu
command -v sudo > /dev/null || { echo "Can't find sudo, exiting."; exit 1; }
uid="$(stat . -c '%u')"
gid="$(stat . -c '%g')"
addgroup -g "$gid" builder
adduser -h "$PWD" -s /bin/sh -G builder -u "$uid" -H -D builder
sudo -Eu "#$uid" -g "#$gid" -- "$@"
#!/bin/sh
set -eu
command -v sudo > /dev/null || { echo "Can't find sudo, exiting."; exit 1; }
uid="$(stat . --format '%u')"
gid="$(stat . --format '%g')"
groupadd --force --non-unique --gid "$gid" builder
useradd --non-unique --uid "$uid" --gid "$gid" --home-dir "$PWD" --no-create-home --shell /bin/bash builder
sudo -Eu "#$uid" -g "#$gid" -- "$@"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment