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.