From 5c5213e87210cbfc383cab719cec12f057d6bc6f Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Fri, 11 Oct 2024 09:34:41 +0900 Subject: [PATCH 01/15] Use Alpine Image for Docker --- Dockerfile | 151 ++++++++++++----------------- docker-compose.yml => compose.yaml | 13 +-- 2 files changed, 68 insertions(+), 96 deletions(-) rename docker-compose.yml => compose.yaml (88%) diff --git a/Dockerfile b/Dockerfile index cb7e79b08ca..6102d08d355 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,92 +1,63 @@ -FROM ubuntu:20.04 - -ENV TZ Europe/London - -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone - -RUN echo "deb http://security.ubuntu.com/ubuntu bionic-security main" >> /etc/apt/sources.list - -# Install all the requirements -RUN apt-get update && apt-get install -y \ +FROM ruby:3.1.4-alpine3.19 AS base +ARG TARGETPLATFORM +ENV LANG=C.UTF-8 \ + LC_ALL=C.UTF-8 \ + TZ=Europe/London \ + RAILS_ROOT=/usr/src/app +RUN apk --no-cache upgrade +RUN apk add --no-cache tzdata +# Only packages required to run Rails in the production environment can be added to `.essentials`. +# postgresql-dev: Required by the postgre gem +# imagemagick, imagemagick-jpeg: Required by the mini_magick gem +# wkhtmltopdf: Required by wicked_pdf and wkhtmltopdf-binary +RUN set -e && \ + apk add --no-cache --virtual .essentials \ + postgresql-client \ + imagemagick \ + imagemagick-jpeg && \ + apk add --no-cache --virtual wkhtmltopdf + +WORKDIR $RAILS_ROOT + +FROM base AS development-base +RUN apk add --no-cache --virtual .build-deps build-base postgresql-dev git nodejs yarn + +# Adjust pre-packaged Alpine gems and software to match current version of OpenFoodNetwork +RUN gem install bundler -v '2.4.3' +RUN gem install rake -v '13.2.1' && gem uninstall rake -v '13.0.6' --executables || true + +RUN < .ruby-version - -# Install Rbenv & Ruby -RUN git clone --depth 1 https://github.com/rbenv/rbenv.git ${RBENV_ROOT} && \ - git clone --depth 1 https://github.com/rbenv/ruby-build.git ${RBENV_ROOT}/plugins/ruby-build && \ - echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh && \ - RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install $(cat .ruby-version) && \ - rbenv global $(cat .ruby-version) - -# Install Postgres -RUN sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main' >> /etc/apt/sources.list.d/pgdg.list" && \ - curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null && \ - apt-get update && \ - apt-get install -yqq --no-install-recommends postgresql-client-10 libpq-dev - - -# trim spaces and line return from .node-version file -COPY .node-version .node-version.raw -RUN cat .node-version.raw | tr -d '\r\t ' > .node-version - -# Install Node and Yarn with Nodenv -RUN git clone --depth 1 https://github.com/nodenv/nodenv.git ${NODENV_ROOT} && \ - git clone --depth 1 https://github.com/nodenv/node-build.git ${NODENV_ROOT}/plugins/node-build && \ - git clone --depth 1 https://github.com/pine/nodenv-yarn-install.git ${NODENV_ROOT}/plugins/nodenv-yarn-install && \ - git clone --depth 1 https://github.com/nodenv/nodenv-package-rehash.git ${NODENV_ROOT}/plugins/nodenv-package-rehash && \ - echo 'eval "$(nodenv init -)"' >> /etc/profile.d/nodenv.sh && \ - nodenv install $(cat .node-version) && \ - nodenv global $(cat .node-version) - -# Install Chrome -RUN wget --quiet -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \ - sh -c "echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list.d/google-chrome.list" && \ - apt-get update && \ - apt-get install -fy google-chrome-stable - -# Install Chromedriver -RUN wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip && \ - unzip chromedriver_linux64.zip -d /usr/bin && \ - chmod u+x /usr/bin/chromedriver - -# Copy code and install app dependencies -COPY . /usr/src/app/ - -# Install Bundler -RUN ./script/install-bundler - -# Install front-end dependencies -RUN yarn install - -# Run bundler install in parallel with the amount of available CPUs -RUN bundle install --jobs="$(nproc)" + libxslt-dev \ + libffi-dev \ + vips-dev \ + chromium-chromedriver +apk add --no-cache bash curl \ + && curl -o /usr/local/bin/wait-for-it https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh \ + && chmod +x /usr/local/bin/wait-for-it +EOF + +FROM development-base AS yarn-dependencies +COPY package.json yarn.lock ./ +RUN yarn install --frozen-lockfile + +FROM development-base AS development +COPY . $RAILS_ROOT +COPY Gemfile Gemfile.lock ./ +RUN bundle install --jobs "$(nproc)" +COPY --from=yarn-dependencies $RAILS_ROOT/node_modules ./node_modules diff --git a/docker-compose.yml b/compose.yaml similarity index 88% rename from docker-compose.yml rename to compose.yaml index 7bb7988a8ca..50c3a420918 100644 --- a/docker-compose.yml +++ b/compose.yaml @@ -1,5 +1,3 @@ -version: '3' - services: db: image: postgres:10.19 @@ -17,7 +15,9 @@ services: web: tty: true stdin_open: true - build: . + build: + target: development + context: . ports: - 3000:3000 volumes: @@ -37,9 +37,10 @@ services: bash -c "wait-for-it -t 30 db:5432 && rm -f tmp/pids/server.pid && (bundle check || bundle install) && - bundle exec rake db:create && + rake db:create && yarn install && - bundle exec rails s -p 3000 -b '0.0.0.0'" + rake ofn:sample_data && + rails s -p 3000 -b '0.0.0.0'" webpack: build: . command: ./bin/webpack-dev-server @@ -70,7 +71,7 @@ services: command: > bash -c "wait-for-it -t 30 db:5432 && (bundle check || bundle install) && - bundle exec sidekiq -q mailers -q default" + sidekiq -q mailers -q default" volumes: gems: postgres: From 3f905cce163c8c413a5cadba06258766cdefa89e Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Fri, 11 Oct 2024 09:34:19 +0900 Subject: [PATCH 02/15] Remove bundle exec from docker commands --- docker/cop | 2 +- docker/seed | 6 +++--- docker/test | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docker/cop b/docker/cop index 739947d5140..8f91705e9c5 100755 --- a/docker/cop +++ b/docker/cop @@ -5,7 +5,7 @@ echo '-------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rubocop' echo '-------------------------------------------------' -docker compose run web bundle exec rubocop +docker compose run web rubocop echo '-----------------------------------------------' echo 'END: docker compose run web bundle exec rubocop' echo '-----------------------------------------------' diff --git a/docker/seed b/docker/seed index c594dc4302d..79f6d8c87e1 100755 --- a/docker/seed +++ b/docker/seed @@ -5,7 +5,7 @@ echo '-------------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rake db:reset' echo '-------------------------------------------------------' -docker compose run web bundle exec rake db:reset +docker compose run web rake db:reset echo '-----------------------------------------------------' echo 'END: docker compose run web bundle exec rake db:reset' echo '-----------------------------------------------------' @@ -13,7 +13,7 @@ echo '-----------------------------------------------------' echo '--------------------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rake db:test:prepare' echo '--------------------------------------------------------------' -docker compose run web bundle exec rake db:test:prepare +docker compose run web rake db:test:prepare echo '------------------------------------------------------------' echo 'END: docker compose run web bundle exec rake db:test:prepare' echo '------------------------------------------------------------' @@ -21,7 +21,7 @@ echo '------------------------------------------------------------' echo '--------------------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rake ofn:sample_data' echo '--------------------------------------------------------------' -docker compose run web bundle exec rake ofn:sample_data +docker compose run web rake ofn:sample_data echo '------------------------------------------------------------' echo 'END: docker compose run web bundle exec rake ofn:sample_data' echo '------------------------------------------------------------' diff --git a/docker/test b/docker/test index bc6da82f5d1..ced0cf8aece 100755 --- a/docker/test +++ b/docker/test @@ -9,7 +9,7 @@ DATE=`date +%Y%m%d-%H%M%S-%3N` echo '--------------------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rake db:test:prepare' echo '--------------------------------------------------------------' -docker compose run web bundle exec rake db:test:prepare 2>&1 | tee log/test-prepare-$DATE.log +docker compose run web rake db:test:prepare 2>&1 | tee log/test-prepare-$DATE.log echo '------------------------------------------------------------' echo 'END: docker compose run web bundle exec rake db:test:prepare' echo '------------------------------------------------------------' @@ -17,7 +17,7 @@ echo '------------------------------------------------------------' echo '----------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rspec spec' echo '----------------------------------------------------' -docker compose run web bundle exec rspec --no-color spec 2>&1 | tee log/test-rspec-$DATE.log +docker compose run web rspec --no-color spec 2>&1 | tee log/test-rspec-$DATE.log echo '--------------------------------------------------' echo 'END: docker compose run web bundle exec rspec spec' echo '--------------------------------------------------' \ No newline at end of file From efe0a2a701a28545887a7d3c6c9792c33917e94a Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Fri, 11 Oct 2024 09:35:52 +0900 Subject: [PATCH 03/15] Add Redis Test URL for Spree Preferences --- compose.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/compose.yaml b/compose.yaml index 50c3a420918..1fe9ee451a0 100644 --- a/compose.yaml +++ b/compose.yaml @@ -32,6 +32,7 @@ services: OFN_DB_HOST: db OFN_REDIS_URL: redis://redis/ OFN_REDIS_JOBS_URL: redis://redis + OFN_REDIS_TEST_URL: redis://redis/3 WEBPACKER_DEV_SERVER_HOST: webpack command: > bash -c "wait-for-it -t 30 db:5432 && From b4aaa0fae12f8be98c0c845dbc1dd6c93553531d Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Wed, 23 Oct 2024 23:18:36 +0900 Subject: [PATCH 04/15] Add Helpful Docker Commands --- docker/README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docker/README.md b/docker/README.md index 78285ea587f..75c31365031 100644 --- a/docker/README.md +++ b/docker/README.md @@ -44,6 +44,26 @@ $ docker/build ```sh $ docker/server ``` +* Alternatively, you can run the server in detached mode. This will return you to the terminal prompt without stopping the server. No logs will be displayed: +```sh +$ docker compose up -d +``` +* To stop the server, run: +```sh +$ docker compose down +``` +* To access the server logs, run: +```sh +$ docker compose logs -f +``` +* To run tests or access the server directly, you can use bash: +```sh +$ docker compose exec web bash +``` +* Once you have connected, you can run tests and other operations without `bundle exec`: +```sh +$ rspec spec/models/voucher_spec.rb +``` * The default admin user is 'ofn@example.com' with the password 'ofn123'. * View the app in the browser at `http://localhost:3000`. * You will then get the trace of the containers in the terminal. You can stop the containers using Ctrl-C in the terminal. @@ -73,6 +93,26 @@ $ docker/build.ps1 ```command $ docker/server.ps1 ``` +* Alternatively, you can run the server in detached mode. This will return you to the terminal prompt without stopping the server. No logs will be displayed: +```sh +$ docker compose up -d +``` +* To stop the server, run: +```sh +$ docker compose down +``` +* To access the server logs, run: +```sh +$ docker compose logs -f +``` +* To run tests or access the server directly, you can use bash: +```sh +$ docker compose exec web bash +``` +* Once you have connected, you can run tests and other operations without `bundle exec`: +```sh +$ rspec spec/models/voucher_spec.rb +``` You may need to wait several minutes before getting the server up and running properly. * The default admin user is 'ofn@example.com' with the password 'ofn123'. * View the app in the browser at `http://localhost:3000`. From 15c920c91112b8e7a86e7335b7eeed6542aa026b Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Sat, 2 Nov 2024 23:22:18 +0900 Subject: [PATCH 05/15] Refactor Alpine Dockerfile --- Dockerfile | 63 ----------------------------------------------- alpine.Dockerfile | 32 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 63 deletions(-) delete mode 100644 Dockerfile create mode 100644 alpine.Dockerfile diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 6102d08d355..00000000000 --- a/Dockerfile +++ /dev/null @@ -1,63 +0,0 @@ -FROM ruby:3.1.4-alpine3.19 AS base -ARG TARGETPLATFORM -ENV LANG=C.UTF-8 \ - LC_ALL=C.UTF-8 \ - TZ=Europe/London \ - RAILS_ROOT=/usr/src/app -RUN apk --no-cache upgrade -RUN apk add --no-cache tzdata -# Only packages required to run Rails in the production environment can be added to `.essentials`. -# postgresql-dev: Required by the postgre gem -# imagemagick, imagemagick-jpeg: Required by the mini_magick gem -# wkhtmltopdf: Required by wicked_pdf and wkhtmltopdf-binary -RUN set -e && \ - apk add --no-cache --virtual .essentials \ - postgresql-client \ - imagemagick \ - imagemagick-jpeg && \ - apk add --no-cache --virtual wkhtmltopdf - -WORKDIR $RAILS_ROOT - -FROM base AS development-base -RUN apk add --no-cache --virtual .build-deps build-base postgresql-dev git nodejs yarn - -# Adjust pre-packaged Alpine gems and software to match current version of OpenFoodNetwork -RUN gem install bundler -v '2.4.3' -RUN gem install rake -v '13.2.1' && gem uninstall rake -v '13.0.6' --executables || true - -RUN < Date: Sat, 9 Nov 2024 09:10:09 +0900 Subject: [PATCH 06/15] Revise docker scripts to use optional dockfiles --- docker/build | 6 +++++- docker/build-log | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docker/build b/docker/build index f3966951f8b..95cfbc43dec 100755 --- a/docker/build +++ b/docker/build @@ -4,5 +4,9 @@ set -e # This script builds the Docker container, seeds the app with sample data, and logs the screen output. DATE=`date +%Y%m%d-%H%M%S-%3N` -docker/build-log 2>&1 | tee log/build-$DATE.log +if [ -n "$1" ]; then + docker/build-log $1 2>&1 | tee log/build-$DATE.log +else + docker/build-log 2>&1 | tee log/build-$DATE.log +fi docker/seed 2>&1 | tee log/seed-$DATE.log diff --git a/docker/build-log b/docker/build-log index d2cbc2fa759..cfbe9b6de89 100755 --- a/docker/build-log +++ b/docker/build-log @@ -6,7 +6,11 @@ wait echo '###########################' echo 'BEGIN: docker compose build' echo '###########################' -docker compose build # Set up the Docker containers +if [ -n "$1" ]; then + docker build -f $1 . # Set up the Docker containers +else + docker compose build # Set up the Docker containers +fi echo '##############################' echo 'FINISHED: docker compose build' echo '##############################' From 0ec8d13641e9d29d62f6c70c46ffb3f60ab3f85e Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Fri, 8 Nov 2024 16:40:44 +0900 Subject: [PATCH 07/15] Optimize Dockerfile --- Dockerfile | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000000..6825efabb01 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,103 @@ +# First stage: Build stage with all dependencies and tools +FROM ubuntu:20.04 AS build + +# Set timezone and install base dependencies +ENV TZ=Europe/London +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \ + apt-get update && apt-get install -y \ + curl \ + git \ + build-essential \ + software-properties-common \ + wget \ + zlib1g-dev \ + libreadline-dev \ + libyaml-dev \ + libffi-dev \ + libxml2-dev \ + libxslt1-dev \ + wait-for-it \ + imagemagick \ + unzip \ + libjemalloc-dev \ + libssl-dev \ + ca-certificates \ + gnupg + +# Setup environment variables +ENV PATH /usr/local/src/rbenv/shims:/usr/local/src/rbenv/bin:/usr/local/src/nodenv/shims:/usr/local/src/nodenv/bin:$PATH +ENV RBENV_ROOT /usr/local/src/rbenv +ENV NODENV_ROOT /usr/local/src/nodenv +ENV CONFIGURE_OPTS --disable-install-doc +ENV BUNDLE_PATH /bundles +ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so + +# Install rbenv and Ruby +COPY .ruby-version .ruby-version.raw +RUN cat .ruby-version.raw | tr -d '\r\t ' > .ruby-version +RUN git clone --depth 1 https://github.com/rbenv/rbenv.git ${RBENV_ROOT} && \ + git clone --depth 1 https://github.com/rbenv/ruby-build.git ${RBENV_ROOT}/plugins/ruby-build && \ + echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh && \ + RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install $(cat .ruby-version) && \ + rbenv global $(cat .ruby-version) + +# Install PostgreSQL client and development libraries +RUN sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main' >> /etc/apt/sources.list.d/pgdg.list" && \ + curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null && \ + apt-get update && \ + apt-get install -y --no-install-recommends postgresql-client-10 libpq-dev + +# Install nodenv and Node.js with Yarn +COPY .node-version .node-version.raw +RUN cat .node-version.raw | tr -d '\r\t ' > .node-version +RUN git clone --depth 1 https://github.com/nodenv/nodenv.git ${NODENV_ROOT} && \ + git clone --depth 1 https://github.com/nodenv/node-build.git ${NODENV_ROOT}/plugins/node-build && \ + git clone --depth 1 https://github.com/pine/nodenv-yarn-install.git ${NODENV_ROOT}/plugins/nodenv-yarn-install && \ + git clone --depth 1 https://github.com/nodenv/nodenv-package-rehash.git ${NODENV_ROOT}/plugins/nodenv-package-rehash && \ + echo 'eval "$(nodenv init -)"' >> /etc/profile.d/nodenv.sh && \ + nodenv install $(cat .node-version) && \ + nodenv global $(cat .node-version) + +# Install Chrome and Chromedriver +RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \ + sh -c "echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' > /etc/apt/sources.list.d/google-chrome.list" && \ + apt-get update && apt-get install -y google-chrome-stable && \ + wget -q https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip && \ + unzip chromedriver_linux64.zip -d /usr/bin && \ + chmod u+x /usr/bin/chromedriver && \ + +# Copy application code and install dependencies +WORKDIR /usr/src/app +COPY . /usr/src/app/ +RUN ./script/install-bundler && yarn install && bundle install --jobs="$(nproc)" + +# Final stage: Runtime stage with minimal necessary components +FROM ubuntu:20.04 AS development + +# Set timezone and copy necessary binaries and libraries +ENV TZ=Europe/London +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \ + apt-get update && apt-get install -y \ + imagemagick \ + libjemalloc-dev \ + libssl-dev \ + libpq-dev \ + ca-certificates \ + && rm -rf /var/lib/apt/lists/* + +# Copy runtime-specific environment configurations +ENV PATH /usr/local/src/rbenv/shims:/usr/local/src/rbenv/bin:/usr/local/src/nodenv/shims:/usr/local/src/nodenv/bin:$PATH +ENV BUNDLE_PATH /bundles +ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so + +# Copy only essential files and libraries from build stage +COPY --from=build /usr/src/app /usr/src/app +COPY --from=build /usr/local/src/rbenv /usr/local/src/rbenv +COPY --from=build /usr/local/src/nodenv /usr/local/src/nodenv +COPY --from=build /etc/profile.d/rbenv.sh /etc/profile.d/rbenv.sh +COPY --from=build /etc/profile.d/nodenv.sh /etc/profile.d/nodenv.sh +COPY --from=build /usr/bin/chromedriver /usr/bin/chromedriver +COPY --from=build /usr/bin/google-chrome-stable /usr/bin/google-chrome-stable + +# Set working directory +WORKDIR /usr/src/app From 02e2214caa670f852596deeadee0142a1e5e2a65 Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Fri, 8 Nov 2024 23:04:53 +0900 Subject: [PATCH 08/15] Revert "Remove bundle exec from docker commands" This reverts commit 2d193a689406cf826e241314acd661fd87a6ae37. --- compose.yaml | 11 ++++------- docker/cop | 2 +- docker/seed | 6 +++--- docker/test | 4 ++-- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/compose.yaml b/compose.yaml index 1fe9ee451a0..fca7b309d5b 100644 --- a/compose.yaml +++ b/compose.yaml @@ -15,9 +15,7 @@ services: web: tty: true stdin_open: true - build: - target: development - context: . + build: . ports: - 3000:3000 volumes: @@ -38,10 +36,9 @@ services: bash -c "wait-for-it -t 30 db:5432 && rm -f tmp/pids/server.pid && (bundle check || bundle install) && - rake db:create && + bundle exec rake db:create && yarn install && - rake ofn:sample_data && - rails s -p 3000 -b '0.0.0.0'" + bundle exec rails s -p 3000 -b '0.0.0.0'" webpack: build: . command: ./bin/webpack-dev-server @@ -72,7 +69,7 @@ services: command: > bash -c "wait-for-it -t 30 db:5432 && (bundle check || bundle install) && - sidekiq -q mailers -q default" + bundle exec sidekiq -q mailers -q default" volumes: gems: postgres: diff --git a/docker/cop b/docker/cop index 8f91705e9c5..739947d5140 100755 --- a/docker/cop +++ b/docker/cop @@ -5,7 +5,7 @@ echo '-------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rubocop' echo '-------------------------------------------------' -docker compose run web rubocop +docker compose run web bundle exec rubocop echo '-----------------------------------------------' echo 'END: docker compose run web bundle exec rubocop' echo '-----------------------------------------------' diff --git a/docker/seed b/docker/seed index 79f6d8c87e1..c594dc4302d 100755 --- a/docker/seed +++ b/docker/seed @@ -5,7 +5,7 @@ echo '-------------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rake db:reset' echo '-------------------------------------------------------' -docker compose run web rake db:reset +docker compose run web bundle exec rake db:reset echo '-----------------------------------------------------' echo 'END: docker compose run web bundle exec rake db:reset' echo '-----------------------------------------------------' @@ -13,7 +13,7 @@ echo '-----------------------------------------------------' echo '--------------------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rake db:test:prepare' echo '--------------------------------------------------------------' -docker compose run web rake db:test:prepare +docker compose run web bundle exec rake db:test:prepare echo '------------------------------------------------------------' echo 'END: docker compose run web bundle exec rake db:test:prepare' echo '------------------------------------------------------------' @@ -21,7 +21,7 @@ echo '------------------------------------------------------------' echo '--------------------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rake ofn:sample_data' echo '--------------------------------------------------------------' -docker compose run web rake ofn:sample_data +docker compose run web bundle exec rake ofn:sample_data echo '------------------------------------------------------------' echo 'END: docker compose run web bundle exec rake ofn:sample_data' echo '------------------------------------------------------------' diff --git a/docker/test b/docker/test index ced0cf8aece..bc6da82f5d1 100755 --- a/docker/test +++ b/docker/test @@ -9,7 +9,7 @@ DATE=`date +%Y%m%d-%H%M%S-%3N` echo '--------------------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rake db:test:prepare' echo '--------------------------------------------------------------' -docker compose run web rake db:test:prepare 2>&1 | tee log/test-prepare-$DATE.log +docker compose run web bundle exec rake db:test:prepare 2>&1 | tee log/test-prepare-$DATE.log echo '------------------------------------------------------------' echo 'END: docker compose run web bundle exec rake db:test:prepare' echo '------------------------------------------------------------' @@ -17,7 +17,7 @@ echo '------------------------------------------------------------' echo '----------------------------------------------------' echo 'BEGIN: docker compose run web bundle exec rspec spec' echo '----------------------------------------------------' -docker compose run web rspec --no-color spec 2>&1 | tee log/test-rspec-$DATE.log +docker compose run web bundle exec rspec --no-color spec 2>&1 | tee log/test-rspec-$DATE.log echo '--------------------------------------------------' echo 'END: docker compose run web bundle exec rspec spec' echo '--------------------------------------------------' \ No newline at end of file From 5b58f7b20efad9416d0371627014e4c29b166592 Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Fri, 8 Nov 2024 23:53:36 +0900 Subject: [PATCH 09/15] Add Dockerfile back in --- Dockerfile | 129 +++++++++++++++++++++------------------------- alpine.Dockerfile | 2 +- 2 files changed, 60 insertions(+), 71 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6825efabb01..d6fb889d0bc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,30 +1,33 @@ -# First stage: Build stage with all dependencies and tools -FROM ubuntu:20.04 AS build - -# Set timezone and install base dependencies -ENV TZ=Europe/London -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \ - apt-get update && apt-get install -y \ - curl \ - git \ - build-essential \ - software-properties-common \ - wget \ - zlib1g-dev \ - libreadline-dev \ - libyaml-dev \ - libffi-dev \ - libxml2-dev \ - libxslt1-dev \ - wait-for-it \ - imagemagick \ - unzip \ - libjemalloc-dev \ - libssl-dev \ - ca-certificates \ - gnupg - -# Setup environment variables +FROM ubuntu:20.04 + +ENV TZ Europe/London + +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +RUN echo "deb http://security.ubuntu.com/ubuntu bionic-security main" >> /etc/apt/sources.list + +# Install all the requirements +RUN apt-get update && apt-get install -y \ + curl \ + git \ + build-essential \ + software-properties-common \ + wget \ + zlib1g-dev \ + libreadline-dev \ + libyaml-dev \ + libffi-dev \ + libxml2-dev \ + libxslt1-dev \ + wait-for-it \ + imagemagick \ + unzip \ + libjemalloc-dev \ + libssl-dev \ + ca-certificates \ + gnupg + +# Setup ENV variables ENV PATH /usr/local/src/rbenv/shims:/usr/local/src/rbenv/bin:/usr/local/src/nodenv/shims:/usr/local/src/nodenv/bin:$PATH ENV RBENV_ROOT /usr/local/src/rbenv ENV NODENV_ROOT /usr/local/src/nodenv @@ -32,24 +35,31 @@ ENV CONFIGURE_OPTS --disable-install-doc ENV BUNDLE_PATH /bundles ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so -# Install rbenv and Ruby +WORKDIR /usr/src/app + +# trim spaces and line return from .ruby-version file COPY .ruby-version .ruby-version.raw RUN cat .ruby-version.raw | tr -d '\r\t ' > .ruby-version + +# Install Rbenv & Ruby RUN git clone --depth 1 https://github.com/rbenv/rbenv.git ${RBENV_ROOT} && \ git clone --depth 1 https://github.com/rbenv/ruby-build.git ${RBENV_ROOT}/plugins/ruby-build && \ echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh && \ RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install $(cat .ruby-version) && \ rbenv global $(cat .ruby-version) -# Install PostgreSQL client and development libraries +# Install Postgres RUN sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main' >> /etc/apt/sources.list.d/pgdg.list" && \ - curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null && \ + curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null && \ apt-get update && \ - apt-get install -y --no-install-recommends postgresql-client-10 libpq-dev + apt-get install -yqq --no-install-recommends postgresql-client-10 libpq-dev + -# Install nodenv and Node.js with Yarn +# trim spaces and line return from .node-version file COPY .node-version .node-version.raw RUN cat .node-version.raw | tr -d '\r\t ' > .node-version + +# Install Node and Yarn with Nodenv RUN git clone --depth 1 https://github.com/nodenv/nodenv.git ${NODENV_ROOT} && \ git clone --depth 1 https://github.com/nodenv/node-build.git ${NODENV_ROOT}/plugins/node-build && \ git clone --depth 1 https://github.com/pine/nodenv-yarn-install.git ${NODENV_ROOT}/plugins/nodenv-yarn-install && \ @@ -58,46 +68,25 @@ RUN git clone --depth 1 https://github.com/nodenv/nodenv.git ${NODENV_ROOT} && \ nodenv install $(cat .node-version) && \ nodenv global $(cat .node-version) -# Install Chrome and Chromedriver -RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \ - sh -c "echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' > /etc/apt/sources.list.d/google-chrome.list" && \ - apt-get update && apt-get install -y google-chrome-stable && \ - wget -q https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip && \ +# Install Chrome +RUN wget --quiet -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \ + sh -c "echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list.d/google-chrome.list" && \ + apt-get update && \ + apt-get install -fy google-chrome-stable + +# Install Chromedriver +RUN wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip && \ unzip chromedriver_linux64.zip -d /usr/bin && \ - chmod u+x /usr/bin/chromedriver && \ + chmod u+x /usr/bin/chromedriver -# Copy application code and install dependencies -WORKDIR /usr/src/app +# Copy code and install app dependencies COPY . /usr/src/app/ -RUN ./script/install-bundler && yarn install && bundle install --jobs="$(nproc)" - -# Final stage: Runtime stage with minimal necessary components -FROM ubuntu:20.04 AS development - -# Set timezone and copy necessary binaries and libraries -ENV TZ=Europe/London -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \ - apt-get update && apt-get install -y \ - imagemagick \ - libjemalloc-dev \ - libssl-dev \ - libpq-dev \ - ca-certificates \ - && rm -rf /var/lib/apt/lists/* - -# Copy runtime-specific environment configurations -ENV PATH /usr/local/src/rbenv/shims:/usr/local/src/rbenv/bin:/usr/local/src/nodenv/shims:/usr/local/src/nodenv/bin:$PATH -ENV BUNDLE_PATH /bundles -ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so -# Copy only essential files and libraries from build stage -COPY --from=build /usr/src/app /usr/src/app -COPY --from=build /usr/local/src/rbenv /usr/local/src/rbenv -COPY --from=build /usr/local/src/nodenv /usr/local/src/nodenv -COPY --from=build /etc/profile.d/rbenv.sh /etc/profile.d/rbenv.sh -COPY --from=build /etc/profile.d/nodenv.sh /etc/profile.d/nodenv.sh -COPY --from=build /usr/bin/chromedriver /usr/bin/chromedriver -COPY --from=build /usr/bin/google-chrome-stable /usr/bin/google-chrome-stable +# Install Bundler +RUN ./script/install-bundler -# Set working directory -WORKDIR /usr/src/app +# Install front-end dependencies +RUN yarn install + +# Run bundler install in parallel with the amount of available CPUs +RUN bundle install --jobs="$(nproc)" \ No newline at end of file diff --git a/alpine.Dockerfile b/alpine.Dockerfile index 8e04631f9f8..abe1123c25b 100644 --- a/alpine.Dockerfile +++ b/alpine.Dockerfile @@ -25,7 +25,7 @@ COPY package.json yarn.lock ./ RUN yarn install --frozen-lockfile # Install Ruby gems -FROM development-base AS development +FROM development-base COPY . $RAILS_ROOT COPY Gemfile Gemfile.lock ./ RUN bundle install --jobs "$(nproc)" From d36438037abab53e05c984b354ddfd0960da6ed3 Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Sat, 9 Nov 2024 09:09:41 +0900 Subject: [PATCH 10/15] Revise Docker README --- docker/README.md | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/docker/README.md b/docker/README.md index 75c31365031..39aae7cf9ee 100644 --- a/docker/README.md +++ b/docker/README.md @@ -36,10 +36,14 @@ $ git clone git@github.com:openfoodfoundation/openfoodnetwork.git ```sh $ cd openfoodnetwork ``` -* Download the Docker images, build the Docker containers, seed the database with sample data, AND log the screen output from these tasks: +* You have two choices of images you can build. The default Ubuntu image is most similar to the production servers but is larger in size and will take longer to build. Alternatively, you can use the Alpine image which is smaller, faster to setup, and should be compatible with the Ubuntu. To download the Ubuntu Docker images, build the Docker containers, seed the database with sample data, AND log the screen output from these tasks: ```sh $ docker/build ``` +* To use the alpine image to start with append the name of the Dockerfile: +```sh +$ docker/build alpine.Dockerfile +``` * Run the Rails server and its required Docker containers: ```sh $ docker/server @@ -60,10 +64,6 @@ $ docker compose logs -f ```sh $ docker compose exec web bash ``` -* Once you have connected, you can run tests and other operations without `bundle exec`: -```sh -$ rspec spec/models/voucher_spec.rb -``` * The default admin user is 'ofn@example.com' with the password 'ofn123'. * View the app in the browser at `http://localhost:3000`. * You will then get the trace of the containers in the terminal. You can stop the containers using Ctrl-C in the terminal. @@ -109,10 +109,6 @@ $ docker compose logs -f ```sh $ docker compose exec web bash ``` -* Once you have connected, you can run tests and other operations without `bundle exec`: -```sh -$ rspec spec/models/voucher_spec.rb -``` You may need to wait several minutes before getting the server up and running properly. * The default admin user is 'ofn@example.com' with the password 'ofn123'. * View the app in the browser at `http://localhost:3000`. From 039399ee37eb5a30d04b76d33ee8e1411e5b9837 Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Sat, 9 Nov 2024 22:07:14 +0900 Subject: [PATCH 11/15] Simplify Docker Build --- docker/build | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docker/build b/docker/build index 95cfbc43dec..d97f1e62dfe 100755 --- a/docker/build +++ b/docker/build @@ -4,9 +4,5 @@ set -e # This script builds the Docker container, seeds the app with sample data, and logs the screen output. DATE=`date +%Y%m%d-%H%M%S-%3N` -if [ -n "$1" ]; then - docker/build-log $1 2>&1 | tee log/build-$DATE.log -else - docker/build-log 2>&1 | tee log/build-$DATE.log -fi +docker/build-log $1 2>&1 | tee log/build-$DATE.log docker/seed 2>&1 | tee log/seed-$DATE.log From 477447ad920f11868aa93f0d66e4c092bea73ac8 Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Sat, 9 Nov 2024 22:08:04 +0900 Subject: [PATCH 12/15] Rewrite Powershell Scripts for Alternative Dockerfile --- docker/build-log.ps1 | 9 ++++++++- docker/build.ps1 | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/docker/build-log.ps1 b/docker/build-log.ps1 index 15139fe4d81..4e668b9d87f 100644 --- a/docker/build-log.ps1 +++ b/docker/build-log.ps1 @@ -1,5 +1,12 @@ Write-Host "Docker cleaning: remove old containers" -ForegroundColor Blue docker compose down -v --remove-orphans Write-Host "Docker build: set up the docker containers" -ForegroundColor Blue -docker compose build + +# Check if an argument is provided +if ($args.Count -gt 0) { + # Run the build command with the argument and log output + docker build -f $1 . +} else { + docker compose build +} Write-Host "Docker build finished" \ No newline at end of file diff --git a/docker/build.ps1 b/docker/build.ps1 index 3032d86b322..99908b93096 100644 --- a/docker/build.ps1 +++ b/docker/build.ps1 @@ -4,5 +4,11 @@ $DateTime=Get-Date -Format "yyyyMMdd-HHmmss" -docker/build-log.ps1 > log/build-$DateTime.log 2>&1 +# Check if an argument is provided +if ($args.Count -gt 0) { + # Run the build command with the argument and log output + docker/build-log.ps1 $args[0] > log/build-$DateTime.log 2>&1 +} else { + docker/build-log.ps1 > log/build-$DateTime.log 2>&1 +} docker/seed.ps1 > log/seed-$DateTime.log 2>&1 From b554eda7c7122a2d9ee0d9749e533fb65d697cbb Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Sat, 9 Nov 2024 22:18:06 +0900 Subject: [PATCH 13/15] Add Windows Docker Commands to README --- docker/README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docker/README.md b/docker/README.md index 39aae7cf9ee..450b0bd0d87 100644 --- a/docker/README.md +++ b/docker/README.md @@ -85,10 +85,14 @@ $ git clone git@github.com:openfoodfoundation/openfoodnetwork.git ```command $ cd openfoodnetwork ``` -* Download the Docker images, build the Docker containers, seed the database with sample data, AND log the screen output from these tasks: +* You have two choices of images you can build. The default Ubuntu image is most similar to the production servers but is larger in size and will take longer to build. Alternatively, you can use the Alpine image which is smaller, faster to setup, and should be compatible with the Ubuntu. To download the Ubuntu Docker images, build the Docker containers, seed the database with sample data, AND log the screen output from these tasks: ```command $ docker/build.ps1 ``` +* To use the alpine image to start with append the name of the Dockerfile: +```sh +$ docker/build.ps1 alpine.Dockerfile +``` * Run the Rails server and its required Docker containers: ```command $ docker/server.ps1 From 078e191d26a4e3281f11abef7dd3168b67e0c9f9 Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Thu, 14 Nov 2024 09:32:05 +0900 Subject: [PATCH 14/15] Sync Docker Container to the Host --- compose.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compose.yaml b/compose.yaml index fca7b309d5b..72837470e2c 100644 --- a/compose.yaml +++ b/compose.yaml @@ -22,6 +22,8 @@ services: - .:/usr/src/app - gems:/bundles - ./config/database.yml:/usr/src/app/config/database.yml + - /etc/timezone:/etc/timezone:ro + - /etc/localtime:/etc/localtime:ro depends_on: - db - redis From e7213dba681c3b103d0559845927e52226bc37d5 Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Thu, 14 Nov 2024 09:52:54 +0900 Subject: [PATCH 15/15] Revise README --- docker/README.md | 119 ++++++++++++++++------------------------------- 1 file changed, 41 insertions(+), 78 deletions(-) diff --git a/docker/README.md b/docker/README.md index 450b0bd0d87..10a919e51d6 100644 --- a/docker/README.md +++ b/docker/README.md @@ -21,7 +21,8 @@ Note: There is no need to install Docker Desktop on Linux. * You may have to deselect the option to use Docker Compose V2 in Docker settings to make our scripts work. ## Getting Started -### Linux + +1. **Clone the Repository**: * Open a terminal with a shell. * Clone the repository. If you're planning on contributing code to the project (which we [LOVE](CONTRIBUTING.md)), begin by forking this repo with the Fork button in the top-right corner of this screen. * Use git clone to copy your fork onto your local machine. @@ -32,88 +33,50 @@ $ git clone https://github.com/YOUR_GITHUB_USERNAME_HERE/openfoodnetwork ```sh $ git clone git@github.com:openfoodfoundation/openfoodnetwork.git ``` -* Go at the root of the app: + +2. **Navigate to the App Directory**: ```sh $ cd openfoodnetwork ``` -* You have two choices of images you can build. The default Ubuntu image is most similar to the production servers but is larger in size and will take longer to build. Alternatively, you can use the Alpine image which is smaller, faster to setup, and should be compatible with the Ubuntu. To download the Ubuntu Docker images, build the Docker containers, seed the database with sample data, AND log the screen output from these tasks: -```sh -$ docker/build -``` -* To use the alpine image to start with append the name of the Dockerfile: -```sh -$ docker/build alpine.Dockerfile -``` -* Run the Rails server and its required Docker containers: -```sh -$ docker/server -``` -* Alternatively, you can run the server in detached mode. This will return you to the terminal prompt without stopping the server. No logs will be displayed: -```sh -$ docker compose up -d -``` -* To stop the server, run: -```sh -$ docker compose down -``` -* To access the server logs, run: -```sh -$ docker compose logs -f -``` -* To run tests or access the server directly, you can use bash: -```sh -$ docker compose exec web bash -``` -* The default admin user is 'ofn@example.com' with the password 'ofn123'. -* View the app in the browser at `http://localhost:3000`. -* You will then get the trace of the containers in the terminal. You can stop the containers using Ctrl-C in the terminal. -* You can find some useful tips and commands [here](https://github.com/openfoodfoundation/openfoodnetwork/wiki/Docker:-useful-tips-and-commands). -### Windows -* Prerequisite : don't forget to activate the execution of powershell scripts following the instruction on [this page chosing "Using RemoteSigned Execution Policy"](https://shellgeek.com/powershell-fix-running-scripts-is-disabled-on-this-system/) -* Open a terminal with a shell command. -* Clone the repository. If you're planning on contributing code to the project (which we [LOVE](CONTRIBUTING.md)), begin by forking this repo with the Fork button in the top-right corner of this screen. -* Use git clone to copy your fork onto your local machine. -```command -$ git clone https://github.com/YOUR_GITHUB_USERNAME_HERE/openfoodnetwork -``` -* Otherwise, if you just want to get things running, clone from the OFN main repo: -```command -$ git clone git@github.com:openfoodfoundation/openfoodnetwork.git -``` -* Go at the root of the app: -```command -$ cd openfoodnetwork -``` -* You have two choices of images you can build. The default Ubuntu image is most similar to the production servers but is larger in size and will take longer to build. Alternatively, you can use the Alpine image which is smaller, faster to setup, and should be compatible with the Ubuntu. To download the Ubuntu Docker images, build the Docker containers, seed the database with sample data, AND log the screen output from these tasks: -```command -$ docker/build.ps1 -``` -* To use the alpine image to start with append the name of the Dockerfile: -```sh -$ docker/build.ps1 alpine.Dockerfile -``` +3. **Choose and Build Docker Image**: +* You have two choices of images you can build. The default Ubuntu image is most similar to the production servers but is larger in size and will take longer to build. Alternatively, you can use the Alpine image which is smaller, faster to setup, and should be compatible with the Ubuntu. However, it will need to be updated when ruby is updated. To download the Docker images, build the Docker containers, seed the database with sample data, AND log the screen output from these tasks: + - **Ubuntu Image** (larger, production-like): + - **Linux**: + ```sh + $ docker/build + ``` + - **Windows**: + ```command + $ docker/build.ps1 + ``` + - **Alpine Image** (smaller, faster): + - **Linux**: + ```sh + $ docker/build alpine.Dockerfile + ``` + - **Windows**: + ```command + $ docker/build.ps1 alpine.Dockerfile + ``` + +4. **Run the Rails Server**: * Run the Rails server and its required Docker containers: -```command -$ docker/server.ps1 -``` -* Alternatively, you can run the server in detached mode. This will return you to the terminal prompt without stopping the server. No logs will be displayed: -```sh -$ docker compose up -d -``` -* To stop the server, run: -```sh -$ docker compose down -``` -* To access the server logs, run: -```sh -$ docker compose logs -f -``` -* To run tests or access the server directly, you can use bash: -```sh -$ docker compose exec web bash -``` -You may need to wait several minutes before getting the server up and running properly. + - **Linux**: + ```sh + $ docker/server + ``` + - **Windows**: + ```command + $ docker/server.ps1 + ``` + * To run tests or access the server directly, you can use bash: + ```sh + $ docker compose exec web bash + ``` + +> **Note**: Windows users may need to enable PowerShell script execution by setting the RemoteSigned policy. See instructions [here](https://shellgeek.com/powershell-fix-running-scripts-is-disabled-on-this-system/). + * The default admin user is 'ofn@example.com' with the password 'ofn123'. * View the app in the browser at `http://localhost:3000`. * You will then get the trace of the containers in the terminal. You can stop the containers using Ctrl-C in the terminal.