HiveBrain v1.2.0
Get Started
← Back to all entries
patternbashdockerMinor

Interference of Docker CMD with su: works with su-exec but not with su?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
interferencedockerwithbutworksexeccmdnot

Problem

In this bash script under Linux Alpine they have (line 8):

exec su-exec "$ZOO_USER" "$0" "$@"


As far as I know, su-exec is however an Apache httpd dependecy - can I avoid it installing it under Ubuntu and use some more plain alternative?

I have tested so far:

su -c "$0 $@" $ZOO_USER


But then the call through Docker CMD shows that I might have some sort of escaping/quotes error,

CMD ["zkServer.sh", "start-foreground"]


resulting in the following output; obviously there is a misplacement for the username field in the command sequence.

No passwd entry for user 'start-foreground'


But what is then the difference to the original (su-exec), which works fine? And how to get it right with su?

====

UPD: su-exec is here a third-parthy Alpine Linux package
https://github.com/ncopa/su-exec

Solution

wrong argument order to su

$ZOO_USER needs to be the first argument to su with the "$@" coming afterwards.

su -c "$0" "$ZOO_USER" "$@"


It is ok for the options to come before the username. It is a good idea to keep them in double quotes so that there aren't issues if someone accidentally puts spaces into it.

Your error message reinforces that this is an order of arguments problem.

even better

Based on comments this would be even better:

su -c "$0" "$ZOO_USER" -- "$@"


su man page excerpt


Additional arguments may be provided after the username, in which case they are supplied to the user's login shell. In particular, an argument of
-c will cause the next argument to be treated as a command by most command interpreters. The command will be executed by the shell specified in /etc/passwd for the target user.


You can use the -- argument to separate su options from the arguments supplied to the shell.

busybox docs excerpt:

In a comment the OP asked about why this wouldn't work in Alpine Linux. Since alpine is based on busybox I looked up the busybox docs.
According to the docs busybox doesn't support sending additional arguments to su:

su
su [OPTIONS] [-] [username]

Change user id or become root

Options:

        -p, -m  Preserve environment
        -c CMD  Command to pass to 'sh -c'
        -s SH   Shell to use instead of default shell

Code Snippets

su -c "$0" "$ZOO_USER" "$@"
su -c "$0" "$ZOO_USER" -- "$@"
su
su [OPTIONS] [-] [username]

Change user id or become root

Options:

        -p, -m  Preserve environment
        -c CMD  Command to pass to 'sh -c'
        -s SH   Shell to use instead of default shell

Context

StackExchange DevOps Q#2526, answer score: 6

Revisions (0)

No revisions yet.