diff --git a/bin/rails-dev-run.sh b/bin/rails-dev-run.sh index c7003c0..98109be 100755 --- a/bin/rails-dev-run.sh +++ b/bin/rails-dev-run.sh @@ -2,14 +2,19 @@ project_dir="$(dirname $( dirname $(readlink -f ${BASH_SOURCE[0]})))" USER_ID=$(id -u ${USER}) -# Get group id for username -GROUP_ID=$(cut -d: -f3 < <(getent group ${USER})) +# Get group id for username - fixed to correctly retrieve numeric GID +GROUP_ID=$(id -g ${USER}) if [ $# != 1 ]; then echo "Please enter docker image name for the rails development environment" exit 1 fi -docker build -t $1 -f ${project_dir}/deploy/rails-dev-Dockerfile ${project_dir} -docker run --rm -it -p 3000:3000 -v ${project_dir}:/rails $1 /bin/bash -sudo chown -R ${USER_ID}:${USER_ID} ${project_dir} \ No newline at end of file +docker build -t $1 -f ${project_dir}/deploy/rails-dev-Dockerfile \ + --build-arg USER_ID=$USER_ID \ + --build-arg GROUP_ID=$GROUP_ID \ + ${project_dir} + +docker run --rm -it -p 3000:3000 \ + -v ${project_dir}:/rails \ + $1 /bin/bash \ No newline at end of file diff --git a/deploy/rails-dev-Dockerfile b/deploy/rails-dev-Dockerfile index 3b4a4ea..11b2840 100644 --- a/deploy/rails-dev-Dockerfile +++ b/deploy/rails-dev-Dockerfile @@ -1,5 +1,9 @@ FROM ruby:3.2.0 +# Accept build arguments for user/group IDs +ARG USER_ID=1000 +ARG GROUP_ID=1000 + RUN apt-get -qq update \ && apt-get -qq install -y \ build-essential \ @@ -17,21 +21,61 @@ RUN echo "America/New_York" > /etc/timezone \ && rm /etc/localtime \ && ln -s /usr/share/zoneinfo/America/New_York /etc/localtime -RUN echo 'gem: --no-rdoc --no-ri' > /root/.gemrc +# Install gems as root first RUN gem install bundler RUN gem update --system + +# Add Gemfile before creating user (still as root) ADD Gemfile* /tmp/ WORKDIR /tmp -RUN bundle config set without 'production' -RUN bundle install --jobs 4 -RUN mkdir /rails +# Create a non-root user with the provided user/group IDs +# Use existing group if GID already exists +RUN if grep -q ":${GROUP_ID}:" /etc/group; then \ + GROUP_NAME=$(getent group ${GROUP_ID} | cut -d: -f1); \ + else \ + GROUP_NAME=devuser; \ + groupadd -g $GROUP_ID $GROUP_NAME; \ + fi && \ + useradd -u $USER_ID -g $GROUP_ID -m -s /bin/bash devuser \ + && echo "devuser ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/devuser \ + && chmod 0440 /etc/sudoers.d/devuser + +# Now that user exists, set up permissions +RUN echo 'gem: --no-rdoc --no-ri' > /home/devuser/.gemrc \ + && mkdir -p /home/devuser/.bundle \ + && chown -R ${USER_ID}:${GROUP_ID} /home/devuser /tmp/Gemfile* \ + && chmod -R 777 /usr/local/bundle + +# Switch to the non-root user for all subsequent commands +USER devuser + +# Pre-install gems from Gemfile +WORKDIR /tmp +RUN bundle config set --local without 'production' && \ + bundle install --jobs 4 + +# Create the rails directory with correct ownership +RUN sudo mkdir -p /rails && sudo chown ${USER_ID}:${GROUP_ID} /rails WORKDIR /rails -ADD . /rails +# Add helper script to initialize the project +RUN echo '#!/bin/bash\n\ +if [ ! -f "bin/rails" ]; then\n\ + echo "Setting up Rails binstubs..."\n\ + bundle binstubs --all\n\ + echo "Rails setup complete. You can now use bin/rails."\n\ +else\n\ + echo "Rails binstubs already exist."\n\ +fi\n\ +exec "$@"\n\ +' > /home/devuser/init_rails.sh \ +&& chmod +x /home/devuser/init_rails.sh VOLUME ["/rails"] EXPOSE 3000 -CMD /bin/bash +# Use the init script as entrypoint +ENTRYPOINT ["/home/devuser/init_rails.sh"] +CMD ["/bin/bash"]