Now that we know whyDockerCompose is useful, itβs important to learn how to organize our project properly.
A good structure allows you to:
- Quickly understand your stack
- Make maintenance easier
- Share or version the project without any headaches
The recommended structure
Here is a classic and functional structure for a Compose projectDocker:
mon-projet/
ββ docker-compose.yml # DΓ©crit tous les services
ββ .env # Variables dβenvironnement (ports, mots de passe, etc.)
ββ app/ # Code de lβapplication (site web, API, scripts)
β ββ index.php
β ββ ...
ββ data/ # DonnΓ©es persistantes (bases de donnΓ©es, uploads)
β ββ mariadb/
ββ conf/ # Fichiers de configuration (php.ini, nginx.conf, Traefik, etc.)
β ββ ...
ββ logs/ # Journaux dβactivitΓ© si besoin
Folder descriptions
| Folder | Content & role |
|---|---|
| app/ | Contains all the application code. With a bind mount,Dockeryou can access your code directly from the container. |
| data/ | Contains persistent volumes for databases or files generated by the application. Example: ./data/mariadb:/var/lib/mysql. |
| conf/ | Contains configuration files specific to your services. Example: nginx.conf, php.ini, traefik.yml. |
| logs/ | Optional, for centralizing logs if you do not want to store them directly in the containers. |
| docker-compose.yml | Describes your services and how they interact. This is the core of the stack. |
| .env | Contains all your sensitive or configurable variables. Allows you to change the configuration without modifying the YAML. |
Best practices
- Separate code, configuration, and data: this simplifies backups and updates.
- Always use a
.envto avoid hard-coding passwords in the YAML. - Name your folders and services clearly:
app,db,cacheare preferable toservice1,service2. - Never store important data solely in the container: containers can disappear. Use volumes.
- Version control your project (Git) by excluding large data folders (
/data) with.gitignore.
π Key Takeaways
- A Compose projectDocker must be clean, readable, and maintainable.
- /app for code, /data for persistent data, /conf for configurations.
.envfor dynamic configuration.- This structure prepares you to manage more complex projects and collaborate with other developers.