Deploy Fooocus and Generate AI Images on Koyeb GPUs
Tags: ai, tutorial, webdev, programming
Published: 2024-06-27 10:14:00

[Fooocus](https://github.com/lllyasviel/Fooocus) is an AI-powered image generation tool that makes it easy to create custom images from descriptive prompts. Built using [Gradio](https://www.gradio.app/), Fooocus uses a prompt-centered approach to image generation, taking care of much of the behind configuration needed to produce high-quality images.

In this guide, we will demonstrate how to deploy and configure Fooocus on Koyeb's GPU instances. We will set up a custom `Dockerfile` to configure authentication and push our project to GitHub. Afterwards, we will deploy the project to Koyeb to build a container image and launch it on GPU-powered hardware.

You can consult the [repository for this guide](https://github.com/koyeb/example-fooocus) to follow along on your own. You can deploy the Fooocus instance by clicking the [Deploy to Koyeb button](/docs/build-and-deploy/deploy-to-koyeb-button) below:

[![Deploy to Koyeb](https://www.koyeb.com/static/images/deploy/button.svg)](https://app.koyeb.com/deploy?name=fooocus&type=git&repository=koyeb%2Fexample-fooocus&branch=main&builder=dockerfile&instance_type=gpu-nvidia-rtx-4000-sff-ada&env%5BCMDARGS%5D=--listen+--port%3D%7B%7B+PORT+%7D%7D&ports=8000%3Bhttp%3B%2F)

You will need to set the **Grace period** in the **Health checks** section to 300 seconds during configuration. If you want to password protect your Fooocus instance, add the `FOOOCUS_USERNAME` and `FOOOCUS_PASSWORD` environment variables with appropriate values. You can consult the appropriate sections of this guide for additional information.

## Requirements

To successfully follow and complete this guide, you need:

- A [Koyeb account](https://app.koyeb.com) to build and run the Fooocus instance on GPUs.
- Access to GPU Instances on Koyeb. [Join the preview today](https://www.koyeb.com/ai) to gain access.

## Steps

To complete this guide and deploy your own Fooocus instance, you'll need to follow these steps:

1. [Create a custom Dockerfile](#create-a-custom-dockerfile)
2. [Push the Dockerfile to GitHub](#push-the-dockerfile-to-git-hub)
3. [Deploy Fooocus on Koyeb](#deploy-fooocus-on-koyeb)

## Create a custom Dockerfile

The Fooocus project provides [Docker images](https://github.com/lllyasviel/Fooocus/pkgs/container/fooocus) based on the [`Dockerfile` included in the repository](https://github.com/lllyasviel/Fooocus/blob/main/Dockerfile). However, the provided images do not configure authentication by default, meaning that, once deployed, the Fooocus instance would be accessible to anyone who visits the URL.

To mitigate this, we will create a custom `Dockerfile` based on the official `Dockefile`. It will conditionally write a [`auth.json` file](https://github.com/lllyasviel/Fooocus?tab=readme-ov-file#ui-access-and-authentication) to the image filesystem if the appropriate variables are configured at build time.

To begin, create a new project directory and navigate inside:

```bash copy
mkdir example-fooocus
cd example-fooocus
```

Inside, create a `Dockerfile` with the following contents:

```dockerfile copy
FROM ghcr.io/lllyasviel/fooocus

ARG FOOOCUS_USERNAME
ARG FOOOCUS_PASSWORD

RUN if [ -n "$FOOOCUS_USERNAME" -a -n "$FOOOCUS_PASSWORD" ]; then echo "[ { \"user\": \"$FOOOCUS_USERNAME\", \"pass\": \"$FOOOCUS_PASSWORD\" } ]" > /content/app/auth.json; fi
```

If the `FOOOCUS_USERNAME` and `FOOOCUS_PASSWORD` variables are set at build time, this Dockerfile will create the `auth.json` file that Fooocus checks to configure authentication. If these values are not set, authentication will be disabled.

## Push the Dockerfile to GitHub

The small `Dockerfile` above is the only thing you need to configure authentication for Fooocus. Next, commit the file to a git repository and push it to GitHub.

[Create a new GitHub repository](https://github.com/new) and then run the following commands to commit and push changes to your GitHub repository:

```bash /<YOUR_GITHUB_USERNAME>/ /<YOUR_REPOSITORY_NAME>/ copy
git add :/
git commit -m "Initial commit"
git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>.git
git branch -M main
git push -u origin main
```

**Note:** Make sure to replace `<YOUR_GITHUB_USERNAME>` and `<YOUR_REPOSITORY_NAME>` with your GitHub username and repository name.

## Deploy Fooocus on Koyeb

Now that the `Dockerfile` is on GitHub, we can deploy it to Koyeb. On the **Overview** tab of the [Koyeb control panel](https://app.koyeb.com/), click **Create Web Service** to begin:

1. Select **GitHub** as the deployment method.
2. Select your Fooocus project repository. Alternatively, you can enter our public [Fooocus example repository](https://github.com/koyeb/example-fooocus) into the **Public GitHub repository** field at the bottom of the page: `https://github.com/koyeb/example-fooocus`.
3. In the **Environment variables** section, click **Bulk edit** to enter multiple environment variables at once. In the text box that appears, paste the following:

   ```bash copy
   CMDARGS=--listen --port={{ PORT }}
   FOOOCUS_USERNAME=
   FOOOCUS_PASSWORD=
   ```

   Set the variable values to reference your own information as follows:

   - `CMDARGS`: The `--listen --port={{ PORT }}` arguments configure Fooocus to listen for external connections. Koyeb automatically sets the `PORT` environment variable to the port it exposes. This will be automatically substituted with the correct value on deployment.
   - `FOOOCUS_USERNAME`: Set to the username you wish to use to authenticate. Remove this environment variable to deploy without authentication.
   - `FOOOCUS_USERNAME`: Set to the password you wish to use to authenticate. Remove this environment variable to deploy without authentication.

4. In the **Instance** section, select the **GPU** category and choose **RTX-4000-SFF-ADA**. These Instances are available when you request access to the [GPU preview](https://www.koyeb.com/ai).
5. In the **Health checks** section, set the **Grace period** to 300 seconds. This will provide time for the server to download all of the relevant models from Hugging Face and initialize the server.
6. Click **Deploy**.

Koyeb will pull the provided Fooocus repository, build the `Dockerfile` it contains, and run it on a GPU Instance. During deployment, Fooocus will fetch the provided model from Hugging Face and start up the service to expose it to users. If you provided authentication details, these will be configure during the build process.

Once the deployment is complete, access your Fooocus instance by visiting your Koyeb deployment URL. The application URL should have the following format:

```
https://<YOUR_APP_NAME>-<YOUR_KOYEB_ORG>-<HASH>.koyeb.app
```

Enter your provided username and password, if configured. The Fooocus prompt interface will be displayed, allowing you to generate images by describing what you would like to produce. To modify the default configuration, check the **Advanced** box below the prompt. From here, you can change the generator settings, the image styles, the models that will be used, and more.

## Conclusion

In this guide, we discussed how to configure and deploy a Fooocus Instance on Koyeb to generate AI images. We started with the [basic Fooocus Docker image](https://github.com/lllyasviel/Fooocus/pkgs/container/fooocus) and configured authentication based on user-provided variables. Afterwards, we deployed Fooocus to Koyeb by building the `Dockerfile` and launching it on Koyeb's GPU Instances.

This process provides you with a fully-functional AI-powered image generator. You can tweak the settings to change the styles the generator will use, its model configuration, and the formats it outputs. As you experiment, you might want to check out the [Fooocus Docker instructions](https://github.com/lllyasviel/Fooocus/blob/main/docker.md) and [troubleshooting tips](https://github.com/lllyasviel/Fooocus/blob/main/troubleshoot.md).