Docker mouted

zammad_run.sh

Latest commit 4aced39 on 3 Jun
1 contributor
48 lines (43 sloc) 2.21 KB
#!/bin/sh

# Attempting to use docker environment variables
usermod -u ${USER_ID} ${ZAMMAD_USER} -g ${GROUP_ID} -G ${ZAMMAD_USER},adm,cdrom,sudo,dip,plugdev;

# How to: https://linuxize.com/post/bash-check-if-file-exists/
# If the "zammad_data" shared volume is empty
if [ "$(ls -A /shared/zammad_data)" ]; then
# If the "elasticsearch", "postgresql", and "zammad" folders already exist in persistent data volume
if [ -d "/shared/zammad_data/elasticsearch" -a -d "/shared/zammad_data/postgresql" -a -d "/shared/zammad_data/zammad" ]; then
# Delete static data from docker image
echo "Cleaning environment: remove folders or links.";
rm -rf /var/lib/elasticsearch;
rm -rf /var/lib/postgresql;
rm -rf /opt/zammad;
else
# If the folders don't exist, notify user they may not have a good time
echo "ERROR: Partial data folder.";
echo "NOTE: For a new instance the folder /shared/zammad_data must be empty.";
#exit 1;
fi
else
# If the "zammad_data" directory is not empty, make sure to copy fresh data from the container
echo "Changing permissions of data from container in an attempt to make persistent data on host accessible"
chgrp -R ${GROUP_ID} /var/lib/elasticsearch;
chmod -R 775 /var/lib/elasticsearch;
chgrp -R ${GROUP_ID} /var/lib/postgresql;
chmod -R 750 /var/lib/postgresql;
chgrp -R ${GROUP_ID} /opt/zammad;
chmod -R 775 /opt/zammad;
echo "Moving managed data folders to the mounted folder.";
mv /var/lib/elasticsearch /shared/zammad_data;
mv /var/lib/postgresql /shared/zammad_data;
mv /opt/zammad /shared/zammad_data;
fi

echo "Creating config backup file if necessary";
[ -f /opt/zammad/contrib/backup/config ] || cp /opt/zammad/contrib/backup/config.dist /opt/zammad/contrib/backup/config

# Ensure persistent data links to original locations
echo "Creating new links.";
ln -s /shared/zammad_data/elasticsearch /var/lib/elasticsearch;
ln -s /shared/zammad_data/postgresql /var/lib/postgresql;
ln -s /shared/zammad_data/zammad /opt/zammad;

echo "Fixing Postgres corrupted sessions if necessary.";
su - postgres -c "/usr/lib/postgresql/11/bin/pg_resetxlog -f /var/lib/postgresql/11/main/";
/docker-entrypoint.sh zammad;



#################################################


FROM zammad/zammad:latest
# Attempting to fix file permissions from container to shared volume
# When using mkdir command, I need to specify -p to create parents as necessary. Otherwise, this line will fail
RUN mkdir -p /shared/zammad_data
RUN mkdir -p /shared/zammad_backup
ENV USER_ID=1000
ENV GROUP_ID=1000
#RUN usermod -u ${USER_ID} ${ZAMMAD_USER}
#RUN groupmod -g ${GROUP_ID} ${ZAMMAD_USER}
RUN chown -R ${USER_ID}:${GROUP_ID} /shared/zammad_data && chown -R ${USER_ID}:${GROUP_ID} /shared/zammad_backup
#https://askubuntu.com/questions/487527/give-specific-user-permission-to-write-to-a-folder-using-w-notation
VOLUME /shared/zammad_data
VOLUME /shared/zammad_backup
COPY ./zammad_run.sh /opt/zammad_run.sh
RUN chmod 777 /opt/zammad_run.sh
ENTRYPOINT /opt/zammad_run.sh