Make usage of Docker for development easier by avoiding COPY operations

This commit is contained in:
Patrick Gansterer 2021-03-02 19:18:57 +01:00
parent 38193e43a9
commit 38d49dedb8
5 changed files with 63 additions and 47 deletions

View file

@ -14,25 +14,12 @@ ENV PORT=3000 \
\
CHROMIUM_FLAGS=--no-sandbox \
\
BUNDLE_PATH=/home/app/bundle \
BUNDLE_APP_CONFIG=/home/app/bundle/config
BUNDLE_PATH=/usr/local/bundle \
BUNDLE_APP_CONFIG=/usr/local/bundle/config
# Run app and all commands as user 'app'. This avoids changing permissions
# for files in mounted volume. Symlink for similarity with production image.
RUN adduser --gecos GECOS --disabled-password --shell /bin/bash app && \
ln -s /home/app/src /usr/src/app
USER app
WORKDIR /app
WORKDIR /home/app/src
# Copy files needed for installing gem dependencies, and install them.
COPY Gemfile Gemfile.lock ./
COPY plugins ./plugins
RUN bundle config build.nokogiri "--use-system-libraries" && \
bundle install --frozen -j 4
# Copy the application code
COPY . ./
RUN bundle config build.nokogiri "--use-system-libraries"
EXPOSE 3000

View file

@ -59,7 +59,7 @@ Rails.application.configure do
# Mailcatcher config, start mailcatcher from console with 'mailcatcher'
# Mailcatcher can be installed by gem install mailcatcher
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: ENV.fetch("MAILCATCHER_PORT_25_TCP_ADDR", "localhost"), port: ENV.fetch("MAILCATCHER_PORT_25_TCP_PORT", 1025) }
config.action_mailer.smtp_settings = { address: ENV.fetch("MAILCATCHER_ADDRESS", "localhost"), port: ENV.fetch("MAILCATCHER_PORT", 1025) }
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

View file

@ -28,19 +28,20 @@ afterwards.
## Setup
Then setup foodsoft development (this will take some time, containers needs
to be pulled from docker registry and a lot dependencies needs to be installed)
Then start the database server and setup foodsoft development (this will take
some time, containers needs to be pulled from docker registry and a lot
dependencies needs to be installed)
docker-compose -f docker-compose-dev.yml up -d mariadb
docker-compose -f docker-compose-dev.yml run --rm foodsoft \
bundle install
docker-compose -f docker-compose-dev.yml run --rm foodsoft \
bundle exec rake foodsoft:setup_development_docker
Optionally an initial database (here seeded with `small.en`) can be loaded by running
docker-compose -f docker-compose-dev.yml run --rm foodsoft \
bundle exec rake foodsoft:setup_development
Do not enable mailcatcher, because this is already included as a docker image.
Then create an initial database (here seeded with `small.en`) by running
docker-compose -f docker-compose-dev.yml up -d mariadb && sleep 15
docker-compose -f docker-compose-dev.yml run --rm foodsoft \
bundle exec rake db:create db:schema:load db:seed:small.en
bundle exec rake db:schema:load db:seed:small.en
To avoid having to pass the `-f` argument all the time, it is recommended to setup
an alias, e.g. by adding the following line to your ~/.bashrc
@ -66,7 +67,8 @@ Open a rails console
Setup the test database
docker-compose-dev run --rm foodsoft bundle exec rake db:create db:schema:load RAILS_ENV=test DATABASE_URL=mysql2://root:secret@mariadb/test?encoding=utf8
docker-compose-dev run --rm mariadb mariadb --host=mariadb --password=secret --execute="CREATE DATABASE test"
docker-compose-dev run --rm foodsoft bundle exec rake db:schema:load RAILS_ENV=test DATABASE_URL=mysql2://root:secret@mariadb/test?encoding=utf8mb4
Run the tests
@ -83,6 +85,10 @@ Jump in a running container for debugging.
Go to [http://localhost:1080](http://localhost:1080)
### Manage database
Go to [http://localhost:2080](http://localhost:2080)
### Gemfile updates
As the gem bundle is stored in a volume, you can run

View file

@ -1,4 +1,4 @@
version: '2'
version: '3'
services:
foodsoft:
@ -13,14 +13,16 @@ services:
dockerfile: Dockerfile-dev
command: ./proc-start worker
volumes:
- bundle:/home/app/bundle
- .:/home/app/src
- bundle:/usr/local/bundle
- .:/app
environment:
- DATABASE_URL=mysql2://root:secret@mariadb/development?encoding=utf8
- DATABASE_URL=mysql2://root:secret@mariadb/development?encoding=utf8mb4
- REDIS_URL=redis://redis:6379
- QUEUE=foodsoft_notifier
- TEST_DATABASE_URL=mysql2://root:secret@mariadb/test?encoding=utf8
- TEST_DATABASE_URL=mysql2://root:secret@mariadb/test?encoding=utf8mb4
- DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true
- MAILCATCHER_ADDRESS=mailcatcher
- MAILCATCHER_PORT=25
mailcatcher:
image: tophfr/mailcatcher
@ -28,14 +30,24 @@ services:
- "1080:80"
mariadb:
image: mariadb:10.1
image: mariadb:10.5
environment:
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_DATABASE=development
volumes:
- mariadb:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
- PMA_HOST=mariadb
- PMA_USER=root
- PMA_PASSWORD=secret
ports:
- "2080:80"
redis:
image: redis:3.2-alpine
image: redis:6.2-alpine
volumes:
bundle:

View file

@ -34,10 +34,19 @@ namespace :foodsoft do
setup_development
setup_database
start_mailcatcher
puts yellow "All done! Your foodcoft should be running smoothly."
puts yellow "All done! Your foodsoft setup should be running smoothly."
start_server
end
desc "Setup foodsoft"
task :setup_development_docker do
puts yellow "This task will help you get your foodcoop running in development via docker."
setup_app_config
setup_development
setup_run_rake_db_setup
puts yellow "All done! Your foodsoft setup should be running smoothly via docker."
end
namespace :setup do
desc "Initialize stock configuration"
task :stock_config do
@ -65,21 +74,23 @@ def setup_database
database = ask("What kind of database do you use?\nOptions:\n(1) MySQL\n(2) SQLite", ["1", "2"])
if database == "1"
puts yellow "Using MySQL..."
%x(cp #{Rails.root.join("#{file}.MySQL_SAMPLE")} #{Rails.root.join(file)})
%x(cp -p #{Rails.root.join("#{file}.MySQL_SAMPLE")} #{Rails.root.join(file)})
elsif database == "2"
puts yellow "Using SQLite..."
%x(cp #{Rails.root.join("#{file}.SQLite_SAMPLE")} #{Rails.root.join(file)})
%x(cp -p #{Rails.root.join("#{file}.SQLite_SAMPLE")} #{Rails.root.join(file)})
end
reminder(file)
puts blue "IMPORTANT: Edit (rake-generated) config/database.yml with valid username and password for EACH env before continuing!"
finished = ask("Finished?\nOptions:\n(y) Yes", ["y"])
if finished
setup_run_rake_db_setup if finished
end
def setup_run_rake_db_setup
Rake::Task["db:setup"].reenable
db_setup = capture_stdout { Rake::Task["db:setup"].invoke }
puts db_setup
end
end
def setup_app_config
@ -88,7 +99,7 @@ def setup_app_config
return nil if skip?(file)
puts yellow "Copying #{file}..."
%x(cp #{sample} #{Rails.root.join(file)})
%x(cp -p #{sample} #{Rails.root.join(file)})
reminder(file)
end
@ -97,7 +108,7 @@ def setup_development
return nil if skip?(file)
puts yellow "Copying #{file}..."
%x(cp #{Rails.root.join("#{file}.SAMPLE")} #{Rails.root.join(file)})
%x(cp -p #{Rails.root.join("#{file}.SAMPLE")} #{Rails.root.join(file)})
reminder(file)
end