Create Laravel 8 project with Docker on MacOS

Trung Vu
3 min readMay 11, 2021

In this article, let’s create a Laravel 8 project with Docker on MacOS

Install Docker Desktop on Mac

Reference: https://docs.docker.com/docker-for-mac/install/

Check Docker and Docker-Compose version

docker -vdocker-compose -v

Create some files and folders with following structure

- project
— nginx
— — default.conf
— src
— docker-compose.yml
— php.dockerfile
— upload.ini

Configure NGINX for project in project/nginx/default.conf

server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

Define services, networks and volumes in compose file

project/docker-compose.yml

version: '3'networks:
laravel:
services:
site:
image: nginx:stable-alpine
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel

phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
restart: always
environment:
PMA_HOST: mysql
PMA_USER: homestead
PMA_PASSWORD: secret
volumes:
- ./upload.ini:/usr/local/etc/php/php.ini
ports:
- "8888:80"
networks:
- laravel
php:
build:
context: .
dockerfile: php.dockerfile
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
composer:
image: composer:latest
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
networks:
- laravel
npm:
image: node:14.8
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['npm']
artisan:
build:
context: .
dockerfile: php.dockerfile
volumes:
- ./src:/var/www/html
depends_on:
- mysql
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel

Configure PHP version and extensions in Dockerfile

project/php.dockerfile

FROM php:7.4-fpm-alpineWORKDIR /var/www/htmlRUN apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
curl-dev \
imagemagick-dev \
libtool \
libxml2-dev \
postgresql-dev \
sqlite-dev \
&& apk add --no-cache \
curl \
git \
imagemagick \
mysql-client \
postgresql-libs \
libintl \
icu \
icu-dev \
libzip-dev \
libpng \
libpng-dev \
zlib-dev \
&& pecl install imagick \
libonig-dev \
&& apk update \
&& docker-php-ext-enable imagick \
&& docker-php-ext-configure gd \
&& docker-php-ext-install \
bcmath \
curl \
iconv \
pdo \
pdo_mysql \
pdo_pgsql \
pdo_sqlite \
pcntl \
tokenizer \
xml \
zip \
intl \
gd \
&& curl -s https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer \
&& apk del -f .build-deps

Set filesize for upload / import file in project/upload.ini

upload_max_filesize = 128M
post_max_size = 128M

Yup! We are going to build and run this container project by following command

docker-compose up -d --build

This process may take a few minutes in first time.

After finished, we can check version of some services

docker-compose exec php php -vdocker-compose run composer -vdocker-compose run npm -vdocker-compose run mysql -v

Now, we will install Laravel by composer

cd srcdocker-compose run composer create-project laravel/laravel .

Check Laravel version after installed

docker-compose exec php php artisan -V

Ok, go to http://localhost:8080 you will be getting new homepage of Laravel 8

Final step, you need to add database accout in .env file. Notice: replace DB_HOST=127.0.0.1 to DB_HOST=mysql (a service in Docker container)

// ..
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

Happy Coding:)

--

--