
What’s Imaginary
Let’s start with h2non/Imaginary, a fantastically useful image processing application written in Go. Once configured, it lets you process images. The images can be posted to it or you can ask it to get an image from a remote server and all of this is controlled through a URL request to the Imaginary server. You can find all the source in the GitHub repository, but for this guide, we don’t need to worry about that. We’re going to use the developer-supplied Docker image to deploy it.Deploying Docker Images to Fly
How do you turn a Docker image into a globally available edge application? With Fly it’s remarkably concise process. One thing we know at Fly is that our users want to deploy more than JavaScript at the edge. Powerful though that is, they also want fully fledged application stacks and backend components to live at the edge too. Now they can, and in this series of guides we’ll show you how to use them. In this first guide, we’re going to take an existing application and make it go global with Fly.Creating the Dockerfile
To use a Docker image, we’ll need a Dockerfile. Create a new directory and create aDockerfile in it with these contents.
FROM line selects the image of the application and version we want to use. The Imaginary image is configured to run the imaginary command.
Imaginary, running in a Docker container, normally opens a port on port 9000, but for this exercise, we want to open its port on 8080, Fly’s default port for external communications. The simplest way to do that is to set the PORT environment value to 8080.
The third CMD line contains the parameters we pass to the imaginary command that are going to configure how Imaginary runs. The -enable-url-source tells Imaginary it is allowed to retrieve images from around the internet and, just so it’s well-behaved, we set the -http-read-timeout to 3 seconds (the default is 60 which is a long time to wait for an image).
Testing Locally
You’ll need to have Docker installed locally to test this locally. We won’t go into the details of installing Docker - check out the Docker site and guides for how to install. Assuming you’ve done this we can move on to testing.Build a Docker Instance
To create a container instance using the Dockerfile, run:Run the Docker Instance
To start up the Docker container, run:ENV line in our docker file.
Processing an Image
To test our local image, let’s process a file. In this case, we’re going to crop an existing image from Imaginary’s github repository. In another terminal session, run:large.webp being written to disc which you can display using your preferred image viewer. MacOS users can simply open large.webp at the command line.
Open the cropped image. Take a moment to play with the width and height values in the URL we’re querying and you will get different sized cropped images. Once you are happy the local build is working, you can return to the session where you started the docker image and ctrl-C to stop it.
We now have a working docker image. Now to globally deploy it with Fly.
Going Global
For this you’ll need flyctl, it’s your CLI-powered remote control for your Fly applications.Installing flyctl
If you’ve already installed flyctl, move on to the next step. If not, hop over to our installation guide.
Sign In (or Up) for Fly
If you have a Fly account, all you need to do is sign-in withflyctl.
New User?
Welcome to Fly. To get your Fly account run:- Sign up with Email: Enter your name, email and password.
- Sign up with GitHub: If you have a GitHub account, you can use that to sign up. Look out for the confirmatory email we will send you which will give you a link to set a password; you’ll need a password set so we can actively verify that it is you for some Fly operations.
Returning User
Run:Sign in with GitHub button to sign in.
Whichever route you take you will be signed-up, signed-in and returned to your command line, ready to Fly.
Preparing to fly
Creating a fly.toml File
The flyctl app helps you handle the entire lifecycle of your Fly Global application. That all starts with creating the fly.toml file which will control how your application is configured when deployed. To do so, run:
flyctl gets to generating the app.
fly.toml? If you look in it, you’ll find various configuration parameters. You can skip to Deploying if you want to.
services.concurrency which sets soft and hard limits to the number of connections the application should take before being transparently scaled horizontally.
There are also two services.ports sections. The first says incoming connections on port 80 will be routed to port 8080 and handled as HTTP. The second, more interesting entry, says that incoming connections on port 443 will be handled as TLS terminated HTTP connections and then routed on as HTTP to port 8080.
There’s also a services.tcp_checks section which is used to define how we do health checks on the application.
We can leave all of this completely untouched for this guide. We’re ready to Fly globally.
Deploying
For deployment, we go back to flyctl.fly.toml file automatically and start the deployment process. There’ll be a fair amount of output as flyctl gets Docker to assemble the image, create a release of the image and then deploy and check it’s running. When it is complete you’ll see something like:
Viewing Fly Apps
You’ll need the URL for the deployed application to connect to it. Flyctl helps out here with theflyctl info command:
Testing Imaginary on Fly
This tells you all you need to know about your currently deployed application. If you take theHostname you can use that in a URL request to the Fly deployment:
Summary
In this guide we created a Docker image, based on a third-party image, tested it locally then deployed it to the Fly platform usingflyctl. You now have a Global Image Service. Whenever you request it processes an image, Fly will create an instance of the service closest to you to fulfill your request.