Skip to content

Commit

Permalink
Simplify and improve docker setup.
Browse files Browse the repository at this point in the history
- Don't `bundle install` or pull in the Ruby base image multiple times.
- Instead of copying over the entire project, just copy over the parts that we need.
  This should make building the `elasticgraph` docker image faster.
- Provide a strong guarantee that no released ElasticGraph gems are being used, as
  we only want our local ElasticGraph gems to get built into the docker image.
- Use `ELASTICGRAPH_GEMS_PATH` env var instead of a `sed` command. The `sed`
  command is a bit harder to maintain, IMO, and could be brittle--the replaced
  code snippet may change in the future.
- Remove OpenSearch Dashboards. While they are often useful to include, we want
  to keep our demo setup as slimmed down as possible, and minimize the likelihood
  that users will run out of docker resources.
- Honor the `OPENSEARCH_VERSION` ENV var rather than hardcoding it to 2.18.0.
  • Loading branch information
myronmarston committed Feb 22, 2025
1 parent 20b0c5e commit 94acf14
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 58 deletions.
5 changes: 4 additions & 1 deletion config/docker_demo/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
/*/log/*
/*/tmp/*

# Ignore spec files.
/*/spec/*

# Ignore dockerfile
Dockerfile
Dockerfile
2 changes: 1 addition & 1 deletion config/docker_demo/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
OPENSEARCH_VERSION=2.18.0
ENV=local
ELASTICGRAPH_PORT=9393
ELASTICGRAPH_PORT=9393
67 changes: 26 additions & 41 deletions config/docker_demo/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,62 +1,47 @@
ARG PORT=9393
ARG RUBY_VERSION=3.4

# Use Ruby 3.x as the base image
FROM ruby:${RUBY_VERSION}

# Set working directory
WORKDIR /app

# Copy the entire project
COPY . .


# Retain just the files needed for building
RUN find . \! -name "Gemfile" \! -name "*.gemspec" -mindepth 2 -maxdepth 2 -print | xargs rm -rf
RUN find . \! -name "Gemfile*" \! -name "*.gemspec" -maxdepth 1 -type f | xargs rm

# Also need the version file. add it back
COPY elasticgraph-support/lib/elastic_graph/version.rb ./elasticgraph-support/lib/elastic_graph/version.rb


# Use Ruby 3.x as the base image
FROM ruby:${RUBY_VERSION}

WORKDIR /app/elasticgraph


# Copy files from the first build stage.
COPY --from=0 /app .

# Install Ruby dependencies
# Copy the minimal Gemfile and local ElasticGraph gems that we need.
COPY config/docker_demo/Gemfile .
COPY elasticgraph elasticgraph
COPY elasticgraph-admin elasticgraph-admin
COPY elasticgraph-datastore_core elasticgraph-datastore_core
COPY elasticgraph-graphql elasticgraph-graphql
COPY elasticgraph-indexer elasticgraph-indexer
COPY elasticgraph-json_schema elasticgraph-json_schema
COPY elasticgraph-local elasticgraph-local
COPY elasticgraph-opensearch elasticgraph-opensearch
COPY elasticgraph-query_registry elasticgraph-query_registry
COPY elasticgraph-rack elasticgraph-rack
COPY elasticgraph-schema_artifacts elasticgraph-schema_artifacts
COPY elasticgraph-schema_definition elasticgraph-schema_definition
COPY elasticgraph-support elasticgraph-support

# Change the `ElasticGraph::VERSION` constant to a version that will never match any of the released
# ElasticGraph gems. This ensures that when we `bundle install` in our demo project, it will not fall
# back to released ElasticGraph gems. If we've failed to copy over any of the ElasticGraph gems needed
# to boot the demo app, we want it to fail fast and tell us rather than pulling in released gems.
RUN sed -i 's/^\(\s*\)VERSION = ".*"/\1VERSION = "999.999.999"/' elasticgraph-support/lib/elastic_graph/version.rb

# Install dependencies
RUN bundle install

# Copy the entire project
COPY . .

# Running the new command will commit to git. Setup defaults
RUN git config --global user.email "[email protected]"
RUN git config --global user.name "Demo User"

# Use the elasticgraph gem local to the container
RUN sed -i 's|"#{VERSION}"|path: \"/app/elasticgraph\"|g' elasticgraph/lib/elastic_graph/cli.rb


# Why does this need to run a second time?
RUN bundle install

WORKDIR /app

# Create demo app using the locally build elasticgraph project
RUN BUNDLE_GEMFILE=/app/elasticgraph/Gemfile bundle exec elasticgraph new demo

# Create demo app using the locally built elasticgraph project
RUN ELASTICGRAPH_GEMS_PATH=/app bundle exec elasticgraph new demo --datastore opensearch

# Change work directory into the demo app
WORKDIR /app/demo

# Reference OpenSearch from the docker container
RUN sed -i 's/localhost:9293/opensearch:9200/g' config/settings/local.yaml


# Generate fake data and boot the graphiql api
# Generate fake data and boot the GraphiQL UI
CMD ["bundle", "exec", "rake", "index_fake_data:artists" ,"boot_graphiql[${PORT},--host=0.0.0.0,true]"]
16 changes: 16 additions & 0 deletions config/docker_demo/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 Block, Inc.
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
#
# frozen_string_literal: true

source "https://rubygems.org"

# This minimal Gemfile is used by the docker image and intentionally only contains
# what is needed to be able to run `bundle exec elasticgraph new`. The resulting
# demo project has its own `Gemfile` which will pull in the rest of the ElasticGraph
# gems.
gemspec path: "elasticgraph"
gemspec path: "elasticgraph-support"
18 changes: 3 additions & 15 deletions config/docker_demo/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ services:
context: ../../elasticgraph-local/lib/elastic_graph/local/opensearch/.
dockerfile: Dockerfile
args:
VERSION: 2.18.0
container_name: opensearch-2.18.0-${ENV}
VERSION: ${OPENSEARCH_VERSION}
container_name: opensearch-${OPENSEARCH_VERSION}-${ENV}
healthcheck:
interval: 10s
retries: 80
Expand Down Expand Up @@ -38,18 +38,6 @@ services:
- opensearch:/usr/share/opensearch/data
ports:
- 9200:9200
dashboards:
build:
context: ../../elasticgraph-local/lib/elastic_graph/local/opensearch/.
dockerfile: UI-Dockerfile
args:
VERSION: ${OPENSEARCH_VERSION}
container_name: dashboards-${OPENSEARCH_VERSION}-${ENV}
environment:
- OPENSEARCH_HOSTS=http://opensearch:9200
- DISABLE_SECURITY_DASHBOARDS_PLUGIN=true
ports:
- 5601:5601
elasticgraph:
build:
context: ../..
Expand All @@ -64,4 +52,4 @@ services:
opensearch:
condition: service_healthy
volumes:
opensearch:
opensearch:

0 comments on commit 94acf14

Please sign in to comment.