From 0db5f3a4dc0fc2297e32ed8956f6b4f0b2b63a00 Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Fri, 4 Feb 2022 17:41:36 +0000 Subject: [PATCH] Fix docker compose flow (#275) * Make docker-compose actually useful * Add docker-compose debug instructions to readme * Add db seed for docker-compose instructions to readme * Update readme to include migrate step * DB connection --- Dockerfile | 2 +- Dockerfile_dev | 33 +++++++++++++++++++++++++++++++++ Gemfile.lock | 4 ++-- README.md | 28 ++++++++++++++++++++-------- config/database.yml | 4 ++-- docker-compose.yml | 8 +++----- 6 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 Dockerfile_dev diff --git a/Dockerfile b/Dockerfile index b9bfae502..c6bc9d990 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,7 @@ RUN apk add --no-cache build-base yarn postgresql-dev git # Install bundler to run bundle exec # This should be the same version as the Gemfile.lock -RUN gem install bundler:2.2.3 --no-document +RUN gem install bundler:2.3.4 --no-document # Install gems defined in Gemfile COPY .ruby-version Gemfile Gemfile.lock /app/ diff --git a/Dockerfile_dev b/Dockerfile_dev new file mode 100644 index 000000000..aac15d8c6 --- /dev/null +++ b/Dockerfile_dev @@ -0,0 +1,33 @@ +# Build compilation image +FROM ruby:3.0.3-alpine3.14 as builder + +# The application runs from /app +WORKDIR /app + +# Add the timezone as it's not configured by default in Alpine +RUN apk add --update --no-cache tzdata && cp /usr/share/zoneinfo/Europe/London /etc/localtime && echo "Europe/London" > /etc/timezone + +RUN apk add --no-cache build-base yarn postgresql-dev git bash + +# Install bundler to run bundle exec +# This should be the same version as the Gemfile.lock +RUN gem install bundler:2.3.4 --no-document + +# Install gems defined in Gemfile +COPY .ruby-version Gemfile Gemfile.lock /app/ + +ARG BUNDLE_FLAGS="--jobs=4 --no-binstubs --no-cache" +RUN bundle install ${BUNDLE_FLAGS} + +# Install node packages defined in package.json, including webpack +COPY package.json yarn.lock /app/ +RUN yarn install --frozen-lockfile + +# Copy all files to /app (except what is defined in .dockerignore) +COPY . /app/ + +ENV PORT=8080 + +EXPOSE ${PORT} + +CMD RAILS_ENV=${RAILS_ENV} bundle exec rake db:migrate && bundle exec rails s -e ${RAILS_ENV} -p ${PORT} --binding=0.0.0.0 diff --git a/Gemfile.lock b/Gemfile.lock index b985f123e..a4c12148f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,7 +8,7 @@ GIT GIT remote: https://github.com/baarkerlounger/devise.git - revision: ac956cc2a58daa57e071202967a212a1dac08053 + revision: 6d0b6b52a9d0e87ae6d9f9acb562169751623078 branch: dluhc-fixes specs: devise (4.8.1) @@ -31,7 +31,7 @@ GIT GIT remote: https://github.com/tagliala/activeadmin.git - revision: d1492c54e76871d95f3a7ff20e445b48f455d4cb + revision: 920f14393bcf56cea9dc155276d20bcd10f4fa85 branch: feature/railties-7 specs: activeadmin (2.9.0) diff --git a/README.md b/README.md index f75b018c9..47db577a0 100644 --- a/README.md +++ b/README.md @@ -29,25 +29,37 @@ Pre-requisites: 4. Run the database migrations:\ `rake db:migrate` -5. Install the frontend depenencies:\ +5. Seed the database if required:\ +`rake db:seed` + +6. Install the frontend depenencies:\ `yarn install` -6. Start the Rails server:\ +7. Start the Rails server:\ `bundle exec rails s` The Rails server will start on . ### Using Docker -```sh -docker-compose build -docker-compose run --rm app rails db:create -docker-compose up -``` +1. Build the image:\ +`docker-compose build` + +2. Run the database migrations:\ +`docker-compose run --rm app /bin/bash -c 'rake db:migrate'` + +3. Seed the database if required:\ +`docker-compose run --rm app /bin/bash -c 'rake db:seed'` + +4. To be able to debug with Pry run the app using:\ +`docker-compose run --service-ports app` + +If this is not needed you can run `docker-compose up` as normal + The Rails server will start on . -Note `docker-compose` runs the production docker image (`RAILS_ENV=production`) as the Dockerfile doesn’t include development gems to keep the image size down. + ## Infrastructure diff --git a/config/database.yml b/config/database.yml index f04b2a776..74e6632ef 100644 --- a/config/database.yml +++ b/config/database.yml @@ -28,8 +28,8 @@ default: &default development: <<: *default - database: data_collector_development - host: localhost + database: <%= ENV['DB_DATABASE'] || 'data_collector_development' %> + host: <%= ENV['DB_HOST'] || 'localhost' %> staging: <<: *default diff --git a/docker-compose.yml b/docker-compose.yml index 0ca8930a1..fc50f354e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ volumes: services: db: - image: postgres:13.3-alpine + image: postgres:13.5-alpine restart: always # To preserve data between runs of docker-compose, we mount a folder from the host machine. volumes: @@ -20,9 +20,7 @@ services: app: build: context: . - dockerfile: ./Dockerfile - args: - GIT_COMMIT_SHA: DOCKER_COMPOSE_GIT_COMMIT_SHA + dockerfile: ./Dockerfile_dev ports: - 8080:8080 depends_on: @@ -32,7 +30,7 @@ services: - DB_DATABASE=data_collector - DB_USERNAME=postgres - DB_PASSWORD=password - - RAILS_ENV=production + - RAILS_ENV=development - RAILS_SERVE_STATIC_FILES=true - RAILS_LOG_TO_STDOUT=true - SECRET_KEY_BASE=abcd1234