DevOps – Start It Up

Almost all the pieces are in place to start up your local development environment. The last piece is an PHP file.

Create a folder called public in the appone folder.

cd ~/Code/appone

mkdir public

Create an index.php file in that folder.

cd public

touch index.php

Add some quick test code the index.php file.

<?php echo 'Hello, World!' ?>

Start up your docker project.

cd ~/Code/appone

docker-compose up -d

It might take a couple minutes because it will have to download the images from Docker Hub. The next time will be a lot faster.

If everything went well, try visiting the website in your browser.

http://localhost

If you see the text “Hello, world!”, then congratulations! Your local development environment setup with Docker was a success.

Here’s everything together.

~/Code/appone/docker-compose.yaml

version: '3.8'
services:

    # Application
    phpapp:
        build:
            context: ./docker
            dockerfile: phpapp.dockerfile
        working_dir: /var/www
        volumes:
            - ./:/var/www

    # Web Server
    webserver:
        build:
            context: ./docker
            dockerfile: webserver.dockerfile
        working_dir: /var/www
        volumes:
            - ./:/var/www
        depends_on:
            - "phpapp"
        ports:
            - 80:80

# Database
database:
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
volumes:
- "./docker/data/db/mysql:/var/lib/mysql"
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
ports:
- 33061:3306

You should also have a .env file in the root of your project.

~/Code/appone/.env

DB_HOST=database
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel-user
DB_PASSWORD=laravel-password
MAIL_HOST=mailhog
MAIL_PORT=1025

Make sure these files exist in the docker folder of your project (~/Code/appone/docker).

~/Code/appone/docker/phpapp.dockerfile

FROM php:8.0-fpm

# Install ImageMagick and MySQL driver
RUN apt-get update && apt-get install -y \
--no-install-recommends \
libmagickwand-dev \
&& pecl install imagick \
&& docker-php-ext-enable imagick \
&& docker-php-ext-install pdo_mysql \
&& apt-get install -y --no-install-recommends zlib1g-dev libzip-dev unzip \
&& docker-php-ext-install zip

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

~/Code/appone/docker/webserver.dockerfile

FROM nginx:1.21

COPY vhost.conf /etc/nginx/conf.d/default.conf

RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log

~/Code/appone/docker/vhost.conf

server {
listen 80;
index index.php index.html;
root /var/www/public;

location / {
try_files $uri /index.php?$args;
}

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass phpapp:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}