diff --git a/Dockerfile b/Dockerfile index cb7e79b08ca..d6fb889d0bc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -89,4 +89,4 @@ RUN ./script/install-bundler RUN yarn install # Run bundler install in parallel with the amount of available CPUs -RUN bundle install --jobs="$(nproc)" +RUN bundle install --jobs="$(nproc)" \ No newline at end of file diff --git a/alpine.Dockerfile b/alpine.Dockerfile new file mode 100644 index 00000000000..abe1123c25b --- /dev/null +++ b/alpine.Dockerfile @@ -0,0 +1,32 @@ +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/docker-compose.yml b/compose.yaml similarity index 93% rename from docker-compose.yml rename to compose.yaml index 7bb7988a8ca..72837470e2c 100644 --- a/docker-compose.yml +++ b/compose.yaml @@ -1,5 +1,3 @@ -version: '3' - services: db: image: postgres:10.19 @@ -24,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 @@ -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 && diff --git a/docker/README.md b/docker/README.md index 78285ea587f..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,48 +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 ``` -* Download the Docker images, build the Docker containers, seed the database with sample data, AND log the screen output from these tasks: -```sh -$ docker/build -``` -* Run the Rails server and its required Docker containers: -```sh -$ docker/server -``` -* 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 -``` -* Download the Docker images, build the Docker containers, seed the database with sample data, AND log the screen output from these tasks: -```command -$ docker/build.ps1 -``` +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 -``` -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. diff --git a/docker/build b/docker/build index f3966951f8b..d97f1e62dfe 100755 --- a/docker/build +++ b/docker/build @@ -4,5 +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` -docker/build-log 2>&1 | tee log/build-$DATE.log +docker/build-log $1 2>&1 | tee log/build-$DATE.log 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 '##############################' 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