Embrace Docker: The Ultimate Guide to Building Symfony Projects with NGINX and PostgreSQL

Mohamed BESBES
3 min readMar 24, 2024

--

Let’s build a functional Symfony application within a Docker container.

Embrace Docker

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Install Docker Desktop
  4. Basic Docker Commands
  5. Understanding Docker
  6. Advantages of Docker over XAMPP/WAMP
  7. GitHub Repository
  8. Folder Structure
  9. Setting Up Docker for Symfony
  10. Symfony + NGINX + PostgreSQL Configuration

1. Introduction:

In the fast-paced world of web development, efficiency and scalability are paramount. Traditional local development environments like XAMPP and WAMP have served us well, but it’s time to elevate our game. Enter Docker, a powerful tool that simplifies the development process and ensures consistency across different environments. In this guide, we’ll explore why Docker is the future and how you can use it to build your Symfony projects with NGINX and PostgreSQL.

2. Prerequisites:

  • Docker installed
  • Access to CLI
  • Make installed

3. Install Docker Desktop:

If you do not yet have Docker Desktop installed, click the appropriate link for your local machine and follow the installation instructions:

Install Docker Desktop on Linux

4. Basic Docker Commands:

These commands provide insights into the Docker version on your machine, offer configuration details, and unveil the array of commands at your disposal in the Command Line Interface (CLI).

docker ps -a: List all containers, including stopped ones.

docker info :Tells detailed config info about the Docker engine.

docker images:List all locally available Docker images.

5. Understanding Docker:

Docker is a containerization platform that allows you to package an application and its dependencies into a container. Unlike traditional environments, Docker containers ensure consistency and portability, making it easier to develop, deploy, and scale applications.

6. Advantages of Docker over XAMPP/WAMP:

  • Isolation: Docker containers are isolated, ensuring that each application has its own environment without interfering with others.
  • Consistency: Docker eliminates the “it works on my machine” problem by providing a consistent environment for developers, testers, and production.
  • Scalability: Docker containers can be easily scaled up or down to meet the demands of your application.
  • Environment Independence: Docker allows you to define your application’s environment, reducing the risk of compatibility issues across different machines.

7. GitHub Repository:

GitHub repository link: Symfony Docker Boilerplate on GitHub.

Feel free to check it out, fork it, or even open issues if you encounter any bugs or have suggestions for improvements. Your contributions are highly appreciated and can help make this project even better for the community.

Let’s collaborate and build something amazing together!

8. Folder Structure :

Symfony Folder Structure

9. Setting Up Docker for Symfony:

  • Compose with Docker Compose: Use Docker Compose to define and run multi-container Docker applications. Create a docker-compose.yml file to specify services, such as Symfony, NGINX, and PostgreSQL.
version: '3.8'

services:
postgres:
restart: always
image: postgres
user: "${USER_UID}:${USER_GID}"
container_name: "${DATABASE_CONTAINER_NAME}"
ports:
- "5432:5432"
environment:
POSTGRES_USER: ${DATABASE_USER}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: ${DATABASE_NAME}
volumes:
- ./data/postgres:/var/lib/postgresql/data

pgAdmin:
restart: always
image: dpage/pgadmin4
ports:
- "5000:80"
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD}
depends_on:
- postgres

php:
container_name: "php-fpm"
build:
context: ./docker/php
args:
PHP_VERSION: ${PHP_VERSION}
environment:
# Symfony Config
- APP_ENV=${APP_ENV}
- APP_SECRET=${APP_SECRET}
- DATABASE_URL=${DATABASE_URL}
- MESSENGER_TRANSPORT_DSN=${MESSENGER_TRANSPORT_DSN}
- APP_DEBUG=${APP_DEBUG}
volumes:
- ${APP_FOLDER}:/var/www

nginx:
container_name: "nginx"
environment:
HOSTNAME: ${HOSTNAME}
build:
context: ./docker/nginx
volumes:
- ${APP_FOLDER}:/var/www
- ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./logs:/var/log
depends_on:
- php
ports:
- "80:80"

10. Symfony + NGINX + PostgreSQL Configuration:

  • Symfony Configuration: Configure your Symfony application to work seamlessly with Docker. Adjust database configurations and environment variables to suit the containerized environment.
  • NGINX Configuration: Set up NGINX as the web server for your Symfony application. Define server blocks, SSL configurations, and other settings to optimize your web server for production.
  • PostgreSQL Integration: Docker allows you to easily spin up a PostgreSQL container for your Symfony project. Configure Symfony to use PostgreSQL as its database backend.

Conclusion:

  • Recap the benefits of using Docker for Symfony development.
  • Encourage readers to adopt Docker for their projects, emphasizing the scalability, consistency, and efficiency it brings to the development process.

By embracing Docker for your Symfony projects with NGINX and PostgreSQL, you’re not just keeping up with the times; you’re future-proofing your development workflow. It’s time to say goodbye to XAMPP and WAMP and usher in a new era of containerized, scalable, and efficient web development.

--

--

Mohamed BESBES

Full-Stack Developer 💻 , Interested in all ways to simplify complex ideas.