Skip to content

simple docker build including php, nginx, mariadb, redis and mailhog

Notifications You must be signed in to change notification settings

murcoder/laravel-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

laravel-docker

A simple docker environment for a laravel or a simple php application.

This configuration is using the following images from DockerHub:

  • PHP: php:8.0-fpm (including xDebug)
  • Webserver: nginx:latest
  • Database: mariadb:10.5.8
  • Cache: redis
  • Email: mailhog

You can customize all the versions and packages in the docker-compose.yaml file, or in the /images/.

Install

  1. Install docker

  2. Clone laravel-docker into your project directory:

    git clone https://github.com/murcoder/laravel-docker.git
    
  3. Domain: Add the DNS to your /hosts file

    127.0.0.1 dev.my-application.com
    
  4. SSL: Create and register a self signed SSL certificate (see chapter SSL below for more informations)

  5. Start: Build and start the docker containers in your docker directory

    docker-compose up -d
    
  6. DONE! open https://dev.my-application.com/



Useful commands

# create and start containers without logging
docker-compose up -d
 
# create and start containers with log
docker-compose up
 
# stop and remove containers, networks, images, and volumes
docker-compose down
 
# step into the php container
docker-compose exec app bash
 
# step into the mysql container
docker-compose exec mysql bash
 
# list containers
docker-compose ps
 
# remove unused data
docker system prune

DB

jdbc:mariadb://localhost:8001/my_app_db

HOST=db
PORT=3306
DATABASE=my_app_db
USERNAME=root
PASSWORD=root

Laravel

Add this DB configuration in your .env file:

DB_CONNECTION=mysql #defined in /app/config/database.php
DB_HOST=db
DB_PORT=3306
DB_DATABASE=my_app_db
DB_USERNAME=root
DB_PASSWORD=root

SSL

Create a self-signed certificate for using https on localhost.

Create Certificate Authority

To sign your own certificate, you need your own certification authority.

  1. Create private key and set password (needed later)

    openssl genrsa -des3 -out myCertificationAuthority.key 2048
    
  2. Create root certificate and set common_name=dev.my-application.com (let other fields empty)

    openssl req -x509 -new -nodes -key myCertificationAuthority.key -sha256 -days 825 -out myCertificationAuthority.pem
    

Register in Browser

Chrome: Settings/Security/Certificates verwalten

Firefox: Settings/Security/ZertifikateCertificates/Import

Create Self Signed Key

  1. Create key

    openssl genrsa -out my_application.key 2048
    
  2. Create Certificate Signing Requests (CSR)

    openssl req -new -key my_application.key -out my_application.csr -subj '/CN=myCertificationAuthority/O=myCertificationAuthority/OU=IT' -extensions SAN -reqexts SAN -config <( \
    printf "[SAN]\nsubjectAltName=DNS:dev.my-application.com
    \n[dn]\nCN=myCertificationAuthority\n[req]\ndistinguished_name = dn\n[EXT]\nauthorityKeyIdentifier=keyid,issuer\nbasicConstraints=CA:FALSE\nsubjectAltName=DNS:dev.my-application.com
    \nkeyUsage=digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment\nextendedKeyUsage=serverAuth")
    
  3. Create file my_application.ext and insert the following content

    authorityKeyIdentifier=keyid,issuer
    basicConstraints=CA:FALSE
    keyUsage=digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment
    subjectAltName = @alt_names
    [alt_names]
    DNS.1 = dev.my-application.com
    
  4. Create key by using the self created certificate authority

    openssl x509 -req -in my_application.csr -out my_application.crt -CA myCertificationAuthority.pem -CAkey myCertificationAuthority.key -CAcreateserial \
    -sha256 -days 3650 \
    -extfile my_application.ext
    

Debugging

For debugging xdebug has been already installed in app.dockerfile. To use it in PHPstorm:

  1. Phpstorm Port: Set Port 9003 in Phpstorm/Preferences/PHP/Debug

  2. Phpstorm Path config: Phpstorm/Run/EditConfiguration/PHP Web Page/xdebug.

Browser Plugin Install browser extension and activate:

Chrome: https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc?hl=de Firefox: https://addons.mozilla.org/de/firefox/addon/xdebug-helper-for-firefox/

About

simple docker build including php, nginx, mariadb, redis and mailhog

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published