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

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
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

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement