Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docker] Fix running docker container with existing volume #3540

Merged
merged 1 commit into from
Dec 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions web/docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
#!/bin/bash

USER_ID=$($USER)
USER_ID="$(id -u)"
USER_GROUP="$(id -g)"
WORKSPACE_DIR="/workspace"
PGPASS_FILE=$WORKSPACE_DIR/.pgpass

if [ "$(id -u)" = '0' ]; then
# Create workspace directory and change the permission to 'codechecker' user
# if this directory doesn't exists.
if [ ! -d $WORKSPACE_DIR ]; then
if [ "$USER_ID" = '0' ]; then
echo "Container started with 'root' user."

if [ -d $WORKSPACE_DIR ]; then
echo "Workspace directory '${WORKSPACE_DIR}' already exists."

workspace_dir_owner=$(stat ${WORKSPACE_DIR} -c %u)
if [ "${workspace_dir_owner}" != '0' ]; then
echo "Executing script with workspace directory owner (UID): '${workspace_dir_owner}'..."
exec gosu $workspace_dir_owner "$0" "$@"
Comment on lines +16 to +17
Copy link
Contributor

@whisperity whisperity Dec 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, can confirm this works!

$ docker run -v $(readlink -f workspace):/workspace codechecker-web:csordasmarton/fix_docker_entry_point
Container started with 'root' user.
Workspace directory '/workspace' already exists.
Executing script with workspace directory owner (UID): '1000'...
Executing command: 'CodeChecker server --workspace /workspace --not-host-only'.
[INFO 2021-12-08 11:25] - Checking configuration database ...
$ pwd; ls -alh
/home/whisperity/CodeChecker/web/docker/workspace
Permissions Size User       Date Modified Name
.rw-r--r--   57k whisperity  8 Dec 12:25  config.sqlite
.rw-r--r--  209k whisperity  8 Dec 12:25  Default.sqlite
.r--------    71 whisperity  8 Dec 12:25  root.user
.rw-r--r--  1,6k whisperity  8 Dec 12:25  server_config.json

fi
else
echo "Creating workspace directory: '${WORKSPACE_DIR}'."
csordasmarton marked this conversation as resolved.
Show resolved Hide resolved
echo "WARNING: This directory exists ONLY within the containter!"

mkdir -p $WORKSPACE_DIR
chown -R codechecker:codechecker $WORKSPACE_DIR
fi

# Execute this script again with codechecker user.
exec gosu codechecker "$0" "$@"
echo "Executing script with internal 'codechecker' user (UID: $(id -u codechecker))..."
exec gosu codechecker "$0" "$@"
csordasmarton marked this conversation as resolved.
Show resolved Hide resolved
fi
fi

# Set PostgreSQL password file from secrets.
pgpass=/run/secrets/pgpass
whisperity marked this conversation as resolved.
Show resolved Hide resolved
if [ -f $pgpass ]; then
echo "Set PostgreSQL password file from secrets."
cat $pgpass > ${PGPASS_FILE}
chmod 0600 ${PGPASS_FILE}
chown ${USER_ID}:${USER_GROUP} ${PGPASS_FILE}
export PGPASSFILE=${PGPASS_FILE}
fi

echo "Executing command: '$@'."
exec "$@"