dunglas / symfony-docker

A Docker-based installer and runtime for Symfony. Install: download and `docker compose up`.

Home Page:https://dunglas.dev/2021/12/symfonys-new-native-docker-support-symfony-world/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to run multiple projects

JoppeDC opened this issue · comments

Hello!

I just wanted to verify that, with this image using Caddy, its impossible to run multiple projects side by side.

For example, i often work on several projects, and leave all their containers running.
Running a single project with this dockerfile is fine, but with multiple projects the ports collide (and Caddy needs those specific ports, it appears you cant use the docker auto generated ports).

Is this correct? If so, i'm not sure this is a perfect solution. Running multiple projects is often a must, and therefore, maybe we can look for a solution?

Hello @JoppeDC!

You can use the 127.0.0.0/8 subnet to run multiple servers on the same port.

You will also have to configure the network for Docker so that applications can be launched on the necessary ports and make changes in /etc/hosts.

Example by steps:

  1. Create new Docker networks
docker network create -o 'com.docker.network.bridge.host_binding_ipv4=127.0.10.1' app1
docker network create -o 'com.docker.network.bridge.host_binding_ipv4=127.0.10.2' app2
docker network create -o 'com.docker.network.bridge.host_binding_ipv4=127.0.10.3' app3
  1. Configure the /etc/hosts
+ app1.localdomain www.app1.localdomain 127.0.10.1
+ app2.localdomain www.app2.localdomain 127.0.10.2
+ app3.localdomain www.app3.localdomain 127.0.10.3
  1. Configure the created networks in the compose.override.yaml files
  services:
    php:
+     networks: ["app1"]
      ...

    database:
+     networks: ["app1"]
      ...

  networks:
    ...
+   app1:
+     external: true

The created networks must be configured for all project containers. Each project should have its own network.

After this configuration, each of the projects will be launched on the same port, but have different host names (addresses). This will allow you to run multiple servers on the same port.

Reference:

Thanks for this very fast reply @7-zete-7 !

This looks very promising. I did not know about host_binding_ipv4, there's still much to learn when it comes to docker.

Although i'm unable to get this to work (I cant even visit the IP directly without the hosts config, which is not 1 to 1 because i'm using mac), it might be related to the fact i'm using Macos.

It seems like this caddy container is a cool and simple way to work on a single project, but quickly introduces a lot of complexities when using multiple projects. Which is why i find it a lil sad that this is the "Official" symfony method.

We are using Traefik for this purpose - you can have any number of docker-compose projects, on each of them you add a "label" to docker-compose file with a hostname to which it should respond, and then Traefik is the only one listening on 80/443 and transferring traffic to actual HTTP servers (like Caddy). If you use whatever.localhost, then you even don't need to touch hosts file, as everything is by default automatically recognized and sent to proper container.
Look at Quick Start to see how easy you can set-it-up.

For completeness, i'm gonna add how i resolved this, witgout the use of a proxy or networks.

So. Caddy NEEDS 80 and 443, when using it out of the box. This is because it automatically requests ssl etc.

Now, if you run it with docker auto generated ports, it doesnt work. Its internal config needs to map the port to the “site” (as you would in eg. nginx or apache config).

So, what you can do, is provide your own certs (with mkcert, just add em to the repo), and run it on 2 other fixed ports. (Eg. 2015 for http and 20015 for https), you then need to configure these ports in the caddyfile as well for the caddy config, and boom, custom ports per project.

Only downside, you still need to define 2 ports in the .env file (i reuse those in docker and caddy config). So need to make sure every project has its own port defined