How to use environment variables with docker without using phoenix?
With Phoenix it works but with only elixir is always null.
How to use environment variables with docker without using phoenix?
With Phoenix it works but with only elixir is always null.
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.
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
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?
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
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.
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
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
And the log:
#12 97.50 Generated stock_alert app
#12 98.42 [info] MY_VAR ->
Not sure if this is the root of the issue, but this should be mix run
.
Tryed with mix run
but no luck
web:
environment:
- DEBUG=1
This one was tricky… Found the answer in docker-compose build environment variable - Stack Overflow
As the mix run is running on the Dockerfile, the environment section of docker-compose won’t work, so I had to use the args section on the docker-compose
version: "3.2"
services:
checker:
build:
context: .
args:
- MY_VAR=hello
restart: on-failure
And also declare the arg in the dockerfile
FROM elixir:1.11-slim
RUN mkdir /app
COPY . /app
WORKDIR /app
ARG MY_VAR
ENV MY_VAR $MY_VAR
# Install hex
RUN mix local.hex --force
#Install rebar
RUN mix local.rebar --force
#Get dependencies
RUN mix deps.get
RUN mix run
Thank you all for your help
This runs mix
during build time of the container. At that point in time docker-compose
has not yet set the environment.
You want to use CMD
or ENTRYPOINT
depending on your exact needs.
Yeah, this is the best answer, with it I don’t even need to use the args on docker-compose.
Thank you!