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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews