Docker: Rails Imagemagick and rmagick gem fails to deploy

Hey there!

Since about two days I’m trying to debug this issue that I have with imagemagick and I don’t know how to continue…

I’m running a rails server on a ruby docker image. Deployment has worked before adding the rmagick gem but since I added the rmagick gem it fails to deploy. At first the installation of ‘gem rmagick’ failed due to the lack of imagemagick. Then I added imagemagick libmagickwand-dev to the Dockerfile and looking at the logs I can see that rmagick installs successfully. However, when trying to run the server in the docker container I get this message:

LoadError: libMagickCore-6.Q16.so.6: cannot open shared object file: No such file or directory - /app/vendor/bundle/ruby/3.1.0/gems/rmagick-5.0.0/lib/RMagick2.so

However, if I build the dockerfile locally and I go into it’s shell I can see that the file exists.
Also, when I try to run convert which is an imagemagick command, I get convert: not found even though I have installed the imagemagick package inside the Dockerfile.

Can anyone point me to a possible solution please? Thanks a lot!

I got it to work by adding pkg-config libmagickwand-dev to BUILD_PACKAGES and imagemagick to DEPLOY_PACKAGES.

root@3287313b105d85:/# convert --version
Version: ImageMagick 6.9.11-60 Q16 x86_64 2021-01-25 https://imagemagick.org
Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP(4.5) 
Delegates (built-in): bzlib djvu fftw fontconfig freetype heic jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png tiff webp wmf x xml zlib
root@3287313b105d85:/# /app/bin/rails console
W, [2022-10-18T15:05:51.717539 #634]  WARN -- : You are running SQLite in production, this is generally not recommended. You can disable this warning by setting "config.active_record.sqlite3_production_warning=false".
Loading production environment (Rails 7.0.4)
irb(main):001:0> require 'rmagick'
=> false
irb(main):002:0> 
1 Like

YEEEESSSS! Thank you!!! :tada::partying_face:
I wasn’t aware of the difference in the build packages and the deploy packages. I guess the FROM base as build_deps command makes the difference and prevents the imagemagick package to be available in the running container

pkg-config wasn’t needed to get it running

Thanks again.

Short version is: smaller images load faster. Some packages are only required at build time.

FROM base as build_depsstarts a separate image. FROM build_deps as gemsstarts yet another image. COPY --from=gems` copies specific directories from the gem directory to the base.

1 Like

hey, where exactly you added it!?

I am having a problem that might be related, and missing proper documentation.

#16 86.14 Running ‘configure’ for sqlite3 3.39.4… OK
#16 86.14 Running ‘compile’ for sqlite3 3.39.4… OK
#16 86.14 Running ‘install’ for sqlite3 3.39.4… OK
#16 86.14 Activating sqlite3 3.39.4 (from
#16 86.14 /app/vendor/bundle/ruby/3.1.0/gems/sqlite3-1.5.3/ports/x86_64-linux-gnu/sqlite3/3.39.4)…
#16 86.14
#16 86.14 Could not configure the build properly (pkg_config). Please install either the
#16 86.14 pkg-config utility or the pkg-config rubygem.
#16 86.14
#16 86.14 *** extconf.rb failed ***
#16 86.14 Could not create Makefile due to some reason, probably lack of necessary
#16 86.14 libraries and/or headers. Check the mkmf.log file for more details. You may
#16 86.14 need configuration options.

Sorry for the cross-post, but feeling it is better than opening a new one.

Inside the Dockerfile under ARG BUILD_PACKAGES= I added libmagickwand-dev
and under FROM base inside ARG DEPLOY_PACKAGES= I added imagemagick

2 Likes

That problem can be addressed by adding pkg-config to the list of BUILD_PACKAGES. I’m not sure why @flov didn’t need this, but it doesn’t hurt to add it.

Meanwhile, I’m working on a new scanner that will automatically add things like this based on the contents of the Gemfile. See scan for rmagick · rubys/fly.io-rails@0c13fb1 · GitHub for the change to handle rmagick.

Meanwhile, I’m working on a new scanner that will automatically add things like this based on the contents of the Gemfile.

wow, sounds an Homeric work to map all the needed system libs per gem.

I added pkg-config in the Dockerfile and as a gem, but now have a

#25 1.747 LoadError: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.33’ not found (required by /app/vendor/bundle/ruby/3.1.0/gems/sqlite3-1.5.3/lib/sqlite3/sqlite3_native.so) - /app/vendor/bundle/ruby/3.1.0/gems/sqlite3-1.5.3/lib/sqlite3/sqlite3_native.so

#25 1.747 Caused by:
#25 1.751 LoadError: cannot load such file – sqlite3/3.1/sqlite3_native

also tried to add libsqlite3-dev in the BUILD_PACKAGE without success.

For sqlite3, you will also need to add a library to the DEPLOY_PACKAGES:

If you are just starting out, there are quite a few other changes required in order to use sqlite3 - you will need to create a volume, mount it, set an environment variable, and change when db:migrate is run.

If you are willing to give my new gem a try, delete your Dockerfile, fly.toml, .dockerignore, and lib/tasks/fly.rake and then run the following:

bundle add fly.io-rails
bin/rails generate fly:app --nomad

I had been searched a fix for a while, thanks!!