Kaquadu1

Kaquadu1

Hi guys! I decided to update OTP/Elixir version of my project. I have to switch from Alpine to Debian, because dart_sass has some problems with Alpine and the package I used for solving it is not working anymore. I managed to do the update almost smoothly however I really cannot make the application connect to the database. I’m getting an nxdomain error. I must miss something and I really can’t spot it. I would really appreciate if someone could take a look and help me finding where the mistake is.

[error] Postgrex.Protocol (#PID<0.3695.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :nxdomain

Docker compose

version: "3.8"

networks:
  default:
    driver: host

services:
  postgres:
    image: postgres:15
    ports:
      - '5432:5432'
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    expose:
      - '5432'
    restart: always
    networks:
      - default

  elixir:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - postgres
    environment:
      MIX_ENV: ${MIX_ENV}
      DATABASE_URL: ${DATABASE_URL}
      SECRET_KEY_BASE: ${SECRET_KEY_BASE}
      AWS_S3_ACCESS_KEY_ID: ${AWS_S3_ACCESS_KEY_ID}
      AWS_S3_SECRET_ACCESS_KEY: ${AWS_S3_SECRET_ACCESS_KEY}
      SENDGRID_API_KEY: ${SENDGRID_API_KEY}
      PORT: ${PORT}
    ports:
      - '4000:4000'
    expose:
      - '4000'
    networks:
      - default

env file

AUTOMATIC_LOGOUT_TIME="99999999999"
CLOSING_TABS_LOGOUT_TIME="99999"
AWS_S3_ACCESS_KEY_ID="some_key"
AWS_S3_SECRET_ACCESS_KEY="some_key"
AWS_S3_BUCKET="some_bucket"
MIX_ENV="dev"
FILE_UPLOAD_ENV="dev"
NODE_ENV="production"
HOST="localhost:4000"
SENDGRID_API_KEY="development_sengrid_api_key"
PORT="4000"
SECRET_KEY_BASE="some_key"
DATABASE_URL="postgres://postgres:postgres@postgres:5432/sample_dev"
POSTGRES_PASSWORD="postgres"
POSTGRES_USER="postgres"
POSTGRES_DB="sample_dev"
DATABASE_SSL="FALSE"

Dockerfile

ARG ELIXIR_VERSION=1.15.2
ARG ERLANG_VERSION=26.0.2
ARG DEBIAN_VERSION=20230612
ARG LINUX_VERSION=debian-bullseye-${DEBIAN_VERSION}-slim

#########################
# Stage: deps-assets    #
#########################
FROM hexpm/elixir:${ELIXIR_VERSION}-erlang-${ERLANG_VERSION}-${LINUX_VERSION} as deps-assets

RUN apt-get autoremove -y
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/*

RUN apt update
RUN apt-get install -f
RUN apt-get install ca-certificates --assume-yes
RUN apt-get install nodejs --assume-yes
RUN apt-get install npm --assume-yes
RUN apt-get install git --assume-yes
RUN apt-get install gcc  --assume-yes
RUN apt-get install build-essential --assume-yes

ENV MIX_ENV=prod \
    MIX_HOME=/opt/mix \
    HEX_HOME=/opt/hex

WORKDIR /app

COPY . .

RUN mix local.rebar --force \
 && mix local.hex --if-missing --force \
 && mix do deps.get --only $MIX_ENV

WORKDIR /app/apps/sample_web/assets

RUN npm install

#########################
# Stage: release        #
#########################

FROM hexpm/elixir:${ELIXIR_VERSION}-erlang-${ERLANG_VERSION}-${LINUX_VERSION} as release

RUN apt-get autoremove -y
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/*

RUN apt update
RUN apt-get install -f
RUN apt-get install git --assume-yes
RUN apt-get install gcc  --assume-yes
RUN apt-get install build-essential --assume-yes

ENV MIX_ENV=prod \
    MIX_HOME=/opt/mix \
    HEX_HOME=/opt/hex

WORKDIR /app

COPY . .

COPY --from=deps-assets /app/apps/sample_web/assets/ /app/apps/sample_web/assets/
COPY --from=deps-assets /app/apps/sample_web/priv/static/ /app/apps/sample_web/priv/static/
COPY --from=deps-assets /app/deps/ /app/deps/
COPY --from=deps-assets /app/apps/sample_web/assets/static/images/ /app/apps/sample_web/priv/static/images/
COPY --from=deps-assets /app/apps/sample_web/assets/fonts/ /app/apps/sample_web/priv/static/fonts

RUN mix local.rebar --force \
 && mix local.hex --if-missing --force \
 && mix do deps.get --only $MIX_ENV, deps.compile, assets.deploy

RUN mix release \
 && rm -rf /app/deps

#########################
# Stage: production     #
#########################
FROM debian:bullseye-${DEBIAN_VERSION}-slim as production

RUN apt-get autoremove -y
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/*

RUN apt update
RUN apt-get install -f
RUN apt-get install openssl
RUN apt-get install gcc --assume-yes
RUN apt-get install build-essential --assume-yes

COPY --from=release /app/_build/prod/rel/sample_umbrella ./

RUN chmod +x /bin/*

CMD ["bin/sample_umbrella", "start"]

EXPOSE 4000

Runtime config:

import Config

defmodule Config.Helper do
  def get_env(env, default) when is_number(default) do
    value = System.get_env(env)

    if value do
      String.to_integer(value)
    else
      default
    end
  end

  def get_env(env, default) do
    value = System.get_env(env) || default
    "#{value}"
  end
end

if System.get_env("MIX_ENV") in ["dev", "prod", nil] do
  config :sample, Sample.Repo,
    ssl: System.get_env("DATABASE_SSL") == "TRUE",
    url: System.get_env("DATABASE_URL"),
    socket_options: [:inet6],
    pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
    stacktrace: System.get_env("MIX_ENV") == "dev",
    timeout: 30_000,
    show_sensitive_data_on_connection_error: System.get_env("MIX_ENV") == "dev"
end

if System.get_env("MIX_ENV") == "test" do
  # Configure your database
  config :sample, Sample.Repo,
    username: "postgres",
    password: "postgres",
    database: "sample_test",
    hostname: "localhost",
    pool: Ecto.Adapters.SQL.Sandbox
end

if System.get_env("MIX_ENV") in ["dev", nil] do
  config :sample_web, SampleWeb.Endpoint,
    check_origin: ["//#{System.get_env("HOST") || "//our_application_address"}"],
    http: [:inet6, port: System.get_env("PORT")],
    secret_key_base: System.get_env("SECRET_KEY_BASE"),
    server: true,
    root: ".",
    live_view: [signing_salt: System.get_env("LIVE_VIEW_SALT") || "Nbe6JYiR"],
    watchers: [
      sass: {
        DartSass,
        :install_and_run,
        [:default, ~w(--embed-source-map --source-map-urls=absolute --watch --quiet)]
      },
      admin_sass: {
        DartSass,
        :install_and_run,
        [:admin, ~w(--embed-source-map --source-map-urls=absolute --watch --quiet)]
      },
      esbuild: {Esbuild, :install_and_run, [:default, ~w(--sourcemap=inline --watch)]}
    ]
end

if System.get_env("MIX_ENV") == "prod" do
  config :sample_web, SampleWeb.Endpoint,
    check_origin: ["//#{System.get_env("HOST") || "our_application_address"}"],
    http: [:inet6, port: System.get_env("PORT")],
    url: [scheme: "https", host: System.get_env("HOST"), port: 443],
    force_ssl: [rewrite_on: [:x_forwarded_proto], hsts: true],
    cache_static_manifest: "priv/static/cache_manifest.json",
    secret_key_base: System.get_env("SECRET_KEY_BASE"),
    server: true,
    root: ".",
    live_view: [signing_salt: System.get_env("LIVE_VIEW_SALT") || "Nbe6JYiR"]
end

config :ex_aws,
  access_key_id: System.fetch_env!("AWS_S3_ACCESS_KEY_ID"),
  secret_access_key: System.fetch_env!("AWS_S3_SECRET_ACCESS_KEY")

config :sample_web, SampleWeb.Guardian,
  secret_key:
    System.get_env("GUARDIAN_SECRET_KEY") ||
      "sample_key"

config :sample, Sample.Mailer, api_key: System.fetch_env!("SENDGRID_API_KEY")

I use just plain

docker compose build

and

docker compose up

for launching it. The database service is up and running first, with a prompt: “ready to accept connections”.

Showing Posts 1 to 9

hubertlepicki

hubertlepicki

I don’t quite remember how to configure netowkring in docker/ docker compose to make this particular thing work, but I suspect the issue is that the host ‘postgres’ is not being resolved from the container. Probably the easiest fix is to replace that line with

DATABASE_URL="postgres://postgres:postgres@localhost:5432/sample_dev"
Kaquadu1

Kaquadu1 OP

Thanks for the suggestion, I tried already:
postgresql://postgres:postgres@postgres:5432/sample_dev

[error] Postgrex.Protocol (#PID<0.3689.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :nxdomain

postgres://postgres:postgres@postgres:5432/sample_dev

[error] Postgrex.Protocol (#PID<0.3686.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :nxdomain

postgres://postgres:postgres@localhost:5432/sample_dev

[error] Postgrex.Protocol (#PID<0.3691.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): can't assign requested address - :eaddrnotavail

postgresql://postgres:postgres@localhost:5432/sample_dev

[error] Postgrex.Protocol (#PID<0.3694.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): can't assign requested address - :eaddrnotavail
regex.sh

regex.sh

I think it should be like, but that’s for releases (Deploying with Releases — Phoenix v1.8.8)

export DATABASE_URL=ecto://USER:PASS@HOST/database
Kaquadu1

Kaquadu1 OP

A friend of mine pointed out that I had bridge network in the compose file - I changed it to host, so I can refer to Postgres by localhost:5432, but it is still not working. For both postgresql and ecto at the beginning of the database string I get the same error :frowning:


networks:
  default:
    driver: host

[error] Postgrex.Protocol (#PID<0.3697.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): can't assign requested address - :eaddrnotavail
regex.sh

regex.sh

This is more like docker compose issue than Elixir/Phoenix one :slight_smile:
The error says that address assigning is bad (try restarting it) :smiley:

dimitarvp

dimitarvp

Yeah, you gotta look up Docker Compose docs on how to make sure all host names are assigned and visible by all nodes described in the config file.

Kaquadu1

Kaquadu1 OP

Well, for some reason switching from -slim version of debian to the normal one and using default bridge network helped :thinking: Also I had to manually setup password for the database via CLI with ALTER ROLE postgres WITH PASSWORD 'postgres' because it weren’t set through the env/compose.

So my compose file looks like that right now:

version: "3.8"

services:
  postgres:
    image: postgres:15
    ports:
      - '5432:5432'
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    expose:
      - '5432'
    restart: always

  elixir:
    build:
      context: .
      dockerfile: Dockerfile
    links:
      - "postgres:postgres"
    depends_on:
      - postgres
    environment:
      MIX_ENV: ${MIX_ENV}
      DATABASE_URL: ${DATABASE_URL}
      SECRET_KEY_BASE: ${SECRET_KEY_BASE}
      AWS_S3_ACCESS_KEY_ID: ${AWS_S3_ACCESS_KEY_ID}
      AWS_S3_SECRET_ACCESS_KEY: ${AWS_S3_SECRET_ACCESS_KEY}
      SENDGRID_API_KEY: ${SENDGRID_API_KEY}
      PORT: ${PORT}
      HOST: ${HOST}
    ports:
      - '4000:4000'
    expose:
      - '4000'

And in the Dokcerfile I’ve got:

ARG DEBIAN_VERSION=20230612
ARG LINUX_VERSION=debian-bullseye-${DEBIAN_VERSION}

...

FROM debian:bullseye-${DEBIAN_VERSION} as production

And the database url is:

postgresql://postgres:postgres@postgres:5432/sample_dev

The only thing that’s left right now is I’m missing static assets (images etc) while running the application :sweat_smile:

com

com

Hi!

Is your Phoenix Docker static file issue resolved?

I’m wondering if you’re running into a Linux sendfile syscall issue…

Regards

Peter

Kaquadu1

Kaquadu1 OP

Sorry for the late response - it has been solved, but I really don’t remember how right now :+1:

— All posts loaded —

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
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

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
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews