|
| 1 | +--- |
| 2 | +title: "Elixir" |
| 3 | +description: "{{% vendor/name %}} supports building and deploying applications written in Elixir. There is no default flavor for the build phase, but you can define it explicitly in your build hook. {{% vendor/name %}} Elixir images support both committed dependencies and download-on-demand. The underlying Erlang version is 22.0.7." |
| 4 | +--- |
| 5 | + |
| 6 | +{{% description %}} |
| 7 | + |
| 8 | +## Supported versions |
| 9 | + |
| 10 | +{{% major-minor-versions-note configMinor="true" %}} |
| 11 | + |
| 12 | +{{< image-versions image="elixir" status="supported" environment="grid" >}} |
| 13 | + |
| 14 | + |
| 15 | +{{% language-specification type="elixir" display_name="Elixir" %}} |
| 16 | + |
| 17 | +```yaml {configFile="app"} |
| 18 | +applications: |
| 19 | + # The app's name, which must be unique within the project. |
| 20 | + <APP_NAME>: |
| 21 | + type: 'elixir:<VERSION_NUMBER>' |
| 22 | +``` |
| 23 | +
|
| 24 | +For example: |
| 25 | +
|
| 26 | +```yaml {configFile="app"} |
| 27 | +applications: |
| 28 | + # The app's name, which must be unique within the project. |
| 29 | + app: |
| 30 | + type: 'elixir:{{% latest "elixir" %}}' |
| 31 | +``` |
| 32 | +
|
| 33 | +## Built-in variables |
| 34 | +
|
| 35 | +{{% vendor/name %}} exposes relationships and other configuration as [environment variables](../development/variables/_index.md). |
| 36 | +Most notably, it allows a program to determine at runtime what HTTP port it should listen on |
| 37 | +and what the credentials are to access [other services](../add-services/_index.md). |
| 38 | +
|
| 39 | +To get the `PORT` environment variable (the port on which your web application is supposed to listen) you would: |
| 40 | + |
| 41 | +```elixir |
| 42 | +String.to_integer(System.get_env("PORT") || "8888") |
| 43 | +``` |
| 44 | + |
| 45 | +Some of the environment variables are in JSON format and are base64 encoded. You would need to import a JSON parsing library such as [JSON](https://hexdocs.pm/json/readme.html) or [Poison](https://hexdocs.pm/poison/api-reference.html) to read those. (There is an example for doing this to decode the `PLATFORM_RELATIONSHIPS` environment variable in the section [below](#accessing-services-manually).) |
| 46 | + |
| 47 | +{{< note title="Tip">}} |
| 48 | +Remember `config/prod.exs` is evaluated at **build time** and has no access to runtime configuration. Use `config/releases.exs` to configure your runtime environment. |
| 49 | +{{< /note >}} |
| 50 | + |
| 51 | +## Building and running the application |
| 52 | + |
| 53 | +If you are using Hex to manage your dependencies, you need to specify the `MIX_ENV` environment variable: |
| 54 | + |
| 55 | +```yaml {configFile="app"} |
| 56 | +applications: |
| 57 | + app: |
| 58 | + type: 'elixir:{{% latest "elixir" %}}' |
| 59 | + variables: |
| 60 | + env: |
| 61 | + MIX_ENV: 'prod' |
| 62 | +``` |
| 63 | +The `SECRET_KEY_BASE` variable is generated automatically based on the [`PLATFORM_PROJECT_ENTROPY` variable](../development/variables/use-variables.md#use-provided-variables). |
| 64 | +You can change it. |
| 65 | + |
| 66 | +Include in your build hook the steps to retrieve a local Hex and `rebar`, and then run `mix do deps.get, deps.compile, compile` on your application to build a binary. |
| 67 | + |
| 68 | +```yaml {configFile="app"} |
| 69 | +applications: |
| 70 | + app: |
| 71 | + type: 'elixir:{{% latest "elixir" %}}' |
| 72 | + hooks: |
| 73 | + build: | |
| 74 | + mix local.hex --force |
| 75 | + mix local.rebar --force |
| 76 | + mix do deps.get --only prod, deps.compile, compile |
| 77 | +``` |
| 78 | + |
| 79 | +{{< note >}} |
| 80 | + |
| 81 | +That build hook works for most cases and assumes that your `mix.exs` file is located at [your app root](../create-apps/app-reference.md#root-directory). |
| 82 | + |
| 83 | +{{< /note >}} |
| 84 | + |
| 85 | +Assuming `mix.exs` is present at your app root and your build hook matches the above, |
| 86 | +you can then start it from the `web.commands.start` directive. |
| 87 | + |
| 88 | +The following basic app configuration is sufficient to run most Elixir applications. |
| 89 | + |
| 90 | + |
| 91 | +```yaml {configFile="app"} |
| 92 | +applications: |
| 93 | + app: |
| 94 | + type: 'elixir:{{% latest "elixir" %}}' |
| 95 | +
|
| 96 | + variables: |
| 97 | + env: |
| 98 | + MIX_ENV: 'prod' |
| 99 | +
|
| 100 | + hooks: |
| 101 | + build: | |
| 102 | + mix local.hex --force |
| 103 | + mix local.rebar --force |
| 104 | + mix do deps.get --only prod, deps.compile, compile |
| 105 | +
|
| 106 | + web: |
| 107 | + commands: |
| 108 | + start: mix phx.server |
| 109 | + locations: |
| 110 | + /: |
| 111 | + allow: false |
| 112 | + passthru: true |
| 113 | +``` |
| 114 | + |
| 115 | +Note that there is still an Nginx proxy server sitting in front of your application. If desired, certain paths may be served directly by Nginx without hitting your application (for static files, primarily) or you may route all requests to the Elixir application unconditionally, as in the example above. |
| 116 | + |
| 117 | +## Dependencies |
| 118 | + |
| 119 | +The recommended way to handle Elixir dependencies on {{% vendor/name %}} is using Hex. |
| 120 | +You can commit a `mix.exs` file in your repository and the system downloads the dependencies in your `deps` section using the build hook above. |
| 121 | + |
| 122 | +```elixir |
| 123 | + defp deps do |
| 124 | + [ |
| 125 | + {:platformshconfig, "~> 0.1.0"} |
| 126 | + ] |
| 127 | + end |
| 128 | +``` |
| 129 | + |
| 130 | +## Accessing Services |
| 131 | + |
| 132 | +{{% access-services version="2" %}} |
| 133 | + |
| 134 | +### Accessing Services Manually |
| 135 | + |
| 136 | +The services configuration is available in the environment variable `PLATFORM_RELATIONSHIPS`. |
| 137 | + |
| 138 | +Given a relationship defined in `{{< vendor/configfile "app" >}}`: |
| 139 | + |
| 140 | +```yaml {configFile="app"} |
| 141 | +applications: |
| 142 | + app: |
| 143 | + type: 'elixir:{{% latest "elixir" %}}' |
| 144 | + ... |
| 145 | + relationships: |
| 146 | + postgresdatabase: "dbpostgres:postgresql" |
| 147 | +``` |
| 148 | + |
| 149 | +Assuming you have in `mix.exs` the Poison library to parse JSON: |
| 150 | + |
| 151 | +```elixir |
| 152 | + defp deps do |
| 153 | + [ |
| 154 | + {:poison, "~> 3.0"} |
| 155 | + ] |
| 156 | + end |
| 157 | +``` |
| 158 | + |
| 159 | +And assuming you use `ecto` you could put in `config/config.exs`: |
| 160 | + |
| 161 | +```elixir |
| 162 | +relationships = Poison.decode!(Base.decode64!(System.get_env("PLATFORM_RELATIONSHIPS"))) |
| 163 | +[postgresql_config | _tail] = relationships["postgresdatabase"] |
| 164 | +
|
| 165 | +config :my_app, Repo, |
| 166 | + database: postgresql_config["path"], |
| 167 | + username: postgresql_config["username"], |
| 168 | + password: postgresql_config["password"], |
| 169 | + hostname: postgresql_config["host"] |
| 170 | +``` |
| 171 | + |
| 172 | +and setup Ecto during the deploy hook: |
| 173 | + |
| 174 | +```yaml |
| 175 | +deploy: | |
| 176 | + mix do ecto.setup |
| 177 | +``` |
0 commit comments