Problems to run VegaLite.Export.to_svg() inside a docker container

I have problem get VegaLite.Export.to_svg() to run in a docker container. The image is based on a tweaked Dockerfile generated with mix phx.gen.release --docker, like this:

# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian
# instead of Alpine to avoid DNS resolution issues in production.
# This file is based on these images:
#   - - for the build image
#   - - for the release image
#   - - resource for finding needed packages
#   - Ex: hexpm/elixir:1.14.1-erlang-
ARG DEBIAN_VERSION=bullseye-20210902-slim


FROM ${BUILDER_IMAGE} as builder

# install build dependencies
RUN apt-get update -y && apt-get install -y build-essential git \
  && apt-get clean && rm -f /var/lib/apt/lists/*_*

# prepare build dir

# install hex + rebar
RUN mix local.hex --force && \
  mix local.rebar --force

# set build ENV
ENV MIX_ENV="prod"

# install mix dependencies
COPY mix.exs mix.lock ./
RUN mix deps.get --only $MIX_ENV
RUN mkdir config

# copy compile-time config files before we compile dependencies
# to ensure any relevant config change will trigger the dependencies
# to be re-compiled.
COPY config/config.exs config/${MIX_ENV}.exs config/
RUN mix deps.compile

COPY priv priv

COPY lib lib

COPY assets assets

# compile assets
RUN mix assets.deploy

# Compile the release
RUN mix compile

# Changes to config/runtime.exs don't require recompiling the code
COPY config/runtime.exs config/

COPY rel rel
RUN mix release

# start a new build stage so that the final image will only contain
# the compiled release and other runtime necessities

RUN apt-get update -y && apt-get install -y libstdc++6 openssl libncurses5 locales curl chromium gcc g++ make build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev \
  && apt-get clean && rm -f /var/lib/apt/lists/*_*

# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen


RUN curl -fsSL | bash - && \
  apt-get install -y nodejs

RUN node --version
RUN npm install -g vega vega-lite canvas

WORKDIR "/app"
# RUN chown nobody /app

# set runner ENV
ENV MIX_ENV="prod"

# Only copy the final release from the build stage
COPY --from=builder /app/_build/${MIX_ENV}/rel/vega ./
# COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/vega ./

# USER nobody

CMD ["/app/bin/server"]

The logs for this container show this error:

14:38:48.616 [info] Running VegaWeb.Endpoint with cowboy 2.9.0 at :::4000 (http)
14:38:48.617 [info] Access VegaWeb.Endpoint at http://localhost:443
14:38:50.325 request_id=F08TOwoSlyo1rTcAAAAD [info] GET /
14:38:50.325 request_id=F08TOwoSlyo1rTcAAAAD [debug] Processing with VegaWeb.PageController.home/2
  Parameters: %{}
  Pipelines: [:browser]
14:38:50.644 request_id=F08TOwoSlyo1rTcAAAAD [info] Sent 500 in 318ms
14:38:50.644 request_id=F08TOwoSlyo1rTcAAAAD [debug] Converted error {:badmatch, {"Unknown command: \"bin\"\n\nTo see a list of supported npm commands, run:\n  npm help\n", 1}} to 500 response
14:38:50.645 [error] #PID<0.1642.0> running VegaWeb.Endpoint (connection #PID<0.1641.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /
** (exit) an exception was raised:
    ** (MatchError) no match of right hand side value: {"Unknown command: \"bin\"\n\nTo see a list of supported npm commands, run:\n  npm help\n", 1}
        (vega_lite 0.1.6) lib/vega_lite/export.ex:186: VegaLite.Export.npm_bin/2
        (vega_lite 0.1.6) lib/vega_lite/export.ex:162: VegaLite.Export.find_npm_script!/2
        (vega_lite 0.1.6) lib/vega_lite/export.ex:146: VegaLite.Export.node_convert/3
        (vega 0.1.0) lib/vega_web/controllers/page_html/home.html.heex:3: anonymous fn/2 in VegaWeb.PageHTML.home/1
        (phoenix_live_view 0.18.18) lib/phoenix_live_view/engine.ex:137: Phoenix.HTML.Safe.Phoenix.LiveView.Rendered.to_iodata/1
        (phoenix_live_view 0.18.18) lib/phoenix_live_view/engine.ex:153: Phoenix.HTML.Safe.Phoenix.LiveView.Rendered.to_iodata/3
        (phoenix 1.7.2) lib/phoenix/controller.ex:1010: anonymous fn/5 in Phoenix.Controller.template_render_to_iodata/4
        (telemetry 1.2.1) /app/deps/telemetry/src/telemetry.erl:321: :telemetry.span/3

Someone knows how to fix this issue?
It looks like it does not find the actual node executable or so?

Thanks for help! :smiley:

1 Like

I created a small repo to reproduce the error here:

1 Like

Looks like the command that that code tries to run (npm bin) was removed for NPM 9.0:

1 Like

Good find, thank you for pointing me into the right direction. I saw you already created an issue at github:

Thank you very much for your help.

1 Like

So the temporary solution for the original problem would be to install an older node version. It looks like version 16 works in this case:

RUN curl -fsSL | bash - && \
  apt-get install -y nodejs
1 Like