
fly machine run command is a tool to configure, build, and start a new Machine in one line.
Many, but not all, Machine configuration options are available to the fly machine run command through flags. The available flags are listed in the flyctl help and on the fly machine run reference page.
Use fly machine run when you want use more than one Docker image in an app, or to run a one-off or temporary Machine.
To create a Machine, but not start it, use
fly machine create.To make changes to a Machine once it’s created or run, use fly machine update.To create and run a new Machine with the same configuration as an existing Machine, use fly machine clone.To add a new Machine to an app managed by fly deploy, see the scaling doc for Fly Launch apps. By default, fly machine run creates Machines that are ignored by Fly Launch features like fly deploy, fly status, and fly scale.What it does
Here’s whatfly machine run does for you:
- Checks with the platform for the org and app you’ve specified, if any, and if needed, guides you through naming a new app.
- Creates a Fly App, if applicable.
- Gets, or builds, a Docker image to make the Machine from, and pushes it to the Fly.io registry if applicable.
- Creates the Machine with some default config, plus config you pass to it with flags.
- Starts the Machine.
- By default, waits for the Machine to start, and for any configured health checks to pass, before declaring success (or failure).
Usage
Here’s the usage offly machine run:
<image> can point to a prebuilt image, or to the current directory (.) to build from a Dockerfile.
Administration: set the Machine’s app and org
The default behavior offly machine run is to create a new Fly App for the new Machine to belong to, unless it’s given the name of an existing app in one of two ways:
- Like many flyctl commands,
fly machine runwill pull an app name from afly.tomlfile if one is present in the working directory. It disregards the rest of the configuration in the file. - If you pass it an app name with
--app <app-name>, flyctl prefers that name over any name it gets from afly.toml.
fly.toml file with just the app name in it, to save using the --app option repeatedly. For example:
--org <org-name> to specify which organization a newly created app should belong to. The --org flag is ignored when creating the new Machine in an existing app.
Name the Machine
Machines have a human-friendlyname property, like ancient-glitter-2128, that shows up alongside the id in the output of the fly machine list command and in the web dashboard.
You can give the Machine a custom name with the --name flag:
Choose a region
Tell the Fly.io platform which region to create the Machine in with the--region flag; if for some reason it can’t start a new Machine in that region, you’ll get an error. If the --region flag is omitted, the platform chooses the region that’s fastest to reach from your location.
This sets the region property of the Machine.
Get or build the Docker image
All Fly Machines are made from Docker images. Once the Machine is created, you can see this image reflected in itsimage_ref and config.image properties.
With fly machine run, there are two options: point to a prebuilt image, or point to a Dockerfile, which flyctl will use to build an image.
Build from a Dockerfile
To build the image from a Dockerfile namedDockerfile, indicate the current working directory using the <image> argument.
--dockerfile option to specify a Dockerfile with a different name. For example:
Use an existing image
For example:Get a shell on a temporary Machine
The following command creates a temporary Machine using the Dockerfile in the working directory, and logs you into an interactive shell on it:--command flag allows you to specify a different shell if Bash isn’t present in the Machine’s Docker image. Log in as a non-root user using the --user flag.
If you just want a shell on a temporary Ubuntu Machine that’s in your org’s private network, omit the <image> argument:
Run with a custom ENTRYPOINT or CMD
You can have the Fly.ioinit override the ENTRYPOINT and CMD (if any) of the Machine’s Docker image.
Custom CMD
Override CMD by including the command to run at the end of thefly machine run invocation. This sets the config.init.cmd property on the Machine.
This example simply spins up a Debian Linux Machine with a sleep task to keep it awake; you can shell into it or whatever:
Custom ENTRYPOINT
Override ENTRYPOINT with the--entrypoint option. This sets the config.init.entrypoint property on the Machine.
In this example we use the --file-local option to send an entrypoint script to the Machine and --entrypoint to run the script before continuing to the custom CMD sleep inf:
entrypoint.sh you can use to test the above example:
Set Machine resources
Include one or more of the following options to use non-default specifications for the Machine to be run:config.guest properties.
Set environment variables
Specify environment variables to be available on the Machine with the--env flag, using NAME=VALUE pairs.
This flag sets the config.env property on the Machine.
Example:
Define a Fly Proxy network service
The--port option defines a network service to allow the Fly Proxy to reach a local service on the Machine. This option gives you access to basic service configuration; the Machines API and Fly Launch both offer more control over the Machine’s config.services properties.
Map any external ports, where the proxy accepts requests directed at the app, to the internal port where the service is listening on IPv4. For each port combination, specify the protocol and connection handler(s), using this format:
Important: If Machines within the same Fly App host different services, use different external ports so that they don’t receive requests meant for another Machine.
Set Fly Proxy autostop/autostart
The--autostart and --autostop flags only work on Machines with Fly Proxy services configured. Learn more about how Fly Proxy autostop/autostart works and how to configure it.
In a Machine service’s configuration, autostop and autostart settings are optional.
If the --autostop flag is absent in a fly machine run command, the Machine’s config.services.autostop value doesn’t get set, and the Fly Proxy does not shut the Machine down, even when there is no traffic to it.
If the --autostart flag is absent in a fly machine run command, the Machine’s config.services.autostart value doesn’t get set, and the Fly Proxy does not start it in response to requests.
The --autostart and --autostop flags set the value of autostart or autostop to true by default; you can explicitly set the value to false. For example, the following runs a new Machine that may be stopped by the Fly Proxy, but will never be restarted by it:
Stop or restart the machine on process exit
Set the Machine’s restart policy using the--restart option. Options are no, always, and on-fail, which correspond to no, always, and on-failure values for the Machine config.restart.policy property.
The default restart policy for a Machine created using fly machine run is always, unless you use the --rm option, in which case the default is no.
Destroy the Machine when it exits
By default, when a Machine isstopped, its file system is reset using its config and Docker image, and it sits ready to be started again. Use the --rm flag to cause the Machine to instead be destroyed when it stops.
The default restart policy for a Machine created with fly machine run --rm is no, to ensure that flyd doesn’t ignore the exit code and restart the Machine.
Mount a Fly Volume
A Fly Volume is a slice of an NVMe drive attached to the hardware that runs the Machine. Create a volume before creating the Machine.--volume <vol_name>:<mount_point>.
--volume <vol_id>:<mount_point>.
--volume flag on the fly machine run command sets a subset of the properties of the Machine’s config.mounts object.
Add metadata to the Machine
The Fly.io platform uses specific metadata, stored in a Machine’s config, for its own purposes, such as assigning Machines to process groups. You can add custom metadata as well. The following starts a Machine that thefly deploy command will try to manage as part of the app process group, replacing its image and config with what, if anything, you have set up in the working directory for that app.
Place data into files on the Machine
Thefiles property of a Machine’s configuration can be used to place data or secrets into files on the Machine file system.
Important: This won’t work for large files. There’s a limit to how much data can be stored in an app secret or a Machine’s configuration.
Copy a local file into the Machine file system
Use the--file-local flag to copy a local file onto the Machine at your specified path:
files.raw_value property of the Machine’s config; /path/inside/machine is stored in files.guest_path. When the Machine is created, the data is decoded and written to the file.
Pass data in on the command line
Place data into a file at your specified path, via an argument of the--file-literal flag:
files.raw_value property of the Machine’s config; /path/inside/machine is stored in files.guest_path. When the Machine is created, the data is decoded and written to the file.
Make a secret available in a file
Fly Secrets are stored in an encrypted vault, and by default they are available as environment variables on each of the app’s Machines. You can make a secret available in a file, rather than an environment variable. Encode the data in Base64 format and put it into an app secret withfly secrets set or fly secrets import. Use the --stage flag to prevent flyctl from initiating a deployment once the secret is registered.
Important: The secret must be Base64-encoded. If you try this with a secret that is not Base64-encoded, Machine creation fails.
--file-secret flag when creating the Machine with fly machine run. In this example I’m putting the contents of the secret called MY_BASE64_SECRET into a file /secret-file on my new Machine:
local-secrets and registers the Base64-encoded string as the value of the secret MY_SECRETS on the app:
MY_SECRETS secret available in a file (/secret-file):
files.secret_name config property and when the Machine is created, that secret is retrieved from the vault and its decoded value is written to the file.
Create a standby Machine
For the sake of resilience, you can create a stopped standby for Machines that don’t have Fly Proxy services and therefore can’t be supplemented by Fly Proxy “autostart” in case of a host failure.fly machine update.
The --standby-for flag sets the config.standbys Machine property.
Start a Machine on a schedule
Use the--schedule flag to set the Machine’s config.schedule property, which starts the Machine on a fuzzy hourly, daily, weekly, or monthly cycle. This is useful for running Machines that do a finite job, then exit. The Machine is started the first time when you run fly machine run, and again once per (approximate) hour, day, week, or month. For machines created with fly machine create --schedule, the first run will need to be manually started, since the cycle begins on the first machine start. Scheduled machines cannot be started via flyctl or Machines API commands, they will only run according to the schedule.
Important: If the host on which a stopped Machine resides doesn’t have the resources to start it when its scheduled time comes, you’ll get an error back. It’s up to you to build the appropriate level of redundancy into your apps.