-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docker] Fix running docker container with existing volume
- 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
1 parent
40273a8
commit 202f72d
Showing
1 changed file
with
21 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" "$@" | ||
fi | ||
else | ||
echo "Creating workspace directory: '${WORKSPACE_DIR}'." | ||
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" "$@" | ||
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 "$@" |