This guide discusses different ways to run multiple processes in your app. To learn about process groups in Fly Apps and the
[processes] configuration in fly.toml, see Run multiple process groups in an app. For process group configuration with the Machines API, see the config.processes object in the Machine config.init kills the Machine and we start a new one. So ultimately, something has to keep running “in the foreground”.
Setting the scene
Let’s use the world’s dumbest Golang app as our test case. Here’s a Dockerfile:-bar. Use your imagination!
And we’re not running anything right now; it’s just going to sit there inert while we figure out how to run both instances of the program.
With a working Dockerfile, we can spin this up with fly launch. We’ll edit fly.toml to tell Fly.io about the two internal ports we’re listening on, and fly deploy to effect that change.
Now, some options to actually run this stuff:
Process groups
Process groups are the recommended way to run multiple processes. This method runs each process in its own Machine or group of Machines. You can define multiple process groups in yourfly.toml, giving each a name. Each defined process runs in its own Machine or group of Machines within the one app.
You can see that in action in the below (truncated) fly.toml file:
web and bar_web. The command defined for each process group is setting the command passed to your Dockerfile entrypoint. That means your entrypoint needs to be able to handle a command being passed to it!
Here’s an example of such an entrypoint:
[[services]] section of the fly.toml file, we define a service for process web. The processes = [...] array acts as a filter - it will apply only to the processes listed.
You can also scale Machines horizontally and vertically by process group. See the process groups docs for details.
Just use Bash
This is gross, but a suggestion Docker makes in its own documentation, so it must be OK. We boot our Docker container into a shell script. That shell script is:gross.sh and change the Dockerfile entrypoint:
fly.toml:
Use Supervisord
The other Docker documentation suggestion: usesupervisor. supervisor is an actual process manager; it’ll run in the foreground and manage all our processes and, most importantly, when our processes exit, supervisor will (configurably) restart it.
supervisor has a lot of configuration options. But because we’re running on Fly.io, we mostly don’t care about them; the platform is doing a lot of this work for us. So the supervisor configuration we want is pretty simple:
/var/log, so we just configure supervisor to log to stdout like a normal person. The rest, apart from our command lines, is just boilerplate.
(You could drop privileges by changing the user line if you wanted; whether you want to depends on whether there’s meaningful security to get by running multi-user in your particular container. If basically everything in the container shares the same data, there probably isn’t.)
Our Dockerfile derives from the golang base image, and to add supervisor to it, we just need to add the dep:
Use a Procfile Manager
supervisor works, and lots of people use it. It’s a bit fussy, though, and it pulls Python in as a dependency. There are lots of other process managers. One useful genus of them are the Procfile managers. Procfiles are so dirt simple you don’t need me to explain them; just look:
overmind is a Go program, so it’s got a small runtime, and you could, if you were fussy, build a container that just takes the overmind binary and none of its build deps. We won’t bother, though, since we’re already bringing those deps in. So:
overmind has a bunch of interesting features, most notable among them that it wraps tmux, so you can get a terminal window on any of your running programs. Deploy, and then you can:
tmux person, ctr-b d detaches you from that window.
There are so many other process managers
Use whichever you like! The only limitation you’re going to run into is that Fly.io owns yourinit (sorry!); we have to be PID 1. There are a bunch of process managers that want to replace init, and those’ll be tricky to get to work in a Fly.io app.
Maybe you want multiple apps, though
There are good reasons to run multiple programs in a single container, but sometimes when you’re doing that you’re actually working against the platform (which is why Docker keeps telling you not to do that). If you’re running multiple heavy-weight things in a single container, you might explore just running them as separate Fly.io apps. The advantage to doing this, apart from the apps not fighting over the same CPUs and memory, is that you can scale separate apps independently, and put them in different sets of regions. Fly.io apps can talk to each other over a private network that’s always available. They can find each other under the.internal top-level domain (if your apps are foo and bar, they’ll be in the DNS as foo.internal and bar.internal). Because the network connection is private and encrypted, you can generally just talk back and forth without extra authentication until you know you need it; in other words, you can keep things simple.
The main conveniences you give up when splitting into separate apps are that secrets are shared across all process groups in a single app, and all process groups deploy from the same Docker image. There are also cases where separate apps are the only option. For instance, the metrics-based autoscaler operates on a whole app, so workloads that need independent scaling behavior have to live in their own apps.
You can still share a single built image across several apps. Build it once without deploying:
registry.fly.io/app:deployment-xxxx. Any app in the same organization can then deploy from that image:
Maybe you don’t need multiple processes
There are a bunch of reasons people want to run multiple things in a container that Fly.io already takes care of for you. For instance: metrics are a built-in feature of the platform, as are logs. You might not need to run helper processes for this kind of stuff. Another thing people run multiple programs to get is static asset HTTP service; you’ll run your app server, and a small webserver alongside it to serve assets. You don’t need to do this if you don’t want! Instead, you can ask the Fly.io platform to do it for you, by addingstatics directives to your fly.toml:
fly.toml, it’ll fish your static assets out of your container and serve them directly from our proxies at the url_prefix you specify. This is faster and, of course, doesn’t demand you package an HTTP server into your container.