Problem creating a docker container for elixir project

I’m trying to create a docker container for a demo project. So I followed this steps

FROM bitwalker/alpine-elixir:1.7 as build

COPY . .

RUN export MIX_ENV=prod && \
    rm -Rf _build && \
    mix deps.get && \
    mix release

But after running this

docker build -t elixir-docker-guide .

I’m getting this error


Sending build context to Docker daemon  16.84MB
Step 1/3 : FROM bitwalker/alpine-elixir:1.7 as build
1.7: Pulling from bitwalker/alpine-elixir
8e3ba11ec2a2: Pull complete 
462178e4d842: Pull complete 
9e748cddddbb: Pull complete 
a6febdbdc7a2: Pull complete 
Digest: sha256:65f21812a0a9bb138ad210fba9e0eb38ae03c0a56c5b29d43fbd3eedf7bcb3ef
Status: Downloaded newer image for bitwalker/alpine-elixir:1.7
 ---> 1242a38c1217
Step 2/3 : COPY . .
 ---> 1a0d8a867026
Step 3/3 : RUN export MIX_ENV=prod &&     rm -Rf _build &&     mix deps.get &&     mix release
 ---> Running in 5379dc4c1305
** (RuntimeError) environment variable DATABASE_URL is missing.
For example: ecto://USER:PASS@HOST/DATABASE

    (stdlib) erl_eval.erl:677: :erl_eval.do_apply/6
    (stdlib) erl_eval.erl:446: :erl_eval.expr/5
    (stdlib) erl_eval.erl:126: :erl_eval.exprs/5
    (elixir) lib/code.ex:232: Code.eval_string/3
    (mix) lib/mix/config.ex:187: anonymous fn/2 in Mix.Config.__import__!/2
    (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
    (mix) lib/mix/config.ex:186: Mix.Config.__import__!/2
The command '/bin/sh -c export MIX_ENV=prod &&     rm -Rf _build &&     mix deps.get &&     mix release' returned a non-zero code: 1

How do I fix this?
and also can I use some particular files instead of copy . . ?

Provide required variable or do not depend on it beeing available at build time.

Also please remember that mix releases are only available since elixir 1.9.

Or are you using distillery prior to version 2.1?

here is my mix file

defmodule Eldeploy.MixProject do
  use Mix.Project

  def project do
    [
      app: :eldeploy,
      version: "0.1.0",
      elixir: "~> 1.7",
      elixirc_paths: elixirc_paths(Mix.env()),
      compilers: [:phoenix, :gettext] ++ Mix.compilers(),
      start_permanent: Mix.env() == :prod,
      aliases: aliases(),
      deps: deps()
    ]
  end

  # Configuration for the OTP application.
  #
  # Type `mix help compile.app` for more information.
  def application do
    [
      mod: {Eldeploy.Application, []},
      extra_applications: [:logger, :runtime_tools]
    ]
  end

  # Specifies which paths to compile per environment.
  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

  # Specifies your project dependencies.
  #
  # Type `mix help deps` for examples and options.
  defp deps do
    [
      {:phoenix, "~> 1.5.4"},
      {:phoenix_ecto, "~> 4.1"},
      {:ecto_sql, "~> 3.4"},
      {:postgrex, ">= 0.0.0"},
      {:phoenix_live_dashboard, "~> 0.2"},
      {:telemetry_metrics, "~> 0.4"},
      {:telemetry_poller, "~> 0.4"},
      {:gettext, "~> 0.11"},
      {:jason, "~> 1.0"},
      {:plug_cowboy, "~> 2.0"},
      {:distillery, "~> 2.1"}
    ]
  end

  # Aliases are shortcuts or tasks specific to the current project.
  # For example, to install project dependencies and perform other setup tasks, run:
  #
  #     $ mix setup
  #
  # See the documentation for `Mix` for more info on aliases.
  defp aliases do
    [
      setup: ["deps.get", "ecto.setup"],
      "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
      "ecto.reset": ["ecto.drop", "ecto.setup"],
      test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"]
    ]
  end
end

Yeah, your elixir Docker base image is pretty old.

Here I posted an example project that is building a release in Docker
Demo of distributed Elixir with libcluster and DNS

Lately I use the hexpm/elixir images. Here is the latest alpine image hexpm/elixir:1.10.4-erlang-23.0.3-alpine-3.12.0

About the missing variable: what do you have in your config.exs, prod.exs?

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 :eldeploy,
  ecto_repos: [Eldeploy.Repo]

# Configures the endpoint
config :eldeploy, EldeployWeb.Endpoint,
  url: [host: "localhost"],
  secret_key_base: "FzwEAieVRYvqVK82zUrKxFaTSK8tbb0J4qcOGXmuNbkzM1DJvEMWpd6BNXmqrv82",
  render_errors: [view: EldeployWeb.ErrorView, accepts: ~w(json), layout: false],
  pubsub_server: Eldeploy.PubSub,
  live_view: [signing_salt: "Ba3q2/XP"]

# 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

# 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"

prod.exs

use Mix.Config

# For production, don't forget to configure the url host
# to something meaningful, Phoenix uses this information
# when generating URLs.
#
# Note we also include the path to a cache manifest
# containing the digested version of static files. This
# manifest is generated by the `mix phx.digest` task,
# which you should run after static files are built and
# before starting your production server.
config :eldeploy, EldeployWeb.Endpoint,
  url: [host: "example.com", port: 80],
  cache_static_manifest: "priv/static/cache_manifest.json"

# Do not print debug messages in production
config :logger, level: :info

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
# to the previous section and set your `:url` port to 443:
#
#     config :eldeploy, EldeployWeb.Endpoint,
#       ...
#       url: [host: "example.com", port: 443],
#       https: [
#         port: 443,
#         cipher_suite: :strong,
#         keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
#         certfile: System.get_env("SOME_APP_SSL_CERT_PATH"),
#         transport_options: [socket_opts: [:inet6]]
#       ]
#
# The `cipher_suite` is set to `:strong` to support only the
# latest and more secure SSL ciphers. This means old browsers
# and clients may not be supported. You can set it to
# `:compatible` for wider support.
#
# `:keyfile` and `:certfile` expect an absolute path to the key
# and cert in disk or a relative path inside priv, for example
# "priv/ssl/server.key". For all supported SSL configuration
# options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1
#
# We also recommend setting `force_ssl` in your endpoint, ensuring
# no data is ever sent via http, always redirecting to https:
#
#     config :eldeploy, EldeployWeb.Endpoint,
#       force_ssl: [hsts: true]
#
# Check `Plug.SSL` for all available options in `force_ssl`.

# Finally import the config/prod.secret.exs which loads secrets
# and configuration from environment variables.
import_config "prod.secret.exs"

These 2 files seem to be good. So either somewhere else you have DATABASE_URL (check dev.exs, test.exs, prod.secret.exs or just do a project search). Or you have no config for ecto at all, but it is expected to have it).

Yes it is in prod.secret.exs.

# In this file, we load production configuration and secrets
# from environment variables. You can also hardcode secrets,
# although such is generally not recommended and you have to
# remember to add this file to your .gitignore.
use Mix.Config

database_url =
  System.get_env("DATABASE_URL") ||
    raise """
    environment variable DATABASE_URL is missing.
    For example: ecto://USER:PASS@HOST/DATABASE
    """

config :eldeploy, Eldeploy.Repo,
  # ssl: true,
  url: database_url,
  pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")

secret_key_base =
  System.get_env("SECRET_KEY_BASE") ||
    raise """
    environment variable SECRET_KEY_BASE is missing.
    You can generate one by calling: mix phx.gen.secret
    """

config :eldeploy, EldeployWeb.Endpoint,
  http: [
    port: String.to_integer(System.get_env("PORT") || "4000"),
    transport_options: [socket_opts: [:inet6]]
  ],
  secret_key_base: secret_key_base

# ## Using releases (Elixir v1.9+)
#
# If you are doing OTP releases, you need to instruct Phoenix
# to start each relevant endpoint:
#
#     config :eldeploy, EldeployWeb.Endpoint, server: true
#
# Then you can assemble a release by calling `mix release`.
# See `mix help release` for more information.

But do I need to ignore this file?

I don’t use prod.secret.exs at all anymore.

Read up on Application configuration. config/releases.exs is what I use for all my projects.

Which is only available for mix-releases, not distillery releases and the former is only available for elixir ~> 1.9.

Though one could use distillery features to read from the environment at runtime instead of trying to read them at beuild time.

Config providers, REPLACE_OS_VARS (deprecated) and possibly others.

You were saying that the image I’m using is very old. So how do I check for my configuration like I’m using elixir 1.10.3

HERE

Erlang/OTP 23 [erts-11.0.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe]

IEx 1.10.3 (compiled with Erlang/OTP 21)

Also, I don’t know why it is prod.secret.exs is being used here, and can I compile my file without it. I might have to see that.

This is strange, because your docker base image is bitwalker/alpine-elixir:1.7, which means you will have the ~> 1.7.X version of Elixir.

Where is the output that you wrote from? Your local machine? Inside Docker container? Maybe for development you are using a different base image?

Sorry I was using this as a reference.

Yes it’s my local machine. Not docker

Ok, so locally you have a different version, than in Docker. For this reason I also have my whole development environment in Docker as well. But that’s another topic :slight_smile:

So, do you still need help with anything else? Or we got to the bottom of this? :slight_smile:

Yeah, let me try this once more if it works it would be great. Otherwise, I’ll ask you again. I got to read some of the things before I ask any more questions.