Exploring Seamless Development and Deployment: Laravel, Docker, and CI Part II


Exploring Seamless Development and Deployment: Laravel, Docker, and CI Part II

Hey there, fellow developers! Today marks a new adventure for me as I dive into the world of blogging. While I am a newbie to the blogging world, my passion for coding and curiosity for exploring new technologies have brought me here, eager to share what I’ve learned.

Why Docker? Is it because it’s popular and a buzzword? No, we need a concrete reason to utilize it. In brief, the reason for adopting Docker is to make our app consistent, portable, and scalable. Consistent by having it work in a variety of environments, regardless of the server. Portable by allowing our program to easily ship to a server without the requirement for daunting server configuration. Scalable by making our application scale horizontally by simply spinning up docker instances of containers.

Feeling overwhelmed? No worries, as you get your hands dirty, those reasonings will make more sense to you.

Well, buckle up because we’re about to dive into the world of Laravel, Docker, and CI!

Assuming that that you’re already familiar with Laravel and Docker, you know they’re both game-changers in their own right. But have you ever thought about combining them? That’s where the magic really happens. And when you throw CI into the mix, you’ve got yourself a recipe for success.

In this guide, we’re going to take a hands-on approach, skipping the basics and diving straight into the stuff. To be specific We are going to cover the development part in this guide, in the upcoming part we are going to cover the continuous integration (CI) part.

First thing is first, create a fresh Laravel project using:

composer create-project laravel/laravel laravel-with-docker

Then download docker from https://www.docker.com/products/docker-desktop/

Now we are all good to start cooking.

From your root folder create Docker/Laravel Folder and create a new file named Dockerfile without any extension and put the below code.

FROM php:latest

WORKDIR /var/www/laravel-with-docker

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

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

COPY . /var/www

RUN composer install

RUN chown -R www-data:www-data /var/www/laravel-with-docker \
 && chmod -R 755 /var/www/laravel-with-docker/storage /var/www/laravel-with-docker/bootstrap/cache

EXPOSE 9000

CMD ["php-fpm"]
The docker-compose.yml file (place this file in your root project file)

version: '3'

services:
app:
build:
context: .
dockerfile: ./Docker/Laravel/Dockerfile

    volumes:
      - .:/var/www/laravel-with-docker
    ports:
      - "9000:9000"

db:
image: mysql:latest
ports: - "3306:3306"
environment:
MYSQL_USER: YOUR_USER
MYSQL_ROOT_PASSWORD: YOUR_PASSWORD

    volumes:
      - db_data:/var/lib/mysql

nginx:
image: nginx:alpine
ports: - "80:80" - "443:443"
depends_on: - app
volumes: - ./:/var/www/laravel-with-docker - ./nginx.conf:/etc/nginx/conf.d/default.conf

volumes:
db_data:
Note: As stated in the attachment above, make sure to include db_data inside volumes unless your database will be deleted upon stopping, restarting the container lifecycle.

Now we left with the nginx configuration, create a file named nginx.conf in the root folder like you did for the docker compose file.

server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/laravel-with-docker/public;
location ~ \.php$ {
try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
Now we are all good it is time to test.

head to your terminal and run the command docker compose up.

then head to your browser and hit localhost your are going to see the default Laravel home page if all good.

.
When you run docker ps you are going to see 3 services running; your Laravel app, the nginx and mysql.

Wait wait how are we going to run those bunch of php commands(php artisan optimize, php artisan migrate 😳….)?

just run the command 😊 docker compose exec app /bin/bash

You need more?

run the command docker compose exec db /bin/bash and access your database.

A common error you may run into is a database connection issue. To fix this, create a new user account and password for your database or use the root password you provided in the Docker compose file with your Laravel.env file.

🥳Hooray that’s it for part I, in the upcoming part we will add more CI concepts; stay tuned until then.
 


Reviews

Post Comment