Pos

Laravel PHP Docker

Environment

  • Ubuntu 22.04 LTS
  • Docker 20.10.12

Install docker

1
2
sudo apt install docker.io
sudo apt install docker-compose

Struktur folder

1
2
3
4
5
6
7
8
9
10
11
laravel_docker
│   docker-compose.yaml
│
└───apache
│   │   default.conf
│
└───php
│   │   Dockerfile
│
└───src
    │   [laravel_folder_projek]

Bina folder

1
2
3
4
5
6
7
8
mkdir laravel_docker
cd laravel_docker
touch docker-compose.yaml
mkdir apache
touch apache/default.conf
mkdir php
touch php/Dockerfile
mkdir src

Kemaskini file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# docker-compose.yaml

version: '3.8'

services:

  laravel:
    container_name: laravel
    image: laravel/zarif:v1
    ports:
        - '80:80'
    volumes:
      - ./src:/var/www/laravel_docker # add laravel php project in this folder
      - ./apache/default.conf:/etc/apache2/sites-enabled/000-default.conf
    depends_on:
      - database
    networks:
      laravel-back-network:

  database:
    image: mysql:latest
    container_name: laravel_database
    volumes:
      - laravel-db:/var/lib/mysql
    restart: unless-stopped
    environment:
      - MYSQL_RANDOM_ROOT_PASSWORD=yes
      - MYSQL_PASSWORD=laravel
      - MYSQL_USER=laravel
      - MYSQL_DATABASE=laravel
    networks:
      laravel-back-network:

volumes:
  laravel-db: 

networks:
  laravel-back-network:
    driver: bridge
1
2
3
4
5
6
7
8
9
10
11
12
# apache/default.conf

<VirtualHost *:80>
   ServerName laravel_docker
   DocumentRoot /var/www/laravel_docker/public

   <Directory /var/www/laravel_docker>
       AllowOverride All
   </Directory>
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# php/Dockerfile

FROM php:8.0-apache

RUN apt update && apt install -y \
    g++ \
    libicu-dev \
    libpq-dev \
    libzip-dev \
    zip \
    zlib1g-dev \
    nano \
    mariadb-client

RUN docker-php-ext-install \
    intl \
    opcache \
    pdo \
    pdo_mysql \
    pgsql 

WORKDIR /var/www/laravel_docker

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

RUN a2enmod rewrite

Copy Laravel PHP projek ke dalam folder src

Kita akan gunakan projek silsilah daripada github

1
git clone https://github.com/nafiesl/silsilah.git src/

Build docker image

1
sudo docker build -t laravel/zarif:v1 ./php

Run docker-compose

1
sudo docker-compose up -d

Akses container terminal

1
sudo docker exec -it laravel bash

Baiki permission container

1
chown www-data:www-data -R /var/www/laravel_docker

Kemaskini .env

1
2
3
cp .env.example .env
php artisan key:generate
nano .env

Pastikan DB enviroment sama dengan docker-compose.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
SYSTEM_ADMIN_EMAILS=

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=laravel

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

LEAFLET_MAP_ZOOM_LEVEL=4
LEAFLET_MAP_DETAIL_ZOOM_LEVEL=18
LEAFLET_MAP_CENTER_LATITUDE="-0.87887"
LEAFLET_MAP_CENTER_LONGITUDE="117.4863"

Kemaskini database

1
2
php artisan migrate
php artisan storage:link

Layari website

Jika tiada masalah, anda boleh melayari website di http://localhost:80

Anda juga boleh dapatkan salinan code di atas disini.

Pos ini dilesenkan di bawah CC BY 4.0 oleh penulis.