ostap

ostap

Can't connect to an SQLite database with Dokku

Trying to put my app out in the wild. Received a Dockerfile from running

mix phx.gen.release --docker

Then on Dokku’s host I mounted a persistent directory and set the DATABASE_PATH env:

dokku config:set --no-restart myapp DATABASE_PATH=/app/storage/db.sqlite3 PHX_HOST=myapp.com SECRET_KEY_BASE=#beforehand do mix phx.gen.secret
dokku storage:ensure-directory myapp
dokku storage:mount myapp /var/lib/dokku/data/storage/myapp:/app/storage

When deploying, I was getting a “database does not exist” error. Therefore made it manually:

sqlite3 /var/lib/dokku/data/storage/myapp/db.sqlite3 "
  PRAGMA journal_mode = WAL;
  PRAGMA auto_vacuum = INCREMENTAL;
  PRAGMA page_size = 4096;
  PRAGMA wal_autocheckpoint = 1000;
  VACUUM;"
# P.S I executed this as root, for better or worse...

However, on another deployment attempt the app still couldn’t connect to this database or run migrations. This time, the error is different:

** (RuntimeError) connect raised MatchError exception. The exception details are hidden, as they may contain sensitive data such as database credentials. You may set :show_sensitive_data_on_connection_error to true when starting your connection if you wish to see all of the details
    (exqlite 0.33.1) lib/exqlite/connection.ex:396: Exqlite.Connection.get_pragma/2
    (exqlite 0.33.1) lib/exqlite/connection.ex:405: Exqlite.Connection.maybe_set_pragma/3
    (exqlite 0.33.1) lib/exqlite/connection.ex:551: Exqlite.Connection.do_connect/2
    (db_connection 2.8.1) lib/db_connection/connection.ex:79: DBConnection.Connection.handle_event/4

Any ideas on why this could be happening?

Marked As Solved

rhcarvalho

rhcarvalho

A safer option is to use

dokku storage:ensure-directory --chown nobody:nobody myapp

(Assuming USER nobody from phoenix/priv/templates/phx.gen.release/Dockerfile.eex at v1.8.3 · phoenixframework/phoenix · GitHub)

Also Liked

rhcarvalho

rhcarvalho

It’s because the user inside the container is not root (good practice). I imagine you have nothing important there yet, so my suggestion is that you delete the file (as root) and recreate from inside the container (as the correct user).

E.g. with mix ecto.setup

(Typing on mobile off the top of my head, there can be mistakes)

rhcarvalho

rhcarvalho

Oh, you don’t want to create a database during build. The database itself is not to be part of the Docker image.

It will be created/migrated at run time, when replacing the current deploy with the new (after you get the app out to the world…)

rhcarvalho

rhcarvalho

Correcting a small mistake in my previous post – there’s no --chown uid:gid, but instead use --chown false and then manually chown the directory as needed:

APP_NAME=myapp
DIRECTORY_BASENAME=myapp
TARGET_PATH=/mnt/data
CONTAINER_USER_ID=4004
CONTAINER_GROUP_ID=4004
DOKKU_STORAGE_PATH=/var/lib/dokku/data/storage/${DIRECTORY_BASENAME}

dokku storage:ensure-directory --chown false ${DIRECTORY_BASENAME}
dokku storage:mount ${APP_NAME} ${DOKKU_STORAGE_PATH}:${TARGET_PATH}
sudo chown -R ${CONTAINER_USER_ID}:${CONTAINER_GROUP_ID} ${DOKKU_STORAGE_PATH}

Where Next?

Popular in Questions Top

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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement