CAIOHOBORGHI

CAIOHOBORGHI

How to use environment variables with docker without using phoenix?

With Phoenix it works but with only elixir is always null.

Showing Posts 1 to 10

stefanchrobot

stefanchrobot

It works the same. Just make sure you’re not fetching the ENV variables in config/config.exs or config/prod.exs - those are evaluated at build time. Either use config/runtime.exs or read in the app.

CAIOHOBORGHI

CAIOHOBORGHI OP

Yeah, I’m reading on the app using System.get_env(“NameOfEnv”) but for some reason they’re always nil.

I read in somewhere that it worked with Phoenix because they handle env variables in a different way

Sebb

Sebb

In a directory with a file test.exs containing:

IO.inspect(System.get_env("MY_VAR"), label: :env)

run

$ docker run -e MY_VAR=Hello -v $(pwd):/ws -w /ws -it --rm elixir:1.11.4 elixir test.exs
env: "Hello"

does that work?

CAIOHOBORGHI

CAIOHOBORGHI OP

Hello again Sebb, thanks for the help.

I’m using

mix run

To start the application and running

System.get_env("MY_VAR")

Inside a file being “supervised” by a Supervisor, would this change anything?

I’ll test the code you’ve sent

Sebb

Sebb

Running in a supervisor does not affect the environment you see. But running in docker does.

If you see the variable in the shell you run the application in - try:

echo $MY_VAR

Sytem.get_env will also see it.

CAIOHOBORGHI

CAIOHOBORGHI OP

Really weird thing is happening… With docker run -e […ENVS] it does work perfectly, but using docker-compose it doesn’t.

I attached to the container after started with docker compose and executed

echo $MY_VAR

in the shell and it printed with success, but for some reason when running with docker-compose and trying to System.get_env(“MY_VAR”) it’s null

CAIOHOBORGHI

CAIOHOBORGHI OP

This is my Dockerfile

FROM elixir:1.11-slim
RUN mkdir /app
COPY . /app
WORKDIR /app

# Install hex
RUN mix local.hex --force

#Install rebar
RUN mix local.rebar --force

#Get dependencies
RUN mix deps.get

#Run
RUN mix

This is my docker-compose.yml

version: "3.2"

services:
  checker:
    build:
      context: .
    environment:
      MY_VAR: "hello" 
    restart: on-failure

Mix.exs:

defmodule StockAlert.MixProject do
  use Mix.Project

  def project do
    [
      app: :stock_alert,
      version: "0.1.0",
      elixir: "~> 1.11.0",
      deps: deps()
    ]
  end

  # Configuration for the OTP application.
  #
  # Type `mix help compile.app` for more information.
  def application do
    [
      mod: {StockAlert.Application, []},
      extra_applications: [:elastix,:lager, :logger, :runtime_tools]
    ]
  end

  # Specifies your project dependencies.
  #
  # Type `mix help deps` for examples and options.
  defp deps do
    [
      {:elastix, "~> 0.10.0"},
      {:gettext, "~> 0.11"},
      {:jason, "~> 1.0"},
      {:websockex, "~> 0.4.2"},
      {:poison, "~> 3.1"},
      {:amqp, "~> 2.1"},
      {:json, "~> 1.4"}
    ]
  end
end

StockAlert.Application

defmodule StockAlert.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application
  require Logger

  @registry :workers_registry

  def start(_type, _args) do
    mvar = System.get_env("MY_VAR")
    Logger.info "MY_VAR-> #{mvar}"
  end

  def registry(), do: @registry
end
CAIOHOBORGHI

CAIOHOBORGHI OP

And the log:

#12 97.50 Generated stock_alert app
#12 98.42 [info] MY_VAR ->
stefanchrobot

stefanchrobot

Not sure if this is the root of the issue, but this should be mix run.

CAIOHOBORGHI

CAIOHOBORGHI OP

Tryed with mix run but no luck

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews