John Davidson

Containerizing legacy PHP Laravel project

0 comments
Message:


I have to containerize a legacy PHP Laravel application and deploy it in an EKS cluster, and as I am completely new to both PHP and Laravel, I am currently having some difficulties.


After googling some examples of a Laravel Dockerfile, there seems to be many different methods of doing this and I had some trouble understanding and executing the process.


In one of the blogs I found, it seems to use a Dockerfile and a docker-compose.yaml file to containerize the application like the one used below.


FROM php:7.3-fpm
# step 2
WORKDIR /root
RUN apt-get update
RUN apt-get install -y curl
# step 3
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/bin/composer
# step 4
RUN apt-get install -y zlib1g-dev && apt-get install -y libzip-dev
RUN docker-php-ext-install zip
# step 5
RUN composer global require laravel/installer
RUN ["/bin/bash", "-c", "echo PATH=$PATH:~/.composer/vendor/bin/ >> ~/.bashrc"]
RUN ["/bin/bash", "-c", "source ~/.bashrc"]
# step 6
EXPOSE 9000
CMD ["php-fpm"]

version: '3'
services:
proxy:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./proxy/nginx.conf:/etc/nginx/nginx.conf
web:
image: nginx:latest
expose:
- "8080"
volumes:
- ./source:/source
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
php:
build:
context: .
dockerfile: php/Dockerfile
volumes:
- ./source:/source

I am guessing that the nginx is used for the web application server, kind of how Apache Tomcat is used in Spring Boot, but other than that, I am a little bit unclear on why there needs to be a yaml file for this.


In addition, I composed using the Docker and docker-compose.yaml file with the following command.


docker build -t website -f Dockerfile .

I did succeed in exporting the image, but I seem to have trouble running a container using this image.


It would be sincerely appreciate if you could tell me what I am doing wrong.


Thank you in advance!



My Answer:
WORKDIR /var/www/html

COPY . .

RUN apt-get update && apt-get install -y \
git \
zip \
unzip \
&& docker-php-ext-install pdo_mysql

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

RUN composer install

CMD php artisan serve --host=0.0.0.0 --port=8000

EXPOSE 8000

This Dockerfile sets up a PHP environment with Laravel dependencies, installs Composer, runs Composer install to install Laravel dependencies, and then starts the Laravel server.

To deploy this containerized Laravel application in an EKS cluster, you will need to build the Docker image using the Dockerfile and then push the image to a container registry like Docker Hub or Amazon ECR. You can then create a Kubernetes deployment manifest file to deploy the application in the EKS cluster.

Here is an example Kubernetes deployment manifest file:

apiVersion: apps/v1
kind: Deployment
metadata:
name: laravel-app
spec:
replicas: 3
selector:
matchLabels:
app: laravel-app
template:
metadata:
labels:
app: laravel-app
spec:
containers:
- name: laravel-app
image: your-container-registry/laravel-app:latest
ports:
- containerPort: 8000

You can apply this deployment manifest file using kubectl apply -f deployment.yaml to deploy the Laravel application in the EKS cluster.

I hope this helps you get started with containerizing and deploying your legacy PHP Laravel project in an EKS cluster. Let me know if you have any further questions or need more assistance.

Rate this post

5 of 5 based on 2951 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us