wdiechmann

wdiechmann

MyXQL says failed to connect ... non-existing domain - but there is no example.com :o

I’m trying to cram my phoenix project into a couple of containers. I have a mysql-server running in a container that exposes 3306 (and which I’m able to connect to from CLI). I’ve built a container for my app with this

# BUILD STAGE
FROM bitwalker/alpine-elixir-phoenix:latest AS phx-builder

# Set exposed ports
ENV MIX_ENV=prod

RUN mkdir /app
WORKDIR /app

# Cache elixir deps
ADD mix.exs mix.lock ./
RUN mix do deps.get, deps.compile

# Same with npm deps
ADD assets/package.json assets/
RUN cd assets && \
  npm install

ADD . .

# Run frontend build, compile, and digest assets
RUN cd assets/ && \
  npm run deploy && \
  cd - && \
  mix do deps.get, deps.compile, phx.digest, release

# APPLICATION STAGE
FROM bitwalker/alpine-elixir:latest

COPY --from=phx-builder /app/_build .

RUN chmod +x ./prod/rel/fish/bin/fish
RUN mkdir ./prod/rel/fish/tmp
RUN chmod +x ./prod/rel/fish/bin/fish

EXPOSE 5000
ENV MIX_ENV=prod 
ENV APP_PORT=4000 
ENV COOL_TEXT="Elixir Rocks" 
ENV SECRET_KEY_BASE="Aru1gpxiZDZHqt6biIsZoblCM8/yUPLy3kG9BhgcvcKUDUv5SkPhR4q/HKhZkcV7" 
ENV POOL_SIZE=10 

CMD ["./prod/rel/fish/bin/fish", "start"]

I can do

$ docker build -f Dockerfile.build -t fish .

but when I do

$ docker run --publish 4000:4000 --env COOL_TEXT='ELIXIR ROCKS!!!!' --env SECRET_KEY_BASE=fikumdick --env APP_PORT=4000 --env PORT_SIZE=10 --env HOST=localhost fish:latest

I get this output

15:31:11.834 [info] Running FishWeb.Endpoint with cowboy 2.7.0 at :::4000 (http)
15:31:11.835 [info] Access FishWeb.Endpoint at http://example.com
15:31:13.194 [error] MyXQL.Connection (#PID<0.3290.0>) failed to connect: ** (DBConnection.ConnectionError) non-existing domain

(and I haven’t the faintest idea where ‘it’ digs up that example.com - I have zero mentioning of that in any file)

My config/config.exs looks like this:

# config/config.exs
use Mix.Config

config :fish,
  ecto_repos: [Fish.Repo]

# Configures the endpoint
config :fish, FishWeb.Endpoint,
  url: [host: {:system, "HOST"}, port: {:system, "PORT"}],
  load_from_system_env: true,
  secret_key_base: "nOPS8kJTmAZtcgOpGVo7fMrAbFCCUVySKShVewMdaudersQgSLszNnTqb36fm71I",
  render_errors: [view: FishWeb.ErrorView, accepts: ~w(html json), layout: false],
  pubsub_server: Fish.PubSub,
  live_view: [signing_salt: "i1ucCSg6"]

config :fish, :pow,
  user: Fish.Users.User,
  repo: Fish.Repo,
  extensions: [PowResetPassword],
  controller_callbacks: Pow.Extension.Phoenix.ControllerCallbacks,
  web_module: FishWeb,
  mailer_backend: FishWeb.Pow.Mailer

config :fish, :pow_assent,
  providers: [
    github: [
      client_id: "REPLACE_WITH_CLIENT_ID",
      client_secret: "REPLACE_WITH_CLIENT_SECRET",
      strategy: Assent.Strategy.Github
    ]
    # example: [
    #   client_id: "REPLACE_WITH_CLIENT_ID",
    #   site: "https://server.example.com",
    #   authorization_params: [scope: "user:read user:write"],
    #   nonce: true,
    #   strategy: Assent.Strategy.OIDC
    # ]
  ]

# 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

and my releases.exs looks like this

#config/releases.exs 
import Config

secret_key_base = System.fetch_env!("SECRET_KEY_BASE")
cool_text = System.fetch_env!("COOL_TEXT")
application_port = System.fetch_env!("APP_PORT")
pool_size = System.fetch_env!("POOL_SIZE")

config :fish,
  cool_text: cool_text

config :fish, Fish.Repo,
  # ssl: true,
  migration_primary_key: [name: :id, type: :binary_id],
  username: "root",
  password: "",
  database: "fish_dev",
  hostname: "fish_db",
  # url: database_url,
  pool_size: String.to_integer(pool_size || "10")

config :fish, FishWeb.Endpoint,
  http: [:inet6, port: String.to_integer(application_port)],
  secret_key_base: secret_key_base

Marked As Solved

wdiechmann

wdiechmann

I erred on two accounts

  1. I referenced file_db (which is the container image name in my configuration) where I should have referenced the service name - because hosts are setup by default with hostname = service name - see the docker reference link below

  2. I quoted db in the docker-compose.yml file where I should have left the quotes out (in effect ‘promoting’ db to a local variable of sorts

# ./docker-compose.yml
version: "3"
services:
  db:
    image: mysql:latest
    container_name: fish_db
    environment:
      - MYSQL_DATABASE='fish_prod'
      - MYSQL_PASSWORD='another secret'
      - MYSQL_USER='root'
      - MYSQL_ROOT_PASSWORD='secret'
    ports:
      - "3306:3306"
    expose:
      - "3306"
    volumes:
      - ./priv/repo/db:/var/lib/mysql
    networks:
      - db-network
  server:
    image: fish:latest
    container_name: fish-app-server
    environment:
      - PORT=4000
      - HOST="127.0.0.1"
      - DBHOST=db
      - DBNAME=fish_prod
      - SECRET_KEY_BASE=fikumdick
    expose:
      - "4000"
    ports:
      - "4000:4000"
    command: ./prod/rel/fish/bin/fish start
    networks:
      - db-network
    depends_on:
      - db
volumes:
  fish_db: {}
networks:
  db-network:

Also Liked

wdiechmann

wdiechmann

yes

  • and I should have been on to that from the start!

Composing Docker compose yml’s will allow you to reference a somewhat covert naming scheme where hosts are named by their service - in this case: db

If you put that in “” I reckon that Docker will not try to extrapolate (or whatever it does) but take the string at face value -

So yes - I should have typed db where I typed "db" - and saved Wojtech and NoobZ precious time!

my bad

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement