DEV Community

Matthias Andrasch
Matthias Andrasch

Posted on

Simple Coolify example with Docker Compose + Github

I recently learned a bit more about Docker Compose and wanted to apply my new knowledge to Coolify. The first goal is to setup a simple webserver with nginx to deploy a "Hello World" HTML webpage.

What is Coolify?

Coolify is an "open-source & self-hostable Heroku / Netlify / Vercel alternative". It is developed by Andras Bacsai.

  • The advantage of Coolify compared to other (awesome) server management tools like ploi.io, Cleavr or Laravel Forge is that you can install it directly on your VPS without additional subscription costs. This is especially great for low budget or small hobby projects.
  • Another advantage is that you can connect your GitHub account and directly deploy repositories (like you would do on Vercel, etc).

My demo specs:

You could also choose ARM as a faster choice.

Install selfhosted Coolify on Hetzner VPS

Just create a new CPX11 cloud server on Hetzner, connect via SSH (ssh root@your-server-ip) and run the command from https://coolify.io/self-hosted:

curl -fsSL https://cdn.coollabs.io/coolify/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

That's really it. After installation, you can create your admin user via http://your-vps-server-ip:8000/. See this slightly older YouTube video for full example: https://www.youtube.com/watch?v=Jg6SWqyvYys

Add swap space to prevent "out of memory"

Sometimes I ran into "502 Bad Gateway" and 200% CPU load after hitting "Deploy" - while I had other (PHP + MySQL) resources running on my Coolify instance. I asked the Coolify Developer on GitHub and he gave me multiple suggestions.

I decided to try the swap option - Swap is disabled by default on Hetzner VPS (see: reddit discussion). I followed this guide (for ubuntu) and added 6GB swap space (since I only had 17G left on my small VPS) https://www.digitalocean.com/community/tutorial-collections/how-to-add-swap-space.

A simple Docker Compose example

The first goal is to setup a simple webserver with nginx to deploy a "Hello World" HTML webpage.

Create a new resource in Coolify

Let's get the party started by creating a new project:

Select production:

Add a new resource:

In the next step, it is important to use a Git Based approach. Don't get confused with the Docker options below.

I'll use the option "public repository" for now, but the cool thing is that you can also use private GitHub repositories.

My demo repository: https://github.com/mandrasch/coolify-docker-compose-simple-example

Now we can switch to Docker Compose:

After selecting Docker Compose as Build Pack option, you can configure the location of the Docker Compose file.

I decided to use /docker-compose.production.yaml in my example:

Image description

docker-compose.production.yaml

My compose file has the following content:

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile.prod
Enter fullscreen mode Exit fullscreen mode

⚠️ Normally, you would publish and expose ports like this in the docker compose file - don't do this on Coolify!

   ports:
      - "8080:80"
Enter fullscreen mode Exit fullscreen mode

Otherwise you will get this error:

Error response from daemon: driver failed programming external connectivity on endpoint web-XXXXXXXXXXX-XXXXXXXXXXX (XXXXXXX): 

Bind for 0.0.0.0:8080 failed: port is already allocated
Enter fullscreen mode Exit fullscreen mode

Coolify handles this on its own, just leave it out.

Dockerfile.prod

The Dockerfile defines how the Docker image will be built. The built image is then used to launch Docker containers. We simply use an nginx alpine linux image from DockerHub as starting point.

# Dockerfile.prod
# Use the official NGINX image
FROM nginx:alpine

# Copy the HTML files into the default NGINX public directory
COPY ./web /usr/share/nginx/html

# Expose port 80 (the default port NGINX listens on)
EXPOSE 80

# Start NGINX (this is the default behavior, no CMD or ENTRYPOINT needed)
Enter fullscreen mode Exit fullscreen mode

web/

In the web/-directory, we just have a simple index.html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello Coolify!</title>
    <style>
        body{
            font-family: Verdana, Geneva, Tahoma, sans-serif;
        }
    </style>
</head>
<body>
    <h1>Hello Coolify!</h1>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Create a temporary domain

Coolify provides temporary domains, just create it here:

This will give you a URL like http://XXXXXXXXXX.1.2.3.4.sslip.io.

Hit deploy

Click the Deploy button on the upper right and wait until the status changes to "Running".

Image description

Open it

We can now open the webpage in our web browser (this will use exposed port 80 by default):

Nice work! 🥳

Next tutorials

In the next tutorials I want to step up our game a little bit. My personal goal is to deploy and host CraftCMS websites (PHP + MySQL database + redis). Therefore we need at least three services in our Docker Compose config file. You could also deploy a headless CMS + a decoupled frontend like NuxtJS / SvelteKit / NextJS with that approach. See https://coolify.io/docs/knowledge-base/docker/compose for more information about Docker Compose on Coolify.

Other examples and tutorials:

PS: If you just want to deploy a single service, you can also use Nixpacks as very simple alternative: https://dev.to/mandrasch/deploy-sveltekit-with-ssr-on-coolify-hetzner-vps-24c5

Top comments (0)