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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews