
Overview
This guide explains how to use base images for faster deployments. A base image is any image that is intended to be used as theFROM line in a Dockerfile. Each app that is deployed on Fly.io can add an image to the Fly registry.
Every app in the same organization can access the image for any other app and use it as the FROM line in their Dockerfile. This means that it is very easy to make a base image by making a second app to use as the base image.
Why use a base image?
Most developers have a specific reason for using a base image in their project. Usually it’s one of the following:- The base image can be built with all of their app’s dependencies. Rebuilding the app on top of this base image means the deploy process is only running the app-specific build processes, not the entire dependency tree.
- The same app is deployed for a number of different end users. Each end user has some specific files or configurations to build into the image. A base image can get most of the way to the final configuration, and their app can do the final steps.
How to make a base image
This guide walks through building and deploying an example application called go-fly-a-site, which runs a simple Go web server. After deploying the app, you’ll create a base image from its non-app-specific parts. Finally, you’ll update the app to use that base image.Making the app
Our program will be very simple. It will respond to HTTP requests on port 8080 and respond with “Hello World!”.main.go and the Dockerfile in your project directory, deploy the application:
fly.toml and have created the app go-fly-a-site.
Deploy the app:
fly deploy we might be reinstalling Go through apt. We probably want to do that much less often than we want to update our main.go, so we can make a base image that installs go for us.
Make a base image
Let’s make a new file calledbase.Dockerfile. For this file, we will keep the Go install. We will replace the previous CMD statement with [ "sleep", "inf" ] so that we can easily SSH into this image to inspect it if needed in the future.
fly.toml to fly-base.toml and make the following modification:
- Append -base to the app name.
- Explicitly include dockerfile in the
[build]section. - Remove the
[http_service]section.
--ha=falseensures the app uses a single Machine.--config base.fly.tomlspecifies the correct config file.
fly.toml file, select yes to copy its configuration. You can skip tweaking the settings.
- We have the base image name we can use, it’s the URL provided in
image:. - When we go to the admin URL, we can click Registry and see the same image URL.
- The Registry will have the list of images that have been created.
- Any app in your organization can use the image URL for a
FROMstatement in a Dockerfile. - Apps in any other organization will get an error if they try to use this image.
Updating the app to use the base image
After creating the base image, update the Dockerfile for go-fly-a-site:fly deploy.
fly machine stop with the app and Machine id:
main.go code without having to reinstall Go. If we ever need to add additional dependencies to the base image, we just modify the base.Dockerfile, deploy it again, and get the new image: from the deploy command or in the Registry section of the app’s dashboard.
Command Reference
Get Machines for the base image:fly machine list --config base.fly.toml
Start Machine for the base image: fly machine start <ID_FROM_LIST> --config base.fly.toml
Stop Machine for the base image: fly machine stop <ID_FROM_LIST> --config base.fly.toml
Get SSH access to the base image: fly ssh console --config base.fly.toml (Make sure the Machine is started)
Rebuild the base image: fly deploy --config base.fly.toml (Update base.Dockerfile with your changes first, and remember to stop the Machine when you’re done)
Additional Notes
If you have a base image that many different apps will use, it can make sense to keep it all in its own app directory. You can usefly.toml and Dockerfile like a normal app.
The base image will stick around in the registry and as long as an image is associated with a Machine — even if the Machine is stopped — it won’t be deleted.
You can find the registry URL of the images for your base image in the dashboard under the Registry tab for the app, or by running fly releases --image
Related use case
Want to skip the Fly builder and use Docker tools directly to manage your images? See Managing Docker Images with Fly.io’s Private Registry, a guide that walks through building, pushing, deploying, and managing images via Fly.io’s Docker registry!Related reading
- Deploy with a Dockerfile The general guide on how to deploy apps from Dockerfiles — useful context when you’re building a base image.
- App Configuration (
fly.toml) Details on how yourfly.tomlorchestrates builds, images, and deployments.