Skip to content

oanhnn/docker-laravel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

oanhnn/docker-laravel

Alpine based Laravel image.

Build Status Software License

Features

  • Build from official PHP docker image
  • Installed some PHP extensions
  • Installed XDebug extension but disable by default
  • Add artisan bin alias to php /var/www/artisan
  • Auto create infinite loop when run artisan schedule:run with option --sleep
  • Auto build and push by Github Workflow
  • Tagging follow sermatic version

Tags

Image oanhnn/laravel

  • X.Y.Z - the PATH version (git tag vX.Y.Z)
  • X.Y - the MINOR version
  • X - the MAJOR version
  • latest - the latest version

Image ghcr.io/oanhnn/laravel

  • edge - the edge version, it is newest code from branch master
  • nightly - the nightly version, it is builded daily at 8:20 AM UTC

NOTE: Using sematic version for production

Usage

Use like official PHP docker image

Use like with official PHP image

Extensions

All extensions are installed and enabled:

$ docker run --rm -it oanhnn/laravel:edge php -m
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
fileinfo
filter
ftp
gd
gmp
hash
iconv
imagick
intl
json
libxml
mbstring
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_pgsql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
SimpleXML
sockets
sodium
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

Enable XDebug

Create a xdebug.ini file like below and mount (or copy) to /usr/local/etc/php/conf.d/ directory in container

zend_extension=xdebug.so

[Xdebug]
xdebug.remote_enable=true
xdebug.remote_autostart=false
xdebug.remote_port=9000
xdebug.remote_connect_back=false
xdebug.remote_handler=dbgp
xdebug.idekey=CODE

Config your IDE to work with XDebug.

Run artisan command

$ docker run --rm -it -v $(pwd):/var/www oanhnn/laravel artisan inspire

Run artisan schedule:run with infinite loop

To run artisan schedule:run in infinite loop, you MUST add option --sleep <seconds>. With <seconds> is loop sleep in seconds .

$ docker run --rm -d -v $(pwd):/var/www oanhnn/laravel artisan schedule:run --verbose --sleep 60

NOTE: This feature like command artisan schedule:work in Laravel 8+

Work with docker-compose

You can copy all files in example directory to your root directory of project and run docker-compose up -d.

Run with UID=1000

Default oanhnn/laravel run:

  • PHP-FPM worker with www-data user (UID=82)

    $ docker run --rm -d --name php-fpm oanhnn/laravel:latest
    b634e56c859837d660ed8697b50e92d3f05efe89325e39c803b731c7f846864f
    $ docker exec php-fpm ps
    PID   USER     TIME  COMMAND
        1 root      0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
        6 www-data  0:00 php-fpm: pool www
        7 www-data  0:00 php-fpm: pool www
        8 root      0:00 ps
  • All artisan command via /usr/local/bin/artisan with www-data (by su-exec)

    $ docker-compose exec horizon ps
    PID   USER     TIME  COMMAND
        1 root      0:00 {artisan} /bin/sh /usr/local/bin/artisan horizon
        7 www-data  0:00 php /var/www/artisan horizon
       14 www-data  0:00 /usr/local/bin/php artisan horizon:supervisor bb9e6284106d
       25 www-data  0:00 /usr/local/bin/php artisan horizon:work redis --name=defau
       38 root      0:00 ps
  • Other command with root user

However, you can also set run with other UID. See below example.

FROM oanhnn/laravel:latest

# Create new user with UID=1000 and GID=1000
RUN set -eux; \
    addgroup -g 1000 dev; \
    adduser -u 1000 -D -G dev dev

# Set PHP-FPM user
RUN set -eux; \
    sed -i "s|^user =.*|user = dev|i" /usr/local/etc/php-fpm.d/www.conf; \
    sed -i "s|^group =.*|user = dev|i" /usr/local/etc/php-fpm.d/www.conf; \
    chown dev:dev /var/www

# Set artisan commands execute user
ENV EXEC_USER=dev

Fix permissions when mouting

When mounting file or directory to container, you can have error about file permissions. To fix it, you can run:

$ sudo setfacl -dR -m u:82:rwX -m u:$(whoami):rwX ./
$ sudo setfacl -R  -m u:82:rwX -m u:$(whoami):rwX ./

Or

$ sudo chmod -R g+w         bootstrap/cache storage
$ sudo chown -R $(whoami):82 bootstrap/cache storage

Contributing

All code contributions must go through a pull request and approved by a core developer before being merged. This is to ensure proper review of all the code.

Fork the project, create a feature branch, and send a pull request.

If you would like to help take a look at the list of issues.

Security

If you discover any security related issues, please contact to me instead of using the issue tracker.

License

This project is available under the MIT license.

Contact

Copyright (c) 2021 Oanh Nguyen

@oanhnn @oanhnn