smallfoot
smallfoot
This is a project written using Lucky. Enjoy!
The goal is to build smallest Docker image needed to run the app.
Notes
- The dotenv (.env) file is checked into the repo (normally it wouldn't be!)
- The docker-compose.yml is configured to run migrations and then the app
- To find IP address of the spun db image:
docker network inspect smallfoot_internal
after firstdocker-compose up
attempt. - It was suggested that I have three images, db, migrator, and app. However, with this approach, the first time images are launched, the migrator and app will come up nearly simultaneously and the app with crash out because migrations have not run, yet. Although subsequent runs will work, it's not good practice anyway to run migrations while the app, that has new code depending on new tables or fields is also running, so this approach is not desired even if first run could be solved.
Evolution
- Started with Dockerfile from https://github.com/luckyframework/website-v2/blob/master/Dockerfile
- Turned into multi-stage build
- Began minimizing following https://manas.tech/blog/2017/04/03/shipping-crystal-apps-in-a-small-docker-image.html
- Used https://gist.github.com/bcardiff/85ae47e66ff0df35a78697508fcb49af#gistcomment-2078660 instead of the list-deps.cr script
- Removed the lucky_cli from system and built it instead from the installed shards of the library
Setting up the project
Gotchas
If attempting to use lucky db.migrate
:
- Realized this must be re-compiling on the fly as it needed various dependencies to build.
- Came up with alternative approach to add tasks.cr as a shard build target.
- This morphs the command to
/app/bin/tasks db.migrate
(see below for more) lucky db.migrate
is a no-go because it will look for and fail to includeprelude
(which I verified does exist from shards install) at which point specific error message suggests I forgot to runshards install
-- very likely I missed something obvious when copying files from prior layer's build image.
Realized I could compile and run tasks.cr
and thus tasks db.migrate
- following the trail of how
lucky db.migrate
works, realized ./tasks.cr could be compiled and executed and skip the lucky cli middleman. - Added new target to shards.yml for this.
- Now have executable that runs via
/app/bin/tasks db.migrate
but will not connect to database when it's host name isdb
. It does connect when hostname is IP address of the host. - however, cannot chain the migration together with running the app. Using
/app/bin/tasks db.migrate && /app/bin/smallfoot
I get the following:Attaching to smallfoot_app app_1 | ******************************************************************************** app_1 | production postgres://lucky:developer@172.30.0.2:5432/smallfoot_production app_1 | ******************************************************************************** app_1 | Migrated CreateUsers::V00000000000001 smallfoot_app exited with code 0
Build one off app that migrates the db and exits, i.e. /bin/app/db.migrate
- Attempted to build a one-purpose app that simply connected and ran the migration and exited.
- Same issues encountered as with
tasks db.migrate
- In this case, command was
/app/bin/db.migrate && /app/bin/smallfoot
Failing to get tasks db.migrate
, turned attention to creating a migration specific executable.
- see
src/db.migrate
- contains monkey patched library class methods so I could emit connect strings and verify
- idea was to set docker-compose command to
/app/bin/db.migrate && /app/bin/smallfoot
- exact same issues as above with
tasks db.migrate
Run the migration within the app itself!
-
Final effort, lines 20 ~ 24 in
config/database.cr
would simply invoke the migrations before the app started up (currently commented out). -
Docker command reduces to just
/app/bin/smallfoot
-
If
db
for DB_HOST is used, migrations won't connect and run. Even if spawned, with sleep delay before attempting. -
If IP address used for DB_HOST, it works, but again, app exits when the migration finishes.
-
Run
script/setup
-
Run
lucky dev
to start the app
Learning Lucky
Lucky uses the Crystal programming language. You can learn about Lucky from the Lucky Guides.
Repository
smallfoot
Owner
Statistic
- 4
- 1
- 18
- 0
- 6
- almost 2 years ago
- July 8, 2019
License
Links
Synced at
Sun, 17 Nov 2024 11:07:00 GMT
Languages