Skip to content

Commit

Permalink
Dockerfile: Adds gosu support
Browse files Browse the repository at this point in the history
Currently is not possible to use a different user id as one
used when the container is created.

| id -u
| # 10060
| docker run --rm -u $(id -u) ...

This will enter in the container with a different user as the default
one created and configured. Now inside the container the user id from
the host machine, 10060 in this case, not exist:

| # I have no name!@2e9103210ce0:/$
| whoami
| # whoami: cannot find name for user ID 10060
| # I have no name!
| id -u builder
| # 1000

To user the default user account with the same id for user and group
we can use gosu with an entrypoint.
Launching docker with the environment argument UID and GID fixes
this issue.

| echo "$(id -u)/$(id -g)"
| # 10060/100
| docker run --rm --env UID=$(id -u) --env GID=$(id -g) ...

And on the container we have the same:

| # builder@20f4393ec053:/$
| whoami
| # builder
| echo "$(id -u)/$(id -g)"
| # 10060/100

Signed-off-by: Jose Quaresma <[email protected]>
  • Loading branch information
quaresmajose committed Jan 4, 2023
1 parent c252669 commit 0d6d2d5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ RUN apt-get update \
make patch repo sudo texinfo vim-tiny wget whiptail libelf-dev git-lfs screen \
socket corkscrew curl xz-utils tcl libtinfo5 device-tree-compiler python3-pip python3-dev \
tmux libncurses-dev vim zstd lz4 liblz4-tool libc6-dev-i386 \
awscli docker-compose \
awscli docker-compose gosu \
&& ln -s /usr/bin/python3 /usr/bin/python \
&& pip3 --no-cache-dir install expandvars jsonFormatter \
&& apt-get autoremove -y \
Expand All @@ -72,8 +72,14 @@ RUN useradd -c $DEV_USER_NAME \
-s /bin/bash \
$DEV_USER

# Add entrypoint to run gosu
COPY entrypoint /
ENTRYPOINT ["/entrypoint"]

# Add default password for the SDK user (useful with sudo)
RUN echo $DEV_USER:$DEV_USER_PASSWD | chpasswd
# and replace entrypoint with dev user name
RUN echo $DEV_USER:$DEV_USER_PASSWD | chpasswd && \
sed "s/@@DOCKER_USER@@/$DEV_USER/g" -i /entrypoint

# Initialize development environment for $DEV_USER.
RUN sudo -u $DEV_USER -H git config --global credential.helper 'cache --timeout=3600'
Expand Down
45 changes: 45 additions & 0 deletions entrypoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh

: '
MIT License
Copyright (c) 2022 Foundries.io
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'

set -e
set -u

: "${UID:=0}"
: "${GID:=${UID}}"

if [ "$#" = 0 ]; then
set -- "$(command -v bash 2>/dev/null || command -v sh)" -l
fi

if [ "$UID" != 0 ]; then
usermod -u "$UID" "@@DOCKER_USER@@" 2>/dev/null && {
groupmod -g "$GID" "@@DOCKER_USER@@" 2>/dev/null ||
usermod -a -G "$GID" "@@DOCKER_USER@@"
}
set -- gosu "${UID}:${GID}" "${@}"
fi

exec "$@"

0 comments on commit 0d6d2d5

Please sign in to comment.