Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Alpine Image for Docker #12905

Merged
merged 15 commits into from
Dec 1, 2024

Conversation

macanudo527
Copy link
Collaborator

@macanudo527 macanudo527 commented Oct 11, 2024

What? Why?

This replaces adds a new option in addition to the Ubuntu image, with a more lightweight image optimized for Ruby, which reduces the size of the images on the computer and simplifies the build. It also eliminates the need to prepend commands with bundle exec and the need for any version manager in the image.

I also added OFN_REDIS_TEST_URL so that tests using SpreePreferences now pass.

Some disadvantages:
When the ruby version is upgraded, the image will need to be updated and rebuilt.
Since Open Food Network uses specific versions of default gems that might not match the Alpine image's default gems, adjustments will need to be made. Currently, OFN's BigDecimal needed to be upgraded to 3.1.1 and the Alpine image's rake and bundler needed to be upgraded to match OFN's.

What should we test?

Build and spin up the docker container:

docker/build

docker/server

Verify that there are no fatal errors.
Visit HTTP://localhost:3000/

This has been tested under Ubuntu, Windows w/WSL, and Windows, but not on Mac

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

@rioug
Copy link
Collaborator

rioug commented Oct 14, 2024

@macanudo527 there is a failed spec rspec ./spec/lib/reports/sales_tax_totals_by_order_spec.rb:141 that seems related to the BigDecimal upgrade, could you have a look at it ?

@macanudo527
Copy link
Collaborator Author

The last error is a bit strange. It is saving the unit_presentation wrong:

 Spree::Variant Create (1.0ms)  INSERT INTO "spree_variants" ("weight", "product_id", "cost_currency", "unit_value", "display_as", 
"variant_unit", "unit_presentation", "created_at", "updated_at", "shipping_category_id", "primary_taxon_id", "supplier_id") VALUES
 ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"  [["weight", "0.0"], ["product_id", 12], ["cost_currency", "AUD"], 
["unit_value", 0.5001], ["display_as", ""], ["variant_unit", "volume"], ["unit_presentation", "500.09mL"], ["created_at", "2024-10-15 
23:48:04.298869"], ["updated_at", "2024-10-15 23:48:04.298869"], ["shipping_category_id", 1], ["primary_taxon_id", 5], 
["supplier_id", 3]]
08:48:04 rails.1   |   ↳ app/models/spree/product.rb:261:in `ensure_standard_variant'

@rioug
Copy link
Collaborator

rioug commented Oct 16, 2024

@macanudo527 the last Product Refactor PR has been merged , you'll probably need to rebase your branch

@macanudo527 macanudo527 force-pushed the docker/use_alpine_image branch from aa041d5 to 3640057 Compare October 16, 2024 02:18
@sigmundpetersen sigmundpetersen force-pushed the docker/use_alpine_image branch from f629c0d to 3e0be85 Compare October 23, 2024 07:01
@macanudo527
Copy link
Collaborator Author

@sigmundpetersen Did you make an accidental update?

@sigmundpetersen
Copy link
Contributor

Hi @macanudo527 I just rebased your branch.
The build error may be due to the recent merge of #12787 .
I can also try rebuild a few times to see if it's a flaky error

@macanudo527
Copy link
Collaborator Author

Hi @macanudo527 I just rebased your branch.
The build error may be due to the recent merge of #12787 .
I can also try rebuild a few times to see if it's a flaky error

OK, that's kind of strange the compare looked like it was strangely incomplete, but maybe the tests are just flakey. Thanks!

@sigmundpetersen
Copy link
Contributor

Rebuild was green so I'm moving this to Code Review again 👍

@macanudo527 macanudo527 force-pushed the docker/use_alpine_image branch from 4209818 to 1b3ebcb Compare October 24, 2024 01:59
Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

I'm not quite sure on the way forward though.

Dockerfile Outdated Show resolved Hide resolved
Gemfile Outdated Show resolved Hide resolved
@macanudo527 macanudo527 force-pushed the docker/use_alpine_image branch 3 times, most recently from ba3fe27 to 7b03d15 Compare November 7, 2024 15:11
@macanudo527 macanudo527 force-pushed the docker/use_alpine_image branch from 0772ad7 to f8e0c7f Compare November 9, 2024 13:08
@macanudo527
Copy link
Collaborator Author

@mkllnk I revised it so that you can optionally build it with the Alpine image by adding it to the build script.
There is a significant difference in build time and size:
Alpine:
2m6.737s
Ubuntu:
7m52.583s

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks gone now ! But I think we should wait for #12950 before merging this has there are still some BigDecimal related changes in this PR.

If you are keen to play with Docker, it would be nice to dockerise OFN, ie have a separate Image for the OFN code, and each dependency (Postgres, Redis etc...) It would be a good start to moving toward a Docker based deployment, especially now that Kamal exists

@macanudo527 macanudo527 force-pushed the docker/use_alpine_image branch from 7752cf6 to 9fb90d9 Compare November 12, 2024 02:05
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look so I could keep up to date with what's happening. I have a few queries.

alpine.Dockerfile Show resolved Hide resolved
docker/README.md Outdated Show resolved Hide resolved
docker/README.md Outdated Show resolved Hide resolved
@dacook dacook added the blocked label Nov 13, 2024
@dacook

This comment was marked as resolved.

@macanudo527

This comment was marked as resolved.

@macanudo527 macanudo527 force-pushed the docker/use_alpine_image branch from 9fb90d9 to e5648dd Compare November 13, 2024 07:30
@macanudo527 macanudo527 force-pushed the docker/use_alpine_image branch 2 times, most recently from 927df70 to eb334d0 Compare November 14, 2024 02:09
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for following up!

@macanudo527 macanudo527 force-pushed the docker/use_alpine_image branch from eb334d0 to cbd0003 Compare November 27, 2024 00:38
@macanudo527
Copy link
Collaborator Author

I rebased this with the BigDecimal update. I was able to build and run the server without issues on Ubuntu. It actually seemed to be more responsive with the Alpine image.

@dacook dacook self-assigned this Nov 27, 2024
@dacook dacook removed the blocked label Nov 27, 2024
@dacook
Copy link
Member

dacook commented Nov 27, 2024

Thanks, I just tried again but still getting the error. Is there a cache/image/container/thing in Docker that I need to clear?

time docker/build alpine.Dockerfile
...
#11 [stage-3 3/4] RUN bundle install --jobs "$(nproc)"
#11 sha256:22af8fcf7ca753038cc5e843a85c8ba6a4f8ffd503af391a5e0b6b1f29f5ac4d
#11 3.249 Bundler 2.3.26 is running, but your lockfile was generated with 2.4.3. Installing Bundler 2.4.3 and restarting using that version.
...
#11 46.95 Fetching bigdecimal 3.1.8
#11 47.09 Installing bigdecimal 3.1.8 with native extensions
...
--------------------------------------------------------------
BEGIN: docker compose run web bundle exec rake ofn:sample_data
--------------------------------------------------------------
...
Container openfoodnetwork-webpack-1  Started
bundler: failed to load command: rake (/bundles/ruby/3.1.0/bin/rake)
/usr/local/src/rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/bundler-2.4.3/lib/bundler/definition.rb:526:in `materialize': Could not find bigdecimal-3.1.8 in locally installed gems (Bundler::GemNotFound)
	from /usr/local/src/rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/bundler-2.4.3/lib/bundler/definition.rb:187:in `specs'

@macanudo527
Copy link
Collaborator Author

macanudo527 commented Nov 27, 2024

Thanks, I just tried again but still getting the error. Is there a cache/image/container/thing in Docker that I need to clear?

Did you docker/nuke it? It looks like it cached an old image.

You can try docker build -f alpine.Dockerfile --no-cache

Or try to clean everything yourself:
docker system prune --all --force

And then try to build it again.

I built it successfully in Windows. It is still very slow, but it works if someone wants to try developing in Windows.

@macanudo527 macanudo527 force-pushed the docker/use_alpine_image branch from cbd0003 to e7213db Compare November 27, 2024 23:39
@dacook
Copy link
Member

dacook commented Nov 28, 2024

Thanks, nuked it and rebuilt, and it finished! Took a while, but that's because my computer went to sleep while I was out for a walk.

------------------------------------------------------------
END: docker compose run web bundle exec rake ofn:sample_data
------------------------------------------------------------

real	40m16.717s
user	0m5.964s
sys	0m10.746s

docker/server starts up, but I get lots of errors from mailer jobs. I think at least some of these are expected, as we can't use letter_opener in this context:

openfoodnetwork-worker-1   | 2024-11-28T01:34:53.156Z pid=1 tid=wnl class=ActionMailer::MailDeliveryJob jid=06fd261284b5683104cf1b99 ERROR: Error performing ActionMailer::MailDeliveryJob (Job ID: 5f42d723-4bd0-4fa8-bb4e-cbd853d1f7b9) from Sidekiq(default) in 38108.12ms: ActionView::Template::Error (Operation not permitted @ rb_sysopen - /usr/src/app/tmp/cache/assets/sprockets/v3.0/QL/QLOM7Wl3cnammmkDjPOx3XtjVOjPYLYVOkfKsMW4Q1o.cache):
...
openfoodnetwork-worker-1   | xprop:  unable to open display ''
openfoodnetwork-worker-1   | 2024-11-28T01:38:10.601Z pid=1 tid=wn1 class=ActionMailer::MailDeliveryJob jid=c9effd497546c9da9fc9ae7e INFO: Delivered mail [email protected] (486.5ms)
openfoodnetwork-worker-1   | 2024-11-28T01:38:10.608Z pid=1 tid=wn1 class=ActionMailer::MailDeliveryJob jid=c9effd497546c9da9fc9ae7e INFO: Performed ActionMailer::MailDeliveryJob (Job ID: 2c47ad1e-0996-4385-8998-74817f949a5b) from Sidekiq(default) in 235498.99ms
openfoodnetwork-worker-1   | xprop:  unable to open display ''
openfoodnetwork-worker-1   | 2024-11-28T01:38:10.673Z pid=1 tid=wn1 class=ActionMailer::MailDeliveryJob jid=c9effd497546c9da9fc9ae7e elapsed=236.099 INFO: done

But the JS also fails. I can't open the login modal for example. I couldn't see any related errors in the server log.
Screenshot 2024-11-28 at 12 48 29 PM

@macanudo527
Copy link
Collaborator Author

docker/server starts up, but I get lots of errors from mailer jobs. I think at least some of these are expected, as we can't use letter_opener in this context:

This is expected. Actually the current default docker build does the same thing. I hope I can find a work around once I get the other things working.

But the JS also fails. I can't open the login modal for example. I couldn't see any related errors in the server log.

This is very odd. I've never had this issue actually and it loads just fine for me. I'm guessing it is related to webpacker.

I'm just getting the 2 non-unique ids error:

Screenshot from 2024-11-29 08-53-37

And you are missing the cart button, too?

@dacook
Copy link
Member

dacook commented Dec 1, 2024

When in doubt, restart and try again...
It works for me now ✅ 🎉 (macOS 14)
Screenshot 2024-12-02 at 9 52 07 AM

@dacook
Copy link
Member

dacook commented Dec 1, 2024

Ready to merge! Thank you Neal, I hope this helps more new developers get started!

@dacook dacook changed the title Use Alpine Image for Docker Add Alpine Image for Docker Dec 1, 2024
@dacook dacook merged commit cb78153 into openfoodfoundation:master Dec 1, 2024
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants