afnan

afnan

Requesting help for slow Docker application

Hi Folks,
I have started working on a project that was not built using docker. Its pheonix framework with liveview implementation. The problem is that in docker there are two problems,

  1. Auto reload does not work despite all settings
  2. Page reloads are extremely slow. It takes 10-20 seconds for page refresh. In the terminal i see after page refresh nothing happens but close to 8-9 seconds db requets for data get fired and then page reloads.

I have spent 2 weeks on this trying different online solutions but no joy.

Following are my docker files.

# Use the latest Elixir image
FROM elixir:latest

# Accept OBAN_AUTH_KEY as a build argument
ARG OBAN_AUTH_KEY
ARG OBAN_PUBLIC_KEY

# Set default to dev environment
ARG MIX_ENV=dev
ENV MIX_ENV=${MIX_ENV}

RUN echo "MIX ENV"
RUN echo $MIX_ENV

RUN apt-get update && \
  apt-get install -y postgresql-client && \
  apt-get install -y inotify-tools && \
  apt-get install -y git nodejs npm curl && \
    curl -L https://npmjs.org/install.sh | sh
  
# Install Hex and Rebar
RUN mix local.hex --force && \
    mix local.rebar --force && \
    mix hex.repo add oban https://getoban.pro/repo \
    --fetch-public-key $OBAN_PUBLIC_KEY \
    --auth-key $OBAN_AUTH_KEY

WORKDIR /app

# Copy mix and hex files
COPY mix.exs mix.lock ./
# COPY config config
# Install dependencies
RUN mix deps.get

# Install Node dependencies
COPY assets/package.json assets/
RUN if [ -f assets/package.json ]; then cd assets && npm install && cd ..; fi

COPY . .
# Copy the rest of your app's source code


# Compile the app
# RUN mix do compile


# CMD ["mix", "phx.server"]
# CMD ["/bin/entrypoint.sh"]

entry script

#!/bin/bash

echo "Postgres Host: $POSTGRES_HOST"
echo "Postgres Port: $POSTGRES_PORT"
echo "Postgres User: $POSTGRES_USER"

echo "GOOGLE_APPLICATION_CREDENTIALS is set to: $GOOGLE_APPLICATION_CREDENTIALS"

# Set the password for PostgreSQL commands
export PGPASSWORD=$POSTGRES_PASSWORD

# Docker entrypoint script.

# Wait until Postgres is ready
while ! pg_isready -q -h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER
do
  echo "$(date) - waiting for database to start"
  sleep 2
done


# Check if the database exists
DB_EXISTS=$(psql -U $POSTGRES_USER -h $POSTGRES_HOST -p $POSTGRES_PORT -lqt | cut -d \| -f 1 | grep -w $POSTGRES_DB | wc -l)

# If the database exists, run migrations. Otherwise, run setup.
if [ "$DB_EXISTS" -eq "0" ]; then
  echo "Database does not exist. Running mix ecto.setup..."
  mix ecto.setup
else
  echo "Database exists. Running migrations..."
  mix ecto.migrate
fi
# echo "Running Seeds"
# mix ecto.seed

echo "Starting Phoenix server..."
exec iex -S mix phx.server

Docker compose

networks: 
  backend:
 
services:
  hndl_platform:
    container_name: hndl__platform
    command: sh -c "./bin/entrypoint.sh"
    build:
      context: .
      dockerfile: Dockerfile.dev
      args:
        OBAN_AUTH_KEY: ${OBAN_AUTH_KEY}
        OBAN_PUBLIC_KEY: ${OBAN_PUBLIC_KEY}
        MIX_ENV: ${MIX_ENV}
    ports:
      - "4000:4000"    
    env_file:
      - .env # Path to your environment file
    environment:
      GOOGLE_APPLICATION_CREDENTIALS: /app/google-credentials.json
    depends_on:
      - hndl_db
    volumes:
      - .:/app
      - ./google-credentials.json:/app/google-credentials.json
    networks:
      backend:

  hndl_db:
    container_name: hndl__db
    image: postgis/postgis
    restart: always
    ports:
      - 5432:5432
    env_file:
      - .env # Path to your environment file    
    volumes:
      - postgres_data:/var/lib/postgresql/data

    networks:
      backend:
  pgadmin:
    # connect using host: host.docker.internal
    container_name: hndl__pgadmin
    image: dpage/pgadmin4:latest
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: hndl@test.com
      PGADMIN_DEFAULT_PASSWORD: hndl
    ports:
      - "5050:80"

volumes:
  postgres_data:

dev file

import Config

# Configure your database
config :hndl, Hndl.Repo,
  username: System.get_env("POSTGRES_USER") || "postgres",
  password: System.get_env("POSTGRES_PASSWORD") || "postgres",
  hostname: System.get_env("POSTGRES_HOST") || "localhost",
  database: System.get_env("HNDL_DEV_DATABASE") || System.get_env("POSTGRES_DB") || "hndl_dev",
  stacktrace: true,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10,
  types: Hndl.PostgresTypes

# For development, we disable any cache and enable
# debugging and code reloading.
#
# The watchers configuration can be used to run external
# watchers to your application. For example, we use it
# with esbuild to bundle .js and .css sources.
config :hndl, HndlWeb.Endpoint,
  # Binding to loopback ipv4 address prevents access from other machines.
  # Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
  http: [ip: {0, 0, 0, 0}, port: 4000],
  check_origin: false,
  code_reloader: true,
  debug_errors: true,
  secret_key_base: "something",
  watchers: [
    esbuild: {Esbuild, :install_and_run, [:default, ~w(--sourcemap=inline --watch)]},
    tailwind: {Tailwind, :install_and_run, [:default, ~w(--watch)]}
  ]

# ## SSL Support
#
# In order to use HTTPS in development, a self-signed
# certificate can be generated by running the following
# Mix task:
#
#     mix phx.gen.cert
#
# Run `mix help phx.gen.cert` for more information.
#
# The `http:` config above can be replaced with:
#
#     https: [
#       port: 4001,
#       cipher_suite: :strong,
#       keyfile: "priv/cert/selfsigned_key.pem",
#       certfile: "priv/cert/selfsigned.pem"
#     ],
#
# If desired, both `http:` and `https:` keys can be
# configured to run both http and https servers on
# different ports.

# Watch static and templates for browser reloading.
config :hndl, HndlWeb.Endpoint,
  live_reload: [
    patterns: [
      ~r"priv/static/(?!uploads).*(js|css|png|jpeg|jpg|gif|svg)$",
      ~r"priv/gettext/.*(po)$",
      ~r"lib/hndl_web/live/.*(ex|heex|leex)$",
      ~r"lib/hndl_web/components/.*(ex|heex|leex)$",
      ~r"lib/hndl_web/views/.*(ex)$",
      ~r"lib/hndl_web/controllers/.*(ex)$",
      ~r"lib/hndl_web/hooks/.*(ex)$"
    ],
    backend: [
      Phoenix.LiveReloader.PollingBackend,
      # Polling interval in milliseconds
      interval: 1000
    ]
  ]

# Enable dev routes for dashboard and mailbox
config :hndl, dev_routes: true

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"

# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20

# Initialize plugs at runtime for faster development compilation
config :phoenix, :plug_init_mode, :runtime

# Disable swoosh api client as it is only required for production adapters.
config :swoosh, :api_client, false

config :hndl, YOJEE_TEMPLATE_TYPE_ID: 1580
config :hndl, YOJEE_SENDER_ID: "4099"

config :stripity_stripe,
  signing_secret: "something"

config :hndl, :addressr_client, base_url: "http://localhost:8080"

config :hndl, :yojee,
  access_token: "something",
  base_url: "https://umbrella-staging.com",
  company_slug: "handelgroup-test"

config :hndl, env: :dev

config :appsignal, :config, active: false


Update

code_reloader: false, solves the speed issue. BUt still no auto reload

First Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @afnan can you provide a bit more info about your setup? What OS are you running on? Which docker utility are you using?

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
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
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement