Deploy Application Easily to Fly.io

Fly.io

Related Posts

All discussions about programming technologies and other technologies
As we move into 2025, the programming world is set ...
odoo erp
Installing Odoo over Docker can be a smart choice because ...

For those of you currently using shared hosting to publish your website or service online, many may be facing performance issues. This is due to the shared resources shared by multiple customers. Some of you may be considering upgrading to a VPS. But most VPS providers charge quite a high price for their services. A similar situation occurred to me. Considering several performance and cost considerations, I recently decided to switch from shared hosting to Fly.io for deploy my application. So, let’s talk about how to easily deploy application to Fly.io .

About Fly.io

Fly.io is a cloud hosting platform designed for developers who want to deploy applications close to their users with minimal configuration. It allows you to run full-stack apps and databases on servers located around the world, making your app faster and more responsive by reducing latency. With built-in support for Docker and support for multiple programming languages and frameworks, Fly.io simplifies deploying, scaling, and managing distributed applications. It’s especially popular for running modern web apps, microservices, and background workers in a globally distributed infrastructure. With Fly.io, you can easily deploy your application, maintain and monitor traffic, storage, and everything about the container and show it on the dashboard view.

Pre-requisite

To using Fly.io as server to host your application, you need to know some pre-requisite.

  • You need to create Fly.io account and validate your identity by adding payment information. You can register here.
  • You’ll need the Fly.io command-line tool (flyctl) to deploy and manage apps. You can install it using your package manager. In my case, i use brew in my MacOS.

brew install flyctl

  • Because Fly.io is container based (same like Docker), you need Docker on your local machine. It needed for building your application and testing the container before you deploy it. For more about Docker Container, you can follow this article.
  • You must be clear about all the requirements and dependencies used in the application you are going to deploy. This will affect the configuration that needs to be done in Fly.io.

After you have these prerequisite, you are ready to deploy your application to Fly.io. Let’s go to next step!

Prepare your application

The next steps is to prepare your application. Make sure your application is running well on the local machine using a Docker Container. In my case, I will deploy a WordPress website that I have already tested before in my local machine. It’s a simple sample, if you have other application to be deployed you can adjust the configuration file based on your needs. Here are configurations that you need to set up:

Modify working folder

Sometimes you need to modify the working folder to make the deployment process easier. It depends on what application you will deploy to Fly.io . In my case, I separate the folders for WordPress application configuration and database configuration. Here is my final working folder structure

Modify Dockerfile

What is Dockerfile? A Dockerfile is a text file that contains a set of instructions used to build a Docker image. It defines the environment, dependencies, and steps needed to run an application in a container. If you see on my working folder structure above, there are 2 Dockerfiles in 2 different folder. The first is inside root folder of project ( /programmermumet ), this will contain configuration for the main WordPress application. The second one is in subfolder ( /programmermumet-db ), this will contain configuration of the database. In may case, I need to create 2 different containers in Fly.io because I separate the WordPress application with the database. This configuration can be different with your need. This is sample of Dockerfile code:

/programmermumet/Dockerfile :

# Use the official WordPress image (version 6.8.1 with PHP 8.1 and Apache) as the base
FROM wordpress:6.8.1-php8.1-apache

# Update the package list, install nano editor and MySQL client,
# enable Apache modules (rewrite and headers), and clean up to reduce image size
RUN apt-get update && \
    apt-get install -y nano default-mysql-client && \
    a2enmod rewrite headers && \
    rm -rf /var/lib/apt/lists/*

# Copy a custom PHP configuration file into the container
# This file may include settings like upload_max_filesize, post_max_size, etc.
COPY ./php.ini /usr/local/etc/php/conf.d/uploads.ini

# Set the working directory to the web root (where WordPress files live)
WORKDIR /var/www/html

# Expose port 80 so it can accept HTTP traffic
EXPOSE 80

NB: Your application may require more complex configuration

Create fly.toml file

The fly.toml file is the configuration file used by Fly.io to manage how your app is deployed and runs on their platform. It defines these things:

  • Name and regions
  • Docker image or build process
  • Networking (ports, protocols)
  • Environment variables
  • Volumes
  • Services (HTTP, TCP, etc.)
  • Scaling options

This is sample of it:

# The name of your Fly.io app. Must be unique across Fly.io.
app = 'programmermumet'

# The preferred region where your app will be primarily deployed (e.g., Singapore).
primary_region = 'sin'

# Section to define how the app is built.
[build]
  # Use a Dockerfile in the project directory to build the image.
  dockerfile = 'Dockerfile'

# Environment variables passed into the container at runtime.
[env]
  # Hostname of the MySQL database service (internal to Fly's private network).
  WORDPRESS_DB_HOST = 'programmermumet-db.internal'
  # Name of the WordPress database.
  WORDPRESS_DB_NAME = 'wordpress'
  # Password for the WordPress database user.
  WORDPRESS_DB_PASSWORD = 'dbpassword'
  # Username for the WordPress database.
  WORDPRESS_DB_USER = 'dbuser'

# Mount configuration for persistent storage.
[[mounts]]
  # The name of the Fly.io volume (must be created with `fly volumes create`).
  source = 'wp_data'
  # Where the volume will be mounted inside the container (WordPress content directory).
  destination = '/var/www/html'

# Service configuration to expose and route traffic.
[[services]]
  # Protocol used by the service (HTTP over TCP).
  protocol = 'tcp'
  # Port exposed inside the container (WordPress uses port 80).
  internal_port = 80

  # Publicly expose port 80 for HTTP requests.
  [[services.ports]]
    port = 80
    handlers = ['http']

  # Publicly expose port 443 for HTTPS requests.
  [[services.ports]]
    port = 443
    handlers = ['tls', 'http']

  # Health check to ensure the service is reachable and running properly.
  [[services.tcp_checks]]
    # Time between each check.
    interval = '15s'
    # How long to wait before a check times out.
    timeout = '2s'
    # Grace period after deployment before health checks start.
    grace_period = '5s'

# Virtual machine (VM) resource allocation for this app.
[[vm]]
  # Allocate 1 GB of RAM.
  memory = '1gb'
  # Use a shared CPU (cost-effective option).
  cpu_kind = 'shared'
  # Number of virtual CPUs assigned to the app.
  cpus = 1

Authenticate Your Identity to Fly.io

Before connect to Fly.io, you need to login first to verify your identity. You can do this by execute this script on your working folder :

fly auth login

Create container on Fly.io

Go to the folder where you put fly.toml, and you can create container on Fly.io using this script:

fly launch --name my-wordpress-app --no-deploy

Set Up Persistent Volume for Your Application

What is persistent volume? In Fly.io, a persistent volume is a storage space that remains intact across app restarts or redeploys. It’s essential for storing data that your app needs to keep like uploaded files, user content, or databases.

This is the sample script to create persistent volume :

fly volumes create wp_data --size 1 --region sin

On script above, i create a volume called wp_data with size 1 GB in Singapore region. You can change these values based on your own configuration.

Final Step, Deploying Application

After all steps above, the last step is to deploy the application. You can deploy your application using this script :

fly deploy

Wait until the process finish. After finish, you will show url of your application on the console. Click it to open your application, or you can use this script to open it if needed:

fly open

Conclusion

Fly.io can be alternative for deploy your application with good performance and cheaper price especially for small application/website. With container based architecture, you can easily deploy your application/website and monitor all about it on Fly.io dashboard.

Scroll to Top