zoedsoupe

zoedsoupe

Why my release don't get env vars?

I’m following these two tutorials:
https://thoughtbot.com/blog/deploying-elixir-to-aws-elastic-beanstalk-with-docker

And I can create a release without so much effort, however, creating a .env file and then sourcing it doesn’t get effect…

My solution was to declare each env var manually, on one-line shot and run migrations and start the app.

Am I doing something wrong?

First 10 of 17 Posts Switch mode

NobbZ

NobbZ

Do you export from the .env file?

zoedsoupe

zoedsoupe OP

Yeah I exported!

NobbZ

NobbZ

Can you then please tell more about your process?

How does your Dockerfile look like? Your entrypoint script? Your relevant config files?

zoedsoupe

zoedsoupe OP

The problem was that I was using System.fetch_env!/1, so all env vars weren’t loading on compile time… Changing to System.get_env/2 solved the issue!

However I’m getting two new problem now:

1 - When I create a release with MIX_ENV=prod mix release and then run _build/prod/rel/conts/bin/conts eval Conts.Release.migrate I get the following error:

** (RuntimeError) connect raised KeyError exception: key :database not found.

This is my runtime.exs:

import Config

secret_key_base = System.get_env("SECRET_KEY_BASE")
session_cookie_name = System.get_env("SESSION_COOKIE_NAME")
session_cookie_signing_salt = System.get_env("SESSION_COOKIE_SIGNING_SALT")
session_cookie_encryption_salt = System.get_env("SESSION_COOKIE_ENCRYPTION_SALT")

app_port = String.to_integer(System.get_env("PORT", "4000"))
app_hostname = System.get_env("HOST", "localhost")

db_url = System.get_env("DB_URL")
pool_size = String.to_integer(System.get_env("POOL_SIZE", "10"))

config :conts, Conts.Repo,
  adapter: Ecto.Adapters.Postgres,
  url: db_url,
  pool_size: pool_size,
  ssl: false

config :conts, ContsWeb.Endpoint,
  load_from_system_env: true,
  http: [:inet6, port: {:system, "PORT"}],
  secret_key_base: secret_key_base,
  session_cookie_name: session_cookie_name,
  session_cookie_signing_salt: session_cookie_signing_salt,
  session_cookie_encryption_salt: session_cookie_encryption_salt

config :conts,
  app_port: app_port

config :conts,
  app_hostname: app_hostname

The strange part is that if I set :show_sensitive_data_on_connection_error to true, this is the output:

** (KeyError) key :database not found in: [pool_index: 1, types: Postgrex.DefaultTypes, hostname: "localhost", username: "matthew", port: 5432, repo: Conts.Repo, telemetry_prefix: [:conts, :repo], otp_app: :conts, timeout: 15000, adapter: Ecto.Adapters.Postgres, show_sensitive_data_on_connection_error: true, ssl: false, pool_size: 2, pool: DBConnection.ConnectionPool]

Why the :username is getting my system’s username? I don’t even set that anywhere…

2 - When I run mix test, I got this error on controllers: ** (ArgumentError) cookie store expects conn.secret_key_base to be set

However, on test env the secret_key_base wasn’t set by default?

this is my config.exs:

# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
#
# This configuration file is loaded before any dependency and
# is restricted to this project.

# General application configuration
use Mix.Config

config :conts,
  ecto_repos: [Conts.Repo],
  generators: [binary_id: true]

# Configures the endpoint
config :conts, ContsWeb.Endpoint,
  url: [host: "localhost"],
  secret_key_base: "JqlSibgU/Ykrwvzz1ByJn2xayNDJlXkuBzmqHIW2yILDXSI3IHB2VbTqde/sIQw4",
  render_errors: [view: ContsWeb.ErrorView, accepts: ~w(html json), layout: false],
  pubsub_server: Conts.PubSub,
  live_view: [signing_salt: "LvrsHaeP"]

# Configures Elixir's Logger
config :logger, :console,
  format: "$time $metadata[$level] $message\n",
  metadata: [:request_id]

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

if Mix.env() != :prod do
  config :git_hooks,
    auto_install: true,
    verbose: true,
    hooks: [
      pre_commit: [
        tasks: [
          {:cmd, "mix format"},
          {:cmd, "mix credo --strict"}
        ]
      ],
      pre_push: [
        verbose: false,
        tasks: [
          {:cmd, "mix dialyzer"},
          {:cmd, "mix test"},
          {:cmd, "echo 'success!'"}
        ]
      ]
    ]
end

# Mailing configs
email_domain = "boosting.tech"

config :conts, Conts.Mailing,
  adapter: Bamboo.LocalAdapter,
  open_email_in_browser_url: "http://localhost:4000/sent_emails"

# Pow
config :conts, :pow,
  user: Conts.Accounts.User,
  repo: Conts.Repo,
  extensions: [PowResetPassword, PowEmailConfirmation],
  web_module: ContsWeb,
  controller_callbacks: Pow.Extension.Phoenix.ControllerCallbacks,
  mailer_backend: Conts.Mailing,
  web_module_mailer: Conts

# Conts custom configs

config :conts, mailing_default_from_name: "Sistema Conts"
config :conts, mailing_default_from_email: "noreplyconts@boosting.tech"

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"

Other files:

Dockerfile:

# Set global arg for multistage
ARG DB_URL

FROM elixir:1.11.2-alpine AS builder

RUN apk add --no-cache yarn build-base

RUN mkdir /app
WORKDIR /app

ARG DB_URL
ARG SECRET_KEY_BASE
ARG SESSION_COOKIE_NAME
ARG SESSION_COOKIE_SIGNING_SALT
ARG SESSION_COOKIE_ENCRYPTION_SALT

# Mix env
ENV MIX_ENV=prod \
    DB_URL=$DB_URL \
    SECRET_KEY_BASE=$SECRET_KEY_BASE \
    SESSION_COOKIE_NAME=$SESSION_COOKIE_NAME \
    SESSION_COOKIE_SIGNING_SALT=$SESSION_COOKIE_SIGNING_SALT \
    SESSION_COOKIE_ENCRYPTION_SALT=$SESSION_COOKIE_ENCRYPTION_SALT 

# Cache elixir deps
ADD mix.exs mix.lock ./
RUN mix local.rebar
RUN mix local.hex --force
RUN mix do deps.get, deps.compile

# Same with node deps
COPY assets assets
RUN yarn --cwd assets

COPY lib lib
COPY config config
COPY priv priv

# Run frontend build, compile, and digest assets
RUN yarn --cwd assets deploy && \
    cd - && \
    mix do compile, phx.digest

RUN mix release

# ---- Application Stage ----
FROM alpine AS app

ARG DB_URL

ENV MIX_ENV=prod \
    DB_URL=$DB_URL \
    SHELL=/bin/fish

# Intall needed packages
RUN apk --no-cache upgrade && \
    apk add --no-cache openssl \
      ncurses-libs postgresql-client fish

# Copy over the build artifact from the previous step and create a non root user
RUN adduser -D -h /home/app app
WORKDIR /home/app
COPY --from=builder /app/_build .
RUN chown -R app: ./prod
USER app

COPY entrypoint.sh .

# Run the Phoenix app
CMD ["./entrypoint.sh"]

entrypoint.sh:

#!/bin/sh
# Docker entrypoint script.

# Wait until Postgres is ready
while ! pg_isready -q -d $DB_URL 
do
  echo "$(date) - waiting for database to start"
  sleep 2
done

echo "Connected to the database"

./prod/rel/conts/bin/conts eval Conts.Release.migrate

./prod/rel/conts/bin/conts start
NobbZ

NobbZ

What is DB_URL in the system environment?

Do you perhaps have no dockerignore file which contains _build and deps and therefore partial builds slip into your container, which again might cause config values to exist that you do not want to exist?

zoedsoupe

zoedsoupe OP

Well, I’m trying to generate the release on my local machine to test, and DB_URL is being set to postgresql://postgres:postgres@localhost/conts_dev

I’m creating the release as:

MIX_ENV=prod DB_URL=postgresql://postgres:postgres@localhost/conts_dev mix release
NobbZ

NobbZ

And how do you run it?

zoedsoupe

zoedsoupe OP

runned as: _build/prod/rel/conts/bin/conts eval Conts.Release.migrate

NobbZ

NobbZ

Before that have you explicitely set and exported the DB_URL?

runtime.exs is re read on each start, but not on build at all, so you have to have the environment set on start, not on build.

zoedsoupe

zoedsoupe OP

Ok, locally I dind’t export the DB_URL, executing in fish shel: setenv DB_URL postgresql://postgres:postgres@localhost:5432/conts_dev, solves the first problem…

In production (Dockerfile) the DB_URL will be set “globally”, so I won’t get this error!

Than you so much! You accompanying me helps so much (:

But the test problem persists…

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement