Skip to content

🐞 Service bindings don't work with Elixir SDK #6091

Closed

Description

@bs1180

What is the issue?

I've tried converting the NodeJS docker with postgres example into Elixir, but the .with_service_binding doesn't seem to work properly - an error is always thrown. If I remove the service binding the workflow completes ok (although the tests fail, because they need the database). My workflow is:

defmodule Mix.Tasks.ElixirWithDagger.Test do
  use Mix.Task

  @impl Mix.Task
  def run(_args) do
    Application.ensure_all_started(:dagger)

    client = Dagger.connect!()

    app =
      client
      |> Dagger.Client.host()
      |> Dagger.Host.directory(".", exclude: ["deps", "_build"])

    database =
      client
      |> Dagger.Client.container()
      |> Dagger.Container.from("postgres:16")
      |> Dagger.Container.with_env_variable("POSTGRES_PASSWORD", "test")
      |> Dagger.Container.with_exec(~w"postgres")
      |> Dagger.Container.with_exposed_port(5432)
      |> Dagger.Container.as_service()

    {:ok, _} =
      client
      |> Dagger.Client.container()
      |> Dagger.Container.from("hexpm/elixir:1.15.7-erlang-26.1.2-debian-bullseye-20231009-slim")
      |> Dagger.Container.with_exec(~w"apt-get update")
      |> Dagger.Container.with_exec(~w"apt-get install -y build-essential git")
      |> Dagger.Container.with_exec(~w"apt-get clean")
      |> Dagger.Container.with_exec(~w"rm -f /var/lib/apt/lists/*_*")
      |> Dagger.Container.with_mounted_directory("/app", app)
      |> Dagger.Container.with_workdir("/app")
      |> Dagger.Container.with_exec(~w"mix local.hex --force")
      |> Dagger.Container.with_exec(~w"mix local.rebar --force")
      |> Dagger.Container.with_exec(~w"mix deps.get")
      |> Dagger.Container.with_service_binding("db", database)
      |> Dagger.Container.with_env_variable("DB_HOST", "db")
      |> Dagger.Container.with_env_variable("DB_USER", "postgres")
      |> Dagger.Container.with_env_variable("DB_PASSWORD", "test")
      |> Dagger.Container.with_env_variable("DB_NAME", "postgres")
      |> Dagger.Container.with_exec(~w"mix test")
      |> Dagger.Sync.sync()

    Dagger.close(client)

    IO.puts("Tests succeeded!")
  end
end

Log output

The output from the Dagger CLI is:

┃ ** (Protocol.UndefinedError) protocol String.Chars not implemented for {:query_timeout, :infinity} of type Tuple. This protocol is implemented for the following type(s): Atom, B
┃ itString, Date, DateTime, Decimal, Float, Integer, List, NaiveDateTime, Phoenix.LiveComponent.CID, Postgrex.Copy, Postgrex.Query, Time, URI, Version, Version.Requirement
┃     (elixir 1.15.7) lib/string/chars.ex:3: String.Chars.impl_for!/1
┃     (elixir 1.15.7) lib/string/chars.ex:22: String.Chars.to_string/1
┃     (dagger 0.9.3) lib/dagger/query_builder.ex:82: Dagger.QueryBuilder.Selection.encode_value/1
┃     (elixir 1.15.7) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
┃     (dagger 0.9.3) lib/dagger/query_builder.ex:65: Dagger.QueryBuilder.Selection.encode_value/1
┃     (dagger 0.9.3) lib/dagger/query_builder.ex:76: anonymous fn/1 in Dagger.QueryBuilder.Selection.encode_value/1
┃     (elixir 1.15.7) lib/enum.ex:1701: anonymous fn/3 in Enum.map/2
┃     (stdlib 5.0.2) maps.erl:416: :maps.fold_1/4

Steps to reproduce

Run the above workflow with dagger run mix elixir_with_dagger.test

SDK version

dagger v0.9.3 (registry.dagger.io/engine) darwin/arm64

OS version

macOS

Activity

  1. allancalix commented on Nov 10, 2023

    @allancalix

    So either the type definitions are wrong or the implementation is wrong I'm not sure which. What's happening is that when this gets encoded into some graphql query it's attempting to serialize the entire Dagger.Service type. What should probably be happening is with_service_binding should map the service to its ID and encode that into the query.

    You can do this yourself to work around this issue, not ideal but it worked for me:

        database =
          client
          |> Dagger.Client.container()
          |> Dagger.Container.from("postgres:16")
          |> Dagger.Container.with_env_variable("POSTGRES_PASSWORD", "test")
          |> Dagger.Container.with_exec(~w"postgres")
          |> Dagger.Container.with_exposed_port(5432)
          |> Dagger.Container.as_service()
          
          {:ok, database_id} = Dagger.Service.id(database)
    
        {:ok, _} =
          client
          |> Dagger.Client.container()
          |> Dagger.Container.from("hexpm/elixir:1.15.7-erlang-26.1.2-debian-bullseye-20231009-slim")
          |> Dagger.Container.with_exec(~w"apt-get update")
          |> Dagger.Container.with_exec(~w"apt-get install -y build-essential git")
          |> Dagger.Container.with_exec(~w"apt-get clean")
          |> Dagger.Container.with_exec(~w"rm -f /var/lib/apt/lists/*_*")
          |> Dagger.Container.with_mounted_directory("/app", app)
          |> Dagger.Container.with_workdir("/app")
          |> Dagger.Container.with_exec(~w"mix local.hex --force")
          |> Dagger.Container.with_exec(~w"mix local.rebar --force")
          |> Dagger.Container.with_exec(~w"mix deps.get")
          |> Dagger.Container.with_service_binding("db", database_id)
          |> Dagger.Container.with_env_variable("DB_HOST", "db")
          |> Dagger.Container.with_env_variable("DB_USER", "postgres")
          |> Dagger.Container.with_env_variable("DB_PASSWORD", "test")
          |> Dagger.Container.with_env_variable("DB_NAME", "postgres")
          |> Dagger.Container.with_exec(~w"mix test")
          |> Dagger.Sync.sync()
  2. bs1180 commented on Nov 10, 2023

    @bs1180
    Author

    Oh wonderful, that worked - thank you for the quick reply. The workaround is fine but I'll leave the issue open?

    Screenshot 2023-11-11 at 00 05 12
  3. wingyplus commented on Nov 11, 2023

    @wingyplus
    Contributor

    Thank you for the report. I can surely say that it is a bug.

    Let me investigate and come up with a fix tomorrow. 🙇‍♂️

  4. added a commit that references this issue on Nov 22, 2023
    5989170
  5. added 6 commits that reference this issue on Nov 24, 2023
    65c1a33
    ed85173
    c8b9e29
    975cc41
    4aa68b8
    e151fba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/sdk/elixirkind/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions