From adf1e9dbe1f575601633065ae588b0ead2a20856 Mon Sep 17 00:00:00 2001 From: Chirag Bhatti Date: Tue, 25 Jul 2023 16:38:29 +0100 Subject: [PATCH] feat: update docker files --- Dockerfile | 63 ++++++++++++++++------------------------------ Dockerfile_dev | 41 ------------------------------ docker-compose.yml | 3 ++- 3 files changed, 23 insertions(+), 84 deletions(-) delete mode 100644 Dockerfile_dev diff --git a/Dockerfile b/Dockerfile index 43479ee9c..3d210f165 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,71 +1,50 @@ -# Build compilation image -FROM ruby:3.1.4-alpine as builder +FROM ruby:3.1.4-alpine3.18 as base -# The application runs from /app WORKDIR /app -# Add the timezone as it's not configured by default in Alpine +# Add the timezone as it’s not configured by default in Alpine RUN apk add --update --no-cache tzdata && \ cp /usr/share/zoneinfo/Europe/London /etc/localtime && \ echo "Europe/London" > /etc/timezone -# build-base: complication tools for bundle +# build-base: compilation tools for bundle # yarn: node package manager # postgresql-dev: postgres driver and libraries -RUN apk add --no-cache build-base yarn postgresql-dev git +RUN apk add --no-cache build-base yarn postgresql-dev git bash -# Install bundler to run bundle exec -# This should be the same version as the Gemfile.lock +# Bundler version should be the same version as what the Gemfile.lock was bundled with RUN gem install bundler:2.3.14 --no-document -# Install gems defined in Gemfile COPY .ruby-version Gemfile Gemfile.lock /app/ +RUN bundle install --jobs=4 --no-binstubs --no-cache -RUN bundle config set without "development test" -ARG BUNDLE_FLAGS="--jobs=4 --no-binstubs --no-cache" -RUN bundle install ${BUNDLE_FLAGS} - -# Install node packages defined in package.json, including webpack COPY package.json yarn.lock /app/ RUN yarn install --frozen-lockfile -# Copy all files to /app (except what is defined in .dockerignore) COPY . /app/ -# Compile assets and run webpack. We set a dummy secret key. -RUN RAILS_ENV=production bundle exec rails SECRET_KEY_BASE=pickasecuretoken assets:precompile +RUN bundle exec rake assets:precompile -# Cleanup to save space in the production image -RUN rm -rf node_modules log tmp && \ - rm -rf /usr/local/bundle/cache && \ - rm -rf .env && \ - find /usr/local/bundle/gems -name "*.c" -delete && \ - find /usr/local/bundle/gems -name "*.h" -delete && \ - find /usr/local/bundle/gems -name "*.o" -delete && \ - find /usr/local/bundle/gems -name "*.html" -delete +ENV PORT=8080 -# Build runtime image -FROM ruby:3.1.4-alpine as production +EXPOSE ${PORT} -# The application runs from /app -WORKDIR /app +FROM base as development -# Add postgres driver library -# Add the timezone as it's not configured by default in Alpine -RUN apk add --update --no-cache libpq tzdata && \ - cp /usr/share/zoneinfo/Europe/London /etc/localtime && \ - echo "Europe/London" > /etc/timezone +# Install gecko driver for Capybara tests +RUN apk add firefox +RUN wget https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-linux64.tar.gz \ + && tar -xvzf geckodriver-v0.31.0-linux64.tar.gz \ + && rm geckodriver-v0.31.0-linux64.tar.gz \ + && chmod +x geckodriver \ + && mv geckodriver /usr/local/bin/ -# Copy files generated in the builder image -COPY --from=builder /app /app -COPY --from=builder /usr/local/bundle/ /usr/local/bundle/ +CMD RAILS_ENV=${RAILS_ENV} bundle exec rake db:migrate && bundle exec rails s -e ${RAILS_ENV} -p ${PORT} --binding=0.0.0.0 -ARG GIT_COMMIT_SHA="UNKNOWN" -ENV GIT_COMMIT_SHA=${GIT_COMMIT_SHA} -RUN echo ${GIT_COMMIT_SHA} > ./GIT_COMMIT_SHA +FROM base as staging -ENV PORT=8080 +CMD RAILS_ENV=${RAILS_ENV} bundle exec rake db:migrate && bundle exec rails s -e ${RAILS_ENV} -p ${PORT} --binding=0.0.0.0 -EXPOSE ${PORT} +FROM base as production CMD RAILS_ENV=${RAILS_ENV} bundle exec rake db:migrate && bundle exec rails s -e ${RAILS_ENV} -p ${PORT} --binding=0.0.0.0 diff --git a/Dockerfile_dev b/Dockerfile_dev deleted file mode 100644 index e80f1d816..000000000 --- a/Dockerfile_dev +++ /dev/null @@ -1,41 +0,0 @@ -# Build compilation image -FROM ruby:3.1.4-alpine - -# The application runs from /app -WORKDIR /app - -# Add the timezone as it's not configured by default in Alpine -RUN apk add --update --no-cache tzdata && cp /usr/share/zoneinfo/Europe/London /etc/localtime && echo "Europe/London" > /etc/timezone - -RUN apk add --no-cache build-base yarn postgresql-dev git bash - -# Install bundler to run bundle exec -# This should be the same version as the Gemfile.lock -RUN gem install bundler:2.3.14 --no-document - -# Install gems defined in Gemfile -COPY .ruby-version Gemfile Gemfile.lock /app/ - -ARG BUNDLE_FLAGS="--jobs=4 --no-binstubs --no-cache" -RUN bundle install ${BUNDLE_FLAGS} - -# Install node packages defined in package.json, including webpack -COPY package.json yarn.lock /app/ -RUN yarn install --frozen-lockfile - -# Install gecko driver for Capybara tests -RUN apk add firefox -RUN wget https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-linux64.tar.gz \ - && tar -xvzf geckodriver-v0.31.0-linux64.tar.gz \ - && rm geckodriver-v0.31.0-linux64.tar.gz \ - && chmod +x geckodriver \ - && mv geckodriver /usr/local/bin/ - -# Copy all files to /app (except what is defined in .dockerignore) -COPY . /app/ - -ENV PORT=8080 - -EXPOSE ${PORT} - -CMD RAILS_ENV=${RAILS_ENV} bundle exec rake db:migrate && bundle exec rails s -e ${RAILS_ENV} -p ${PORT} --binding=0.0.0.0 diff --git a/docker-compose.yml b/docker-compose.yml index fc50f354e..39383968e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,7 +20,8 @@ services: app: build: context: . - dockerfile: ./Dockerfile_dev + dockerfile: ./Dockerfile + target: development ports: - 8080:8080 depends_on: