Skip to main content

Command Palette

Search for a command to run...

How to deploy Docker Applications with Laravel Forge

Updated
4 min read
How to deploy Docker Applications with Laravel Forge
P

Full-stack web developer with a taste for Laravel, Docker, and a few more components in the ecosystem

Most likely, if you’re reading this, you are a Laravel Forge user. Probably, also a Docker user or with interest in it. I was a happy Forge user for a long time in a row until I started using Docker, learned a bunch of its advantages, and ultimately fell in love with such.

Why use Docker for development and deployments, you may ask? I have a bunch of good reasons I could share, but that will be in another post.

Pre-requisites for this guide

  • You know what Laravel Forge is and you are familiar with hosting web applications (notion of server, DNS, )
  • You use Docker or Laravel Sail (which is a CLI and Docker environment for Laravel)
  • Servers for demo purposes are now deleted, which will break URLs in the screenshots

Show me how to get started!

First things first, let's provision a server.

image.png

The relevant thing to learn here is to choose a Worker Server. This is because we want the server as clean as possible and ports 80 and 443 available for web hosting. All other VPS options can be sized per your needs.

image.png

So, in a matter of minutes, we have our newly provisioned server! We also know its IP address, so it's time to make the DNS records and point the desired domains to such IP address.

image.png

I ended up doing this demo for the blog post with forge-docker-app.jpat.dev and forge-docker-traefik.jpat.dev, and we will see why in a minute.

So now we need Docker installed in our VPS. We can accomplish this very easily by adding a Recipe. Lets do just that and run it in our VPS. Copy source code from here

image.png

image.png

By now, we can see the recipe log, or SSH into our server and check Docker and Docker Compose are installed! 🚀

image.png

We are now ready to host Docker containers and Composes

So now we are going to install a "web server", which is Traefik. It's not exactly a webserver; think of this like a Router in your Laravel application. It will take care of the server ports 80 and 443, handle SSL with Let's Encrypt automatically, and point the requests to the right Docker application.

image.png

In the simple diagram, we can see a VPS running four containers: Traefik and three containerized apps. For this example, they would be all accessible from the outside, redirecting to SSL if needed and routing the request depending on the required host. It's a very simplified version and much more can happen. Let's keep it like this for simplicity's sake!

Our entry point to the Apps: Traefik

We can use this repository ijpatricio/traefik-for-forge as is; it's meant to be used by this "common use-case", but of course, feel free to fork, use and customize.

image.png

In Forge, inside our server, we go to create a new site, Traefik.

image.png

Now we install the repository and leave "Install Composer Dependencies" unchecked.

image.png

We now have a freshly installed repo! Now let's do some preparations. We can SSH into the machine and run the commands in the site's directory or run right from the Commands in the Forge UI. I absolutely recommend SSH into the machine and running it from there.

docker network ls | grep web || docker network create web

cd ~/traefik 

cp .env.example .env

cp docker-compose.prod.yml docker-compose.yml

# Choose a username and a strong password
./Taskfile auth username password

Let's now fill the environment variables in the Forge UI. Example below.

image.png

We also need to change the deploy script to:

cd /home/forge/traefik

git pull origin $FORGE_SITE_BRANCH

cp docker-compose.prod.yml docker-compose.yml

docker-compose up -d --remove-orphans

image.png

It should now look something like the above. We can press Deploy now.

image.png

image.png

With any luck, deploy must be complete now, and the deploy log will look something like the above, and a docker ps on the machine will show the running container, and mapped ports :)

Furthermore, we can visit https://forge-docker-traefik.jpat.dev/ and check that is password protected.

image.png

After entering credentials, we have our SSL secured Traefik dashboard.

image.png

Deploying Dockerized Applications

Woohooo!! Glad you made it so far! By now, we can already deploy applications just by using a docker-compose.yml file!

From here, we can use public images or adds login(s) to use with private registries.

Here, continue the journey on How to Dockerize a Laravel App to run in Laravel Forge.

Happy coding, happy deploys!!

Patricio

E

Muito bom o post. Quando vc instala Traefic não precisa configurar nada no cloudflare para apontar para esse sub domain? E pelo que entendi não vou precisar configurar ngnix tambem, nem para esse host e nem para os futuros containers(apps) com docker, correto?

P
Patricio1y ago

Obrigado Elinardo.

Pensa no Traefik como o "roteador". Dependendo da configuração, que tem em conta o hostname, encaminha pedidos para o serviço (container) correspondente.

O container de Laravel vai ter que ter Nginx e FPM ou outro webserver.

Se queres colocar Cloudflare com IP anonimizado, então terás de configurar o Certificado Origin. Da forma que está, é o Traefik que coloca SSL com Lets Encrypt.

Há muita forma de fazer este setup. O que te recomendo é experimentar várias, para que possas tirar conclusões do que é melhor para o teu caso de uso.

1
E

Patricio Muito exclarecedor! Obg. Deste que me deparei com seu post q venho estudando a possibilidade de containizar todos os meus prontos Laravel com vue. Pq mesmo com laravelForge que ajuda muito no setup, sempre dar trabalho. Localmente eu já uso laradock. Eu vou subir uma nova vps e ver se eu consigo fazer uma mistura de laradock com Traefik. Já vi uns posts sobre. Tai até uma ideia de vcs escrever algo sobre isso se n for pedir muito. Rsr Muito bom seus posts cara. Obg. Abraços do Brasil.

P
Patricio1y ago

Elinardo Silva Ora essa, muito gosto em conseguir ajudar.

Verifica esse projeto também https://github.com/serversideup/spin

As imagens base de Docker são espetaculares, o autor dá sempre mt feedback, e lançou recente esse Spin, que é como o Sail, mas para Dev e Prod

Um abraço

1
E

Patricio Valeu Patrício. Obrigado. Estarei verificando elas. Abraço

C

Very good article, especially for people making a first foray into the world of setting up Docker in Laravel Forge. I wish there were more basic articles for this kind of thing, for as always seems to be the case, there's no "middle ground", you either find articles that are way too basic and no virtually nothing, then there's a big leap towards "computer science" level where the article is just way too complex for "normal people" to follow along with...

1
P
Patricio2y ago

Thank you for the feedback! It feels very well to know, it's precisely the spot I try to explore... That middle ground you mention :)

More to come, I have more ways, maybe more fun to use in Forge also!

Thank you again!

A

Excellent tutorial - thanks so much! I've just set up my first Forge server with docker, and it's running fine.

1
P
Patricio3y ago

I'm very glad it worked for you!

I'm little concerned because some time has passed and eventually it will need some updates :)

But your comment is very reassuring.

Thank you so much!

Enjoy the containers :) :)

More from this blog

Patricio on Code

11 posts

How to deploy Docker Applications with Laravel Forge