Merge pull request #10305 from heroinedor/feature/docker-dev

Docker dev environment improvement
This commit is contained in:
David Cook
2023-01-31 10:52:37 +11:00
committed by GitHub
13 changed files with 121 additions and 29 deletions

View File

@@ -2,3 +2,4 @@
.gitignore
log/*
tmp/*
node_modules/

11
.gitattributes vendored Normal file
View File

@@ -0,0 +1,11 @@
# Set default behavior to automatically normalize line endings.
* text=auto
# Set line endings to LF, even on Windows. Otherwise, execution within Docker fails.
# See https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings#per-repository-settings
*.sh text eol=lf
# Same thing for following files, but they don't have an sh extension
pre-commit eol=lf
webpack-dev-server eol=lf
install-bundler eol=lf

View File

@@ -35,7 +35,10 @@ ENV BUNDLE_PATH /bundles
ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so
WORKDIR /usr/src/app
COPY .ruby-version .
# 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} && \

View File

@@ -20,11 +20,12 @@ $ sudo apt install docker-compose
```
* To run Docker commands as a regular user instead of as root (with sudo), follow the instructions at https://docs.docker.com/engine/install/linux-postinstall/.
#### Windows and Mac (untested)
#### Windows
* Docker installation instructions are at https://docs.docker.com/get-docker/.
* You may have to deselect the option to use Docker Compose V2 in Docker settings to make our scripts work.
## Getting Started
### Linux
* 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.
@@ -52,7 +53,39 @@ $ docker/server
* 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
```
* 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.
* 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).
### Troubleshooting
* If you get a PowerShell error saying that "execution of scripts is disabled on this system.", you may need to [activate the powershell script execution](https://shellgeek.com/powershell-fix-running-scripts-is-disabled-on-this-system/) on your Windows machine.
* If you are using Windows and having issues related to the ruby-build not finding a definition for the ruby version, you may need to follow these commands [here](https://stackoverflow.com/questions/2517190/how-do-i-force-git-to-use-lf-instead-of-crlf-under-windows/33424884#33424884) to fix your local git config related to line breaks.
* If youre getting the following error:
```sh
@@ -93,14 +126,14 @@ To fix this error on server startup, you need to bump `mini-racer` gem from `0.4
Based on [spree/spree_starter #984](https://github.com/spree/spree_starter/issues/984)
## Script Summary
* docker/build: This script builds the Docker containers specified for this app, seeds the database, and logs the screen output for these operations. After you use "git clone" to download this repository, run the docker/build script to start the setup process.
* docker/server: Use this script to run this app in the Rails server. This script executes the "docker-compose up" command and logs the results. If all goes well, you will be able to view this app on your local browser at http://localhost:3000/.
* docker/test: Use this script to run the entire test suite.
* docker/build(.ps1): This script builds the Docker containers specified for this app, seeds the database, and logs the screen output for these operations. After you use "git clone" to download this repository, run the docker/build script to start the setup process.
* docker/server(.ps1): Use this script to run this app in the Rails server. This script executes the "docker-compose up" command and logs the results. If all goes well, you will be able to view this app on your local browser at http://localhost:3000/.
* docker/test(.ps1): Use this script to run the entire test suite.
* docker/qtest: Use this script to run the entire test suite in quiet mode. The deprecation warnings are removed to make the test results easier to read.
* docker/run: Use this script to run commands within the Docker container. If you want shell access, enter "docker/run bash". To execute "ls -l" within the Docker container, enter "docker/run ls -l".
* docker/seed: Use this script to seed the database. Please note that this process is not compatible with simultaneously running the Rails server or tests.
* docker/seed(.ps1): Use this script to seed the database. Please note that this process is not compatible with simultaneously running the Rails server or tests.
* docker/nuke: Use this script to delete all Docker images and containers. This fully resets your Docker setup and is useful for making sure that the setup procedure specified for this app is complete.
* docker/cop: This script runs RuboCop.
* docker/cop(.ps1): This script runs RuboCop.

5
docker/build-log.ps1 Normal file
View File

@@ -0,0 +1,5 @@
Write-Host "Docker cleaning: remove old containers" -ForegroundColor Blue
docker-compose.exe down -v --remove-orphans
Write-Host "Docker build: set up the docker containers" -ForegroundColor Blue
docker-compose.exe build
Write-Host "Docker build finished"

8
docker/build.ps1 Normal file
View File

@@ -0,0 +1,8 @@
# This script does :
# - build the Docker container and log the output of several containers in a unique "build-yyyyMMdd-HHmmss.log" file
# - seed the app with sample data and log the output in "seed-yyyyMMdd-HHmmss.log" file
$DateTime=Get-Date -Format "yyyyMMdd-HHmmss"
docker/build-log.ps1 > log/build-$DateTime.log 2>&1
docker/seed.ps1 > log/seed-$DateTime.log 2>&1

4
docker/cop.ps1 Normal file
View File

@@ -0,0 +1,4 @@
# This script runs RuboCop
Write-Host "bundle exec rubocop : runs rubocop" -ForegroundColor Blue
docker-compose.exe run web bundle exec rubocop

13
docker/seed.ps1 Normal file
View File

@@ -0,0 +1,13 @@
# This is the data seeding script :
# - reset the database
# - prepare the database
# - seed the database with sample data
Write-Host "bundle exec rake db:reset : reset the dev and test databases" -ForegroundColor Blue
docker-compose.exe run web bundle exec rake db:reset
Write-Host "bundle exec rake db:test:prepare : prepare the database" -ForegroundColor Blue
docker-compose.exe run web bundle exec rake db:test:prepare
Write-Host "bundle exec rake ofn:sample_data : seed the database with sample data" -ForegroundColor Blue
docker-compose.exe run web bundle exec rake ofn:sample_data

6
docker/server.ps1 Normal file
View File

@@ -0,0 +1,6 @@
# This script launches the whole stack of containers (web server, database, webpack, redis, etc.)
$DateTime=Get-Date -Format "yyyyMMdd-HHmmss"
Write-Host "Docker-compose up: launches the whole stack of containers" -ForegroundColor Blue
docker-compose.exe up -d > log/server-$DateTime.log 2>&1
Write-Host "Docker-compose up finished : View this app in your web browser at http://localhost:3000/" -ForegroundColor Blue

View File

@@ -1,7 +1,23 @@
#!/bin/bash
set -e
# This script runs the entire test suite AND logs the screen output.
# This test script :
# - prepares the database for rspec tests
# - launches the rspec tests
DATE=`date +%Y%m%d-%H%M%S-%3N`
docker/test-log 2>&1 | tee log/test-$DATE.log
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
echo '------------------------------------------------------------'
echo 'END: docker-compose run web bundle exec rake db:test:prepare'
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
echo '--------------------------------------------------'
echo 'END: docker-compose run web bundle exec rspec spec'
echo '--------------------------------------------------'

View File

@@ -1,17 +0,0 @@
#!/bin/bash
echo '--------------------------------------------------------------'
echo 'BEGIN: docker-compose run web bundle exec rake db:test:prepare'
echo '--------------------------------------------------------------'
docker-compose run web bundle exec rake db:test:prepare
echo '------------------------------------------------------------'
echo 'END: docker-compose run web bundle exec rake db:test:prepare'
echo '------------------------------------------------------------'
echo '----------------------------------------------------'
echo 'BEGIN: docker-compose run web bundle exec rspec spec'
echo '----------------------------------------------------'
docker-compose run web bundle exec rspec spec
echo '--------------------------------------------------'
echo 'END: docker-compose run web bundle exec rspec spec'
echo '--------------------------------------------------'

9
docker/test.ps1 Normal file
View File

@@ -0,0 +1,9 @@
# This test script :
# - prepares the database for rspec tests
# - launches the rspec tests
$DateTime=Get-Date -Format "yyyyMMdd-HHmmss"
Write-Host "bundle exec rake db:test:prepare : prepare the database for rspec tests" -ForegroundColor Blue
docker-compose.exe run web bundle exec rake db:test:prepare > log/test-prepare-$DateTime.log 2>&1
Write-Host "bundle exec rspec spec : launch the rspec tests" -ForegroundColor Blue
docker-compose.exe run web bundle exec rspec spec > log/test-rspec-$DateTime.log 2>&1

View File

@@ -16,10 +16,10 @@ set -e
# `grep`: find the occurrences of "BUNDLED WITH" (-m is unnecessary and behaves different on macOS)
# `-A`: print the next line after "BUNDLED WITH" as well
# `-x -F`: find exactly that string without interpreting regex
# `-F`: find exactly that string
# `tail -n 1`: take the last line, the version line
# `tr -d`: delete all spaces, the indent before the version
version="$(grep -A 1 -x -F "BUNDLED WITH" Gemfile.lock | tail -n 1 | tr -d '[:space:]')"
version="$(grep -A 1 -F "BUNDLED WITH" Gemfile.lock | tail -n 1 | tr -d '[:space:]')"
# if the length of $version is zero
if [ -z "$version" ]; then