Skip to content

Commit

Permalink
[docker] Fix running docker container with existing volume
Browse files Browse the repository at this point in the history
- Print some useful info message in the `entrypoint.sh` script file.
- If the `workspace` directory already exists when the container started, try
to run the given command with the owner of the workspace directory, otherwise
run it with the `codechecker` user.
  • Loading branch information
csordasmarton committed Dec 7, 2021
1 parent 40273a8 commit 8ff3666
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions web/docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,34 @@ 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
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 "Execute script again with workspace directory owner (UID): '${workspace_dir_owner}'."
exec gosu $workspace_dir_owner "$0" "$@"
fi
else
echo "Creating workspace directory: '${WORKSPACE_DIR}'."
mkdir -p $WORKSPACE_DIR
chown -R codechecker:codechecker $WORKSPACE_DIR
fi

# Execute this script again with codechecker user.
exec gosu codechecker "$0" "$@"
echo "Execute script again with 'codechecker' user."
exec gosu codechecker "$0" "$@"
fi
fi

# Set PostgreSQL password file from secrets.
pgpass=/run/secrets/pgpass
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 "$@"

0 comments on commit 8ff3666

Please sign in to comment.