Skip to content

Instantly share code, notes, and snippets.

@rahuldhole
Last active May 9, 2024 00:22
Show Gist options
  • Save rahuldhole/ec6ecb051802b561d7134478f735a5d8 to your computer and use it in GitHub Desktop.
Save rahuldhole/ec6ecb051802b561d7134478f735a5d8 to your computer and use it in GitHub Desktop.
rsync your code into docker container. Instead of direct mounting to the WORKDIR mount somewhere else an sync it to avoid permission override issue leading to permission denied errors in the container.
# Use a Debian base image
FROM debian:latest
ARG USERNAME=debian
# Define environment variables
ENV SOURCE_CODE_VOLUME_MOUNT="/host-code" \
WORKING_DIR="/app"
# Install rsync, make, nano, and procps
RUN apt-get update \
&& apt-get install -y rsync procps make nano \
&& rm -rf /var/lib/apt/lists/*
## create a non-root user for security
RUN useradd -m -s /bin/bash $USERNAME
WORKDIR /app
RUN chown -R $USERNAME:$USERNAME ./
# Set up directories
RUN mkdir /host-code
# Create rsync script
RUN echo '#!/bin/bash' > /usr/local/bin/rsync_source \
&& echo 'rsync -ha --exclude=log --exclude=public/packs --exclude=node_modules --exclude=tmp --exclude=.git --exclude=mysql --exclude=.env "${SOURCE_CODE_VOLUME_MOUNT}/." "${WORKING_DIR}/."' >> /usr/local/bin/rsync_source \
&& chmod +x /usr/local/bin/rsync_source
# Set up continuous sync loop
RUN echo '#!/bin/bash' > /usr/local/bin/dev_sync.sh \
&& echo 'while true; do /usr/local/bin/rsync_source; echo "sources synced."; sleep 3; done' >> /usr/local/bin/dev_sync.sh \
&& chmod +x /usr/local/bin/dev_sync.sh
# Define dev_sync and dev_sync_kill commands
RUN echo '#!/bin/bash' > /usr/local/bin/dev_sync \
&& echo '/usr/local/bin/dev_sync.sh > /dev/null 2>&1 &' >> /usr/local/bin/dev_sync \
&& echo '#!/bin/bash' > /usr/local/bin/dev_sync_kill \
&& echo 'pkill -f "/usr/local/bin/dev_sync.sh"' >> /usr/local/bin/dev_sync_kill \
&& chmod +x /usr/local/bin/dev_sync /usr/local/bin/dev_sync_kill
USER $USERNAME
CMD ["/bin/bash"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment