Skip to content

Commit

Permalink
Merge pull request openfoodfoundation#12905 from macanudo527/docker/u…
Browse files Browse the repository at this point in the history
…se_alpine_image

Add Alpine Image for Docker
  • Loading branch information
dacook authored Dec 1, 2024
2 parents db76cd1 + e7213db commit cb78153
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
32 changes: 32 additions & 0 deletions alpine.Dockerfile
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions docker-compose.yml → compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3'

services:
db:
image: postgres:10.19
Expand All @@ -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
Expand All @@ -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 &&
Expand Down
79 changes: 41 additions & 38 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,48 +33,50 @@ $ git clone https://github.com/YOUR_GITHUB_USERNAME_HERE/openfoodnetwork
```sh
$ git clone [email protected]: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 '[email protected]' 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 [email protected]: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 '[email protected]' 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.
Expand Down
2 changes: 1 addition & 1 deletion docker/build
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion docker/build-log
Original file line number Diff line number Diff line change
Expand Up @@ -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 '##############################'
9 changes: 8 additions & 1 deletion docker/build-log.ps1
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 7 additions & 1 deletion docker/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cb78153

Please sign in to comment.