diff --git a/Dockerfile b/Dockerfile index d6fb889d0b..abe1123c25 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,92 +1,32 @@ -FROM ubuntu:20.04 +FROM ruby:3.1.4-alpine3.19 AS base +ENV LANG=C.UTF-8 \ + LC_ALL=C.UTF-8 \ + TZ=Europe/London \ + RAILS_ROOT=/usr/src/app +RUN apk --no-cache upgrade && \ + apk add --no-cache tzdata postgresql-client imagemagick imagemagick-jpeg && \ + apk add --no-cache --virtual wkhtmltopdf -ENV TZ Europe/London +WORKDIR $RAILS_ROOT -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +# Development dependencies +FROM base AS development-base +RUN apk add --no-cache --virtual .build-deps \ + build-base postgresql-dev git nodejs yarn && \ + apk add --no-cache --virtual .dev-utils \ + bash curl less vim chromium-chromedriver zlib-dev openssl-dev \ + readline-dev yaml-dev sqlite-dev libxml2-dev libxslt-dev libffi-dev vips-dev && \ + 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 -RUN echo "deb http://security.ubuntu.com/ubuntu bionic-security main" >> /etc/apt/sources.list +# Install yarn dependencies separately for caching +FROM development-base AS yarn-dependencies +COPY package.json yarn.lock ./ +RUN yarn install --frozen-lockfile -# 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 -ENV CONFIGURE_OPTS --disable-install-doc -ENV BUNDLE_PATH /bundles -ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so - -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 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)" \ No newline at end of file +# Install Ruby gems +FROM development-base +COPY . $RAILS_ROOT +COPY Gemfile Gemfile.lock ./ +RUN bundle install --jobs "$(nproc)" +COPY --from=yarn-dependencies $RAILS_ROOT/node_modules ./node_modules \ No newline at end of file diff --git a/Dockerfile.ubuntu b/Dockerfile.ubuntu new file mode 100644 index 0000000000..d6fb889d0b --- /dev/null +++ b/Dockerfile.ubuntu @@ -0,0 +1,92 @@ +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 +ENV CONFIGURE_OPTS --disable-install-doc +ENV BUNDLE_PATH /bundles +ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so + +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 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)" \ No newline at end of file diff --git a/Procfile.docker b/Procfile.docker new file mode 100644 index 0000000000..3ad3b7c78d --- /dev/null +++ b/Procfile.docker @@ -0,0 +1,5 @@ +# Foreman Procfile for Docker env. Start all dev server processes with: `bundle exec foreman start -f Procfile.docker` + +webpack: WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server +sidekiq: DEV_CACHING=true bundle exec sidekiq -q mailers -q default +rails: WEBPACKER_DEV_SERVER_HOST=0.0.0.0 DEV_CACHING=true bundle exec rails s -p 3000 -b 0.0.0.0 diff --git a/alpine.Dockerfile b/alpine.Dockerfile deleted file mode 100644 index abe1123c25..0000000000 --- a/alpine.Dockerfile +++ /dev/null @@ -1,32 +0,0 @@ -FROM ruby:3.1.4-alpine3.19 AS base -ENV LANG=C.UTF-8 \ - LC_ALL=C.UTF-8 \ - TZ=Europe/London \ - RAILS_ROOT=/usr/src/app -RUN apk --no-cache upgrade && \ - apk add --no-cache tzdata postgresql-client imagemagick imagemagick-jpeg && \ - apk add --no-cache --virtual wkhtmltopdf - -WORKDIR $RAILS_ROOT - -# Development dependencies -FROM base AS development-base -RUN apk add --no-cache --virtual .build-deps \ - build-base postgresql-dev git nodejs yarn && \ - apk add --no-cache --virtual .dev-utils \ - bash curl less vim chromium-chromedriver zlib-dev openssl-dev \ - readline-dev yaml-dev sqlite-dev libxml2-dev libxslt-dev libffi-dev vips-dev && \ - 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 - -# Install yarn dependencies separately for caching -FROM development-base AS yarn-dependencies -COPY package.json yarn.lock ./ -RUN yarn install --frozen-lockfile - -# Install Ruby gems -FROM development-base -COPY . $RAILS_ROOT -COPY Gemfile Gemfile.lock ./ -RUN bundle install --jobs "$(nproc)" -COPY --from=yarn-dependencies $RAILS_ROOT/node_modules ./node_modules \ No newline at end of file diff --git a/compose.yaml b/compose.yaml index 72837470e2..5572e3e4ef 100644 --- a/compose.yaml +++ b/compose.yaml @@ -18,6 +18,7 @@ services: build: . ports: - 3000:3000 + - 3035:3035 volumes: - .:/usr/src/app - gems:/bundles @@ -27,51 +28,18 @@ services: depends_on: - db - redis - - webpack environment: + DOCKER: true 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 && - rm -f tmp/pids/server.pid && + bash -c "rm -f tmp/pids/server.pid && (bundle check || bundle install) && bundle exec rake db:create && yarn install && - bundle exec rails s -p 3000 -b '0.0.0.0'" - webpack: - build: . - command: ./bin/webpack-dev-server - volumes: - - .:/usr/src/app - - gems:/bundles - ports: - - '3035:3035' - environment: - NODE_ENV: development - RAILS_ENV: development - WEBPACKER_DEV_SERVER_HOST: 0.0.0.0 - worker: - tty: true - stdin_open: true - build: . - volumes: - - .:/usr/src/app - - gems:/bundles - - ./config/database.yml:/usr/src/app/config/database.yml - depends_on: - - db - - redis - environment: - OFN_DB_HOST: db - OFN_REDIS_URL: redis://redis - OFN_REDIS_JOBS_URL: redis://redis - command: > - bash -c "wait-for-it -t 30 db:5432 && - (bundle check || bundle install) && - bundle exec sidekiq -q mailers -q default" + bundle exec foreman start -f Procfile.docker" volumes: gems: postgres: diff --git a/docker/README.md b/docker/README.md index 10a919e51d..9b6ff65e40 100644 --- a/docker/README.md +++ b/docker/README.md @@ -40,8 +40,8 @@ $ cd openfoodnetwork ``` 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): +* You have two choices of images you can build. The default Alpine image is faster to setup and should be compatible with Ubuntu setup. Alternatively, you can use the Ubuntu image which is most similar to the production servers but is larger in size and will take longer to build. Note that the Alpine image 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: + - **Alpine Image** (smaller, faster): - **Linux**: ```sh $ docker/build @@ -50,14 +50,14 @@ $ cd openfoodnetwork ```command $ docker/build.ps1 ``` - - **Alpine Image** (smaller, faster): + - **Ubuntu Image** (larger, production-like): - **Linux**: ```sh - $ docker/build alpine.Dockerfile + $ docker/build Dockerfile.ubuntu ``` - **Windows**: ```command - $ docker/build.ps1 alpine.Dockerfile + $ docker/build.ps1 Dockerfile.ubuntu ``` 4. **Run the Rails Server**: