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

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
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
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews