Skip to content

Commit

Permalink
Add --sudo option
Browse files Browse the repository at this point in the history
This adds the option --sudo to run docker with sudo.
  • Loading branch information
gportay committed Nov 5, 2024
1 parent 8ff22bf commit 6874f67
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,13 @@ Adding these two following lines to the Shell `~/.profile` tells *dosh* to uses

It is not recommended to run [dosh(1)] using `sudo` in case the user does not
have permission to send the context to the Docker daemon. Instead, consider
setting the `DOSH_DOCKER` environment to `sudo docker` as it will only run the
`docker` commands with the superuser privileges.
using the option `--sudo` as it will only run the `docker` commands with the
superuser privileges.

On Linux, if you are not a member of the `docker` group, please consider to run
`dosh` as below:

DOSH_DOCKER="sudo docker" dosh

The `DOSH_DOCKER` environment can be set to the Shell profile files to make it
persistent for the session.
dosh --sudo

### DOCKER EXTRA OPTIONS

Expand Down
2 changes: 1 addition & 1 deletion bash-completion
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ _dosh()

local shopts="-a -b -e -f -h -m -n -u -v -x -C"
local short_opts="-c -s -i -o"
local long_opts="--dockerfile --context --no-auto-context --no-extra-options --no-doshprofile --no-doshrc --directory --working-directory --root --dind --home --mount-options --shell --ls --gc --build-only --build --rebuild --rmi --detach --exec --attach --kill --rm --tag --dry-run --verbose --version --help"
local long_opts="--dockerfile --context --no-auto-context --no-extra-options --no-doshprofile --no-doshrc --directory --working-directory --sudo --root --dind --home --mount-options --shell --ls --gc --build-only --build --rebuild --rmi --detach --exec --attach --kill --rm --tag --dry-run --verbose --version --help"
local shell="${DOSHELL:-/bin/sh}"
for (( i=0; i < ${#words[@]}; i++ )); do
if [[ ${words[i]} == --shell ]]; then
Expand Down
13 changes: 10 additions & 3 deletions dosh
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Docker related options:
--no-doshrc Disable read of ./doshrc.
--directory DIR Change to directory before doing anything else.
--working-directory DIR Working directory inside the container.
--sudo Run docker with sudo.
--root Run as root.
--dind Run dosh in dosh.
--home Bind mount home directory.
Expand Down Expand Up @@ -126,6 +127,8 @@ Environment variables:
DOSHLVL Incremented by one each time an instance of dosh
is started.
DOSH_SUDO Set sudo command.
DOSH_DOCKER The docker executable.
DOSH_DOCKERFILE The filename of the Dockerfile to use.
Expand Down Expand Up @@ -589,7 +592,7 @@ has_positional_parameter() {
}

is_doshopt() {
if [[ "$1" =~ ^--(no-auto-context|no-extra-options|no-doshprofile|no-doshrc|root|dind|home|ls|gc|build|rebuild|build-only|rmi|detach|kill|rm|tag)$ ]]
if [[ "$1" =~ ^--(no-auto-context|no-extra-options|no-doshprofile|no-doshrc|sudo|root|dind|home|ls|gc|build|rebuild|build-only|rmi|detach|kill|rm|tag)$ ]]
then
return 0
fi
Expand Down Expand Up @@ -662,7 +665,6 @@ is_shopt_argument() {
}

shopts=()
read -r -a docker <<<"${DOSH_DOCKER[*]:-docker}"
dockerfile="${DOSH_DOCKERFILE:-Dockerfile}"
directory="."
working_directory="$PWD"
Expand All @@ -671,6 +673,8 @@ buildopts=("--quiet")
cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/dosh"
DOSHELL="${DOSHELL:-/bin/sh}"
DOSHLVL="${DOSHLVL:-0}"
DOSH_SUDO="${DOSH_SUDO:-sudo}"
DOSH_DOCKER="${DOSH_DOCKER:-docker}"
while [ "$#" -ne 0 ]
do
if [ "$1" = "--help" ]
Expand All @@ -688,7 +692,7 @@ do
then
verbose=true
buildopts=()
# It is a dosh option without argument (i.e. --root, --home...)
# It is a dosh option without argument (i.e. --sudo, --root, --home...)
elif is_doshopt "$1"
then
optname="${1//-/_}"
Expand Down Expand Up @@ -762,6 +766,9 @@ then
DOSH_DOCKER_RMI_EXTRA_OPTS=
fi

# Set docker executable
read -r -a docker <<<"${sudo:+$DOSH_SUDO }${DOSH_DOCKER[*]}"

# Change the current directory
cd "$directory"

Expand Down
3 changes: 3 additions & 0 deletions dosh.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ command-line through *docker-run(1)*.
**--working-directory DIR**::
Working directory inside the container.

**--sudo**::
Run docker with sudo.

**--root**::
Run as root.

Expand Down
11 changes: 11 additions & 0 deletions tests.bash
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,17 @@ else
fi
echo

run "Test option --sudo"
if DOSH_SUDO="echo sudo" dosh --sudo | tee /dev/stderr | \
grep -q "sudo docker run --rm --volume $PWD:$PWD:rw --user $UID:${GROUPS[0]} --env USER=$USER --env HOME=$HOME --interactive --tty --workdir $PWD --env DOSHLVL=1 --entrypoint /bin/sh dosh-$USER-[0-9a-z]\{64\}"

then
ok
else
ko
fi
echo

run "Test option --root"
if dosh --root -c 'whoami' | tee /dev/stderr | \
diff - <(fakeroot -- /bin/sh -c 'whoami' | tee /dev/stderr )
Expand Down

0 comments on commit 6874f67

Please sign in to comment.