Deploying Symfony applications can be complex, but using Docker and Kubernetes simplifies the process significantly. This step-by-step tutorial guides you through deploying a Symfony app using these powerful tools, ensuring a scalable and efficient deployment pipeline.

Prerequisites

  • Basic knowledge of Symfony framework
  • Docker installed on your machine
  • Kubernetes cluster (local or cloud-based)
  • kubectl CLI configured
  • Access to a container registry (Docker Hub, GitHub Container Registry, etc.)

Step 1: Prepare Your Symfony Application

Ensure your Symfony application is ready for containerization. Run the following command to build your app:

composer install --no-dev --optimize-autoloader

Verify that your application works locally before proceeding.

Step 2: Create a Dockerfile

Create a Dockerfile in the root of your Symfony project with the following content:

FROM php:8.2-fpm

# Install dependencies
RUN apt-get update && apt-get install -y \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    zip \
    unzip \
    git

# Install PHP extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd pdo pdo_mysql

# Set working directory
WORKDIR /var/www/symfony

# Copy existing application directory contents
COPY . /var/www/symfony

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install dependencies
RUN composer install --no-dev --optimize-autoloader

# Set permissions
RUN chown -R www-data:www-data /var/www/symfony

EXPOSE 9000

CMD ["php-fpm"]

Step 3: Build and Push Docker Image

Build your Docker image with a descriptive tag:

docker build -t yourusername/symfony-app:latest .

Push the image to your container registry:

docker push yourusername/symfony-app:latest

Step 4: Create Kubernetes Deployment and Service

Define your deployment in a deployment.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: symfony-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: symfony
  template:
    metadata:
      labels:
        app: symfony
    spec:
      containers:
      - name: symfony
        image: yourusername/symfony-app:latest
        ports:
        - containerPort: 9000
---
apiVersion: v1
kind: Service
metadata:
  name: symfony-service
spec:
  type: LoadBalancer
  selector:
    app: symfony
  ports:
  - port: 80
    targetPort: 9000

Step 5: Deploy to Kubernetes

Apply your configuration with the following command:

kubectl apply -f deployment.yaml

Check the status of your pods and service:

kubectl get pods

kubectl get svc

Step 6: Access Your Symfony Application

Once the LoadBalancer is provisioned, retrieve its external IP address:

kubectl get svc symfony-service

Open this IP in your browser to see your Symfony application live.

Conclusion

Using Docker and Kubernetes streamlines the deployment of Symfony applications, offering scalability and ease of management. Follow these steps to deploy your app efficiently and prepare for production environments.