The story that docker had a hard time

This teaching material

Learn Test Driven Development with Laravel!

Get hooked on the swamp with Laravel's Migrate before tackling the challenge

Reset Uninstall docker desktop Start-> Settings-> App (usually disappears) image.png When I try to turn it off normally with explorer, a blue screen appears and it restarts, and I can't turn it off forever (I've never seen a blue screen in this era ...). It seems that there is no choice but to snap it off in powershell administrator mode image.png

learning-laravel-Based on a directory called tdd


PS C:\WINDOWS\system32> cd D:\OneDrive\dev
PS D:\OneDrive\dev> rm -Force learning-laravel-tdd

Install Docker Desktop for Windows

https://hub.docker.com/editions/community/docker-ce-desktop-windows/ The installation itself goes smoothly

Clone git repository for Docker

OneDrive\dev>


$ mkdir learning-laravel-tdd
$ cd learning-laravel-tdd
$ git clone https://github.com/nunulk/learning-laravel-tdd-docker docker
$ mkdir app

Current state

learning-laravel-tdd
├── app
└── docker

Do it after starting Docker


$ cd docker
$ cp .env.example .env
$ docker-compose up -d 

Resolve Port fog (DB_PORT to 3308)

In my case, I found that 3306 was on (because I usually use it in python) image.png

learning-laravel-tdd\docker\.env


COMPOSE_PROJECT_NAME=learning-laravel-tdd
APP_PATH=../app
TZ=Asia/Tokyo
WEB_PORT=80
- DB_PORT=3306
+ DB_PORT=3308
DB_DATABASE=learning_laravel_tdd
DB_USERNAME=root
DB_PASSWORD=root
DB_TESTING_DATABASE=learning_laravel_tdd_testing
DB_TESTING_PORT=3307
DB_TESTING_USERNAME=root
DB_TESTING_PASSWORD=root

Then, when you start with Docker's control panel, everything will be turned on image.png

Operation check

Execute the following command to check if all containers are running

learning-laravel-tdd\docker>


$ docker-compose ps
              Name                             Command              State                 Ports
-------------------------------------------------------------------------------------------------------------
learning-laravel-tdd_app_1          docker-php-entrypoint php-fpm   Up      9000/tcp
learning-laravel-tdd_db-testing_1   docker-entrypoint.sh mysqld     Up      0.0.0.0:3307->3306/tcp, 33060/tcp
learning-laravel-tdd_db_1           docker-entrypoint.sh mysqld     Up      0.0.0.0:3308->3306/tcp, 33060/tcp
learning-laravel-tdd_web_1          nginx -g daemon off;            Up      0.0.0.0:80->80/tcp

Application initialization

learning-laravel-tdd\docker>


$ docker exec -it learning-laravel-tdd_app_1 ash
# composer create-project --prefer-dist "laravel/laravel=7.*" .

learning-laravel-tdd\app\.env


#Omission

LOG_CHANNEL=stack


- DB_CONNECTION=mysql
- DB_HOST=127.0.0.1
- DB_PORT=3306
- DB_DATABASE=laravel
- DB_USERNAME=root
- DB_PASSWORD=
+ DB_CONNECTION=mysql
+ DB_HOST=db
+ DB_PORT=3306
+ DB_DATABASE=learning_laravel_tdd
+ DB_USERNAME=root
+ DB_PASSWORD=root
+ DB_TESTING_HOST=db-testing
+ DB_TESTING_PORT=3306
+ DB_TESTING_DATABASE=learning_laravel_tdd_testing
+ DB_TESTING_USERNAME=root
+ DB_TESTING_PASSWORD=root

BROADCAST_DRIVER=log

#Omission

learning-laravel-tdd\app\config\database.php


        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
-           'port' => env('DB_PORT', '3306'),
+           'port' => env('DB_PORT', '3308'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],


+       'mysql_testing' => [
+           'driver' => 'mysql',
+           'host' => env('DB_TESTING_HOST', '127.0.0.1'),
+           'port' => env('DB_TESTING_PORT', '3307'),
+           'database' => env('DB_TESTING_DATABASE', 'forge'),
+           'username' => env('DB_TESTING_USERNAME', 'forge'),
+           'password' => env('DB_TESTING_PASSWORD', ''),
+           'unix_socket' => env('DB_SOCKET', ''),
+           'charset' => 'utf8mb4',
+           'collation' => 'utf8mb4_unicode_ci',
+           'prefix' => '',
+           'prefix_indexes' => true,
+           'strict' => true,
+           'engine' => null,
+           'options' => extension_loaded('pdo_mysql') ? array_filter([
+               PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
+           ]) : [],
+        ],
 
         'pgsql' => [
             'driver' => 'pgsql',
             'url' => env('DATABASE_URL'),

learning-laravel-tdd\app\phpunit.xml


        <server name="CACHE_DRIVER" value="array"/>
-       <!-- <server name="DB_CONNECTION" value="sqlite"/> -->
-       <!-- <server name="DB_DATABASE" value=":memory:"/> -->
+       <server name="DB_CONNECTION" value="mysql_testing"/>
+       <server name="DB_DATABASE" value="learning_laravel_tdd_testing"/>       
        <server name="MAIL_MAILER" value="array"/>

Creating a Models directory and moving User.php

By default, Laravel will generate the Model class directly under the app directory when you generate it with the artisan command, but in this teaching material, we will create a Models directory and place it there. Move User.php, which is automatically created by the installation, to the Models directory in advance.

First, execute the following command to create a directory (either the container side or the host side is fine).

learning-laravel-tdd\app


# mkdir app/Models

Then move app/User.php under app/Models and change the namespace.

learning-laravel-tdd\app\Models\User.php


namespace App\Models;

Also change the namespace of the part that refers to the User class.

The following two files are to be changed.

learning-laravel-tdd\app\config\auth.php


'providers' => [
    'users' => [
        'driver' => 'eloquent',
-       'model' => App\User::class,
+       'model' => App\Models\User::class,
    ],

learning-laravel-tdd\app\database\factories\UserFactory.php


- use App\User;
+ use App\Models\User;

config is ignored Make learning-laravel-tdd \ docker \ db \ my.cnf read-only It seems to be a problem only for Windows

mysqld: [Warning] World-writable config file'/etc/mysql/my.cnf' is ignored. [Docker + Windows] What to do if the docker image of mysql does not start due to a mount error of my.cnf

Checking the application

Finally, run the following command inside the container to see if the application works.

/app # php artisan migrate

Recommended Posts

The story that docker had a hard time
[Solution] A memo that I had a hard time because the format of sinatra-validation changed
A story that I had a hard time trying to build PHP 7.4 on GCE's CentOS 8
A story that took time to establish a connection
A story about making a Builder that inherits the Builder
Android: I had a hard time displaying the HTML file on the SD card
A command that definitely cleans the local docker environment
[Docker] The story that an error occurred in docker-compose up
left4dead2 I made a Docker image for the server and tried running it on GCE # 3 (I had a hard time building the server)
A story that I realized that I had to study as an engineer in the first place
[Docker] Is it good enough to call it a multi-stage build? → The story that became so good
A story about having a hard time aligning a testing framework with Java 6
A story that struggled with the introduction of Web Apple Pay
The story that Tomcat suffered from a timeout error in Eclipse
A story that confirmed the profile of Yasuko Sawaguchi 36 years ago
A story that a Ruby beginner made and released a LINE BOT that tells the train time in 2 months
The story of updating SonarQube's Docker Container
[Small story] Misleading method name (a story that wasted time due to setScale.
A story that was embarrassing to give anison file to the production environment
I had a hard time installing MariaDB 10.5.5 on CentOS 8 of Sakura VPS
I had a hard time doing Java multithreading from scratch, so organize it
A story about a super beginner participating in the AtCoder contest for the first time (AtCoder Beginner Contest 140)
I tried using Docker for the first time
A shell script that builds the OpenJDK11 source
A story about the JDK in the Java 11 era
A story that separates business logic and model
Set the time of LocalDateTime to a specific time
The story that link_to is deep (cause unknown)
I tried touching Docker for the first time
A description that only the poster can access
About the daylight saving time that really happened
A story that failed using "bundle exec rubocop -a"
I tried to make an app that allows you to post and chat by genre ~ Where I had a hard time ~
A story that got stuck with an error during migration in docker PHP laravel
Create a docker image that runs a simple Java app
A small story that is sometimes useful in Maven
The story of making a reverse proxy with ProxyServlet
A reference book that my brother had, had, had Ruby, Java
A story about trying hard to decompile JAR files
A story about introducing Evolutions into the Play Framework
Make the strongest Laravel development environment (Docker) Japan time
Command to try using Docker for the time being
Creating a lightweight Java environment that runs on Docker
The story that the forced update could not be implemented
The story that .java is also built in Unity 2018
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
The story of migrating a stray batch without an owner from EC2 to a Docker environment
A story that deepened the understanding of devise's methods user_signed_in? And current_user through error resolution
A story that ended up taking a break when using the Linked List with a light feeling