Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

luke___'s avatar

I don't get docker compose and Laravel; why multiple containers?

I'm new to using Docker and docker compose and I'm struggling to 'get it'. my question is; why have multiple containers?

Can someone help me understand why I would use docker compose and multiple containers?

I feel like i'm looking for a diagram or description that will 'click' for my understanding , but I can't find it. Can anyone suggest an explainer?

Thanks!!!

0 likes
18 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Yeah it can seem a bit strange at first, but it lets you do awesome things. Be aware that each container is a tiny linux image that only does one thing (eg. run php, or mysql or similar). So the image is alot smaller than a full linux installation.

But we can set up each container exactly like we want without them having any influence on the others. So I can install something I need for php in the php container without it touching mysql or redis for example.

If I want to install something in a specific container, I can just rebuild that container and nothing else. So changing this is fast. And I can easily go back, but just removing the build instruction and rebuilding again.

It will also allow me to run multiple containers with the same type of data. For instance I could have 2 containers with mysql if I wanted.

It also makes it easy to try out things. Say I want to try out meilisearch. I just add a new container with that and try it out. If I dont like it, I just remove that container again. I dont need to uninstall anything

And finally a thing that I have used alot myself. When I am upgrading php versions (like 7.4 to 8.0) I add two seperate php containers to that project. One with the old version and one with the new. Now I can run tests in both at the same time, to make sure that I dont get false positives in my upgrade.

Personally I use lando to run my containers: https://sinnbeck.dev/posts/using-lando-to-run-laravel-in-docker

1 like
luke___'s avatar

@Sinnbeck this is great, thanks Sinnbeck.

And in local dev/production do you have a 'workflow' of sorts for Laravel + Docker + Docker Compose?

In the Docker video series I saw the trainer create a 'temp' container for composer and then destroy it once it did its job the first time. Do you, or someone on the forum, know what's happening when in this situation? So a little linux box is created with just composer and it's networking in to the other containers? And then the linux box/container gets deleted?

Sinnbeck's avatar

@luke2020 If you check my guide linked in my post that is actally the first item :) It downloads the composer image, runs it to install laravel, and has it remote itself (the --rm flag)

luke___'s avatar

@Sinnbeck haha, sorry. I clicked the link to read later so I could come back here check the replies.

I'll get reading!! :-)

Sinnbeck's avatar

@luke2020 But of course you can do the same without lando if you want.

This will just install laravel :)

SITENAME=my-app; docker run --rm --interactive --tty \
  --volume $PWD:/app \
  --user $(id -u):$(id -g) \
  composer create-project laravel/laravel $SITENAME ^9.0
1 like
johnDoe220's avatar

To containerize an application refers to the process of adapting an application and its components in order to be able to run it in lightweight environments known as containers. Such environments are isolated and disposable, and can be leveraged for developing, testing, and deploying applications to production.

johnDoe220's avatar

@Sinnbeck The reason for this problem is that when I put a link in the reply, it does not appear or the changes are not applied after editing, I find it strange, I even changed the profile picture many times, but it does not change

sr57's avatar

as @sinnbeck wrote Docker container are a nice tool to do different (configuration) tests.

As it's one more layer, it consume resources of your server, I never use it in prod. If, for any reason, I want, to do same thing in prod (isolation of some apps, have a quick backup from a last version for instance) I use Lxc container that's the basement of Docker.

luke___'s avatar

Thanks @sr57. Is this what you're referring to? https://linuxcontainers.org/

So, you would have a vm on aws/gcp and use Lxc on the VPS? do you have a workflow for Laravel or is it easy enough can get start looking at it?

sr57's avatar

@luke2020

Is this what you're referring to? https://linuxcontainers.org/

Yes.

it easy enough can get start looking at it?

Not to difficult, but Docker is/should be easier as it's a layer above, I used Lxc before the creation of Docker,. Today I suggest you to 'play' in dev/test first with virtualbox, then Docker and then Lxc if you want some 'resources optimized' in prod server.

1 like
luke___'s avatar

I've done some more Googling and found this diagram and explainer on the lifecycle of a docker container, which has helped me make a bit more sense of what's happening.

https://www.educative.io/edpresso/what-is-the-docker-container-lifecycle

Does anyone know of a good diagram/explainer on how multiple containers are networked together? Because my understanding is they are each separate instances and have their own 'scopes', if that's the right way to describe it.

Sinnbeck's avatar

@luke2020 Networking in docker is quite complex. But if you are working on a single project and you are using docker-compose, then each container in that project can talk together. To make this simple, each container exposes a name. By default this is the name of the service. So if I make a service called mysql, then I will contact that from my php container, using the hostname mysql

If you need to communicate between projects, it gets quite complex, and you will need to manually specify a common network and how it all connects.. Or use lando as it works here out of the box (using the traefik proxy)

2 likes
luke___'s avatar

@Sinnbeck ah ok, so it's like an alias?

so one project that uses multiple containers will leave docker to network those containers together and give me a named alias (php if i call my php container the name php and so on), and i don't have to worry about the wiring at the lower level.

great, that makes a lot of sense

Sinnbeck's avatar

@luke2020 Exactly. But be aware that your containers arent open to your host machine unless you tell it to :) You expose the port (like 80 for the webserver, 3306 for the mysql db if you need to connect to it from a database manager etc)

luke___'s avatar

@Sinnbeck Understood. And this is helping me understand what's happening with using a container image with Cloud Run.

Please or to participate in this conversation.