loading...
Cover image for I tried using CakePHP 4.0.8 🍓with Docker. (apach)

I tried using CakePHP 4.0.8 🍓with Docker. (apach)

sachikokame profile image sachiko ・4 min read

Introduction

☀️日本語バージョン

☀️github

I'm going to write the procedure I'm writing on my_github here.

Thank you.

Step1 Up to the necessary container creation!

clone
$ git clone https://github.com/sachiko-kame/docker_cakePHP.git

Clone and use the following command where docker-compose.yml file exists
$ docker-compose up --build -d

Check if the container was created successfully
$ docker ps

output👁

d6e752c1a5b7        2020_5_30_template_cakephp_apach   "docker-php-entrypoi…"   4 seconds ago       Up 4 seconds        0.0.0.0:80->80/tcp                  sample-apache-container1
602a4868b08b        2020_5_30_template_cakephp_mysql   "docker-entrypoint.s…"   4 seconds ago       Up 4 seconds        0.0.0.0:3306->3306/tcp, 33060/tcp   sample-mysql1

ip check
$ docker-machine ip default

output👁

***.***.**.***

Confirm the pasted ip on your browser.
Alt Text

Step2 Enter apache container and install cakePHP

Enter the Apache container. The following command. d6e752c1a5b7, please put your own
$ docker exec -i -t d6e752c1a5b7 bash

apache in🚪
root@d6e752c1a5b7:/var/www/html#

put composer
$ curl -sS https://getcomposer.org/installer | php

put cakePHP in composer
$ php composer.phar create-project --prefer-dist cakephp/app:4.* my_app_name

Set Folder Permissions ? (Default to Y) [Y,n]? → y → enter!

Move the contents of the cakePHP folder
$ mv my_app_name/* /var/www/html/

output👁

root@d6e752c1a5b7:/var/www/html# ls
README.md  bin  composer.json  composer.lock  composer.phar  config  index.php  logs  my_app_name  phpcs.xml  phpunit.xml.dist  plugins  resources  src  templates  tests  tmp  vendor  webroot

If you reload the browser and it looks like the following, it's OK !!
Alt Text

It is OK to reload again as below !!
Alt Text

Exit the container
$ exit

apache out🚪

Setp3 Resolving any errors

The reason is a problem with file permissions, so
The following command.
$ chmod -R 777 apach/html/logs
$ chmod -R 777 apach/html/tmp

OK if you reload the browser and the error disappears

Step4 Create a database in Mysql container and complete the cakePHP database connection.

Enter the mysql container. 602a4868b08b, please put your own
$ docker exec -i -t 602a4868b08b bash

mysql in🚪
root@602a4868b08b:/#

Login to mysql with mysql container
$ mysql -u root -p

If you are asked a password, you can use example
By the way, the password will not be displayed even if you type it!
output👁

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

Create a database for this time in the mysql container

$ create database mydb01;
$ show databases; ← Just a confirmation command

exit from mysql container
You can get out by hitting $ exit; twice!

mysql out🚪

Modify the cakePHP file to connect to the database. Change the Datasources of "/apach/html/config/app_local.php" as follows.

    'Datasources' => [
        'default' => [
          'className' => 'Cake\Database\Connection',
          'driver' => 'Cake\Database\Driver\Mysql',
          'persistent' => false,
          'encoding' => 'utf8mb4',
          'timezone' => 'UTC',
          'cacheMetadata' => true,

          'host' => 'mysql',
            /*
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 'non_standard_port_number',

            'username' => 'root',
            'password' => 'example',

            'database' => 'mydb01',
            /**
             * If not using the default 'public' schema with the PostgreSQL driver
             * set it here.
             */
            //'schema' => 'myapp',

            /**
             * You can use a DSN string to set the entire configuration
             */
            'url' => env('DATABASE_URL', null),
        ],

If possible, I think that the database connection can be made as follows.
Alt Text

Step5 Set up around apache so that cakePHP can be rewritten

apache in🚪
Enter the apache container
$ docker exec -i -t d6e752c1a5b7 bash

Command depression to activate the rewrite module
$ a2enmod rewrite
$ service apache2 restart

apache out (auto)🚪
Build Docker again
$ docker-compose up --build -d

If there is a file that has not been moved as follows, move it
Alt Text

It is OK if you hit it in the form of ip address / pages and return to the ip address!

The my_app_name folder is empty so delete it!

Step6 Create a little page and check the display.

Move to /apach/html and create a controller for user with the following command.
$ bin/cake bake controller users

Modify the following files created by the command
/apach/html/src/Controller/UsersController.php
Comment out in index () as follows

    public function index()
    {
        //$users = $this->paginate($this->Users);

        //$this->set(compact('users'));
    }

Then create a users folder in the /apach/html/templates folder. Create index.php file in it
There is no problem if you write something like the following in index.php!
<h1>sample!!!</h1>
Alt Text

Finally

It was my first post on dev.to!✨
Thank you for reading to the end!ヽ(´▽`)/

Posted on by:

sachikokame profile

sachiko

@sachikokame

I'm not good at English, but I will do my best to write it! Thank you. Japanese version dev.to↓ https://qiita.com/sachiko-kame twitter↓ https://twitter.com/854729

Discussion

markdown guide
 

Thank you for your comment! !✨

I used the shape of the cakePHP file to touch on my computer

I would like to try something that can be completed on the container in the future!

 

Hello sachiko, if you don't mind i would suggest you put your cakephp, composer etc. configuration into to your Dockerfile.