Problem
I’m making a side project that needed to ship custom code under machines with the same Dockerfile.
My solution
I choose to do this through environment variables!
This example will be in elixir but would work with pretty much any programming language!
tl;dr: we make a run.sh
that elixir TMP_file
1. The entrypoint
Create a run.sh
file like this:
#!/bin/sh
TMPFILE=$(mktemp)
echo "$CODE" > $TMPFILE
elixir $TMPFILE
Then make sure to make it executable with chmod +x run.sh
.
2. The Dockerfile
Your Dockerfile should install your programming language and at the end ADD run.sh /path/to/somewhere
and end with CMD /path/to/somewhere/run.sh
. Here’s an actual example: flyio-run-code-from-env-variable/Dockerfile at 2c2b4c7b7ac7aab8b0bff95f405f3161660c05a5 · lubien/flyio-run-code-from-env-variable · GitHub
3. Preparing a fly.io app
Create your app using fly launch --no-deploy
, tweak the internal_port
to 4000 or whatever you want for your app. My example uses 4000.
Assuming your file is named bug.ex
you can set the env variable as CODE=
cat bug.exs fly secrets set CODE="$CODE" --stage
. This will not trigger a deploy yet.
4. Deploy!
Run fly deploy --ha=false
(disabling HA to make only one machine).
Your CLI will say that the deployment didn’t work if it takes forever to compile (like my Elixir example) so don’t worry. Use fly logs
to see when it finishes compiling and starts the server.
Go to your-app-name.fly.dev
Tips
If the above guide is confusing, it’s fine look at the first posts that I’ve wrote here. This was originally a post where I added detailed notes about me experimenting this so there’s more tips below!
Original post:
Title: [Dev log] Trying to deploy a machine with the code on an env variable!
I got interested in one thing: can I deploy an elixir machine with the code being inside an environment variable?
I’m not sure this will work but I’m really interested in it so I’m going to try and I will post updates here in soft real time.
Edit: it works, by this message it was working already: How to run code from an env variable - #14 by lubien