Next, use Docker's composer image to mount the directories that you will need for your Laravel project and avoid the overhead of installing Composer globally:
$ docker run --rm -v $(pwd):/app composer install
As a final step, set permissions on the project directory(I dont expererience this issue before you keep moving along check the permission) so that it is owned by your non-root user, move one level up:
cd ../
$ sudo chown -R $USER:$USER docker-laravel
Now that you have defined all of your services in your docker-compose file and created the configuration files for these services, you can start the containers. As a final step, though, we will make a copy of the .env.example file that Laravel includes by default and name the copy .env, which is the file Laravel expects to define its environment:
$ cp .env.example .env
We will configure the specific details of our setup in this file once we have started the containers.
With all of your services defined in your docker-compose file, you just need to issue a single command to start all of the containers, create the volumes, and set up and connect the networks:
$ docker-compose up -d
When you run docker-compose up for the first time, it will download all of the necessary Docker images, which might take a while. Once the images are downloaded and stored in your local machine, Compose will create your containers. The -d flag daemonizes the process, running your containers in the background.
Once the process is complete, use the following command to list all of the running containers:
$ docker ps
You will see the following output with details about your app
, webserver
, and db
containers:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc8f26d43109 digitalocean.com/php "docker-php-entrypoi…" 14 seconds ago Up 13 seconds 9000/tcp app
481a60d5e1f2 mariadb:latest "docker-entrypoint.s…" 14 seconds ago Up 13 seconds 0.0.0.0:3306->3306/tcp db
1bef4a1e8330 nginx:alpine "nginx -g 'daemon of…" 14 seconds ago Up 13 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp webserver
The CONTAINER ID in this output is a unique identifier for each container, while NAMES lists the service name associated with each. You can use both of these identifiers to access the containers. IMAGE defines the image name for each container, while STATUS provides information about the container's state: whether it's running, restarting, or stopped.
You can now modify the .env file on the app container to include specific details about your setup.
Open the file using docker-compose exec, which allows you to run specific commands in containers. In this case, you are opening the file for editing:
$ docker-compose exec web nano .env
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laraveluser
DB_PASSWORD=secret
Next, set the application key for the Laravel application with the php artisan key:generate command. This command will generate a key and copy it to your .env file, ensuring that your user sessions and encrypted data remain secure:
$ docker-compose exec web php artisan key:generate
You now have the environment settings required to run your application. To cache these settings into a file, which will boost your application's load speed, run:
$ docker-compose exec web php artisan config:cache
run the yarn in the disposable container
$ docker run --rm -it -v $(pwd):/app -w /app node yarn
$ docker run --rm -it -v $(pwd):/app -w /app node yarn dev
Your configuration settings will be loaded into /var/www/bootstrap/cache/config.php on the container.
As a final step, visit http://localhost
in the browser. You will see the following home page for your Laravel application:
With your containers running and your configuration information in place, you can move on to configuring your user information for the laravel database on the db container.
With your containers running and your configuration information in place, you can move on to configuring your user information for the laravel database
on the db container.
To create a new user, execute an interactive bash shell on the db container with docker-compose
exec:
$ docker-compose exec db bash
Inside the container, log into the MySQL root administrative account:
$ root@481a60d5e1f2:/# mysql -u root -p
Start by checking for the database called laravel, which you defined in your docker-compose file. Run the show databases command to check for existing databases:
$ MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| laravel |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
Next, create the user account that will be allowed to access this database. Our username will be laraveluser
as well, though you can replace this with another name if you'd prefer. Just be sure that your username and password here match the details you set in your .env
file in the previous step:
MariaDB [(none)]># GRANT ALL ON laravel.* TO 'laraveluser'@'%' IDENTIFIED BY 'secret';
Flush the privileges to notify the MySQL server of the changes:
MariaDB [(none)]># FLUSH PRIVILEGES;
Exit the DB
MariaDB [(none)]># EXIT;
Exit the container
root@481a60d5e1f2:/# exit
Step 10 — Migrating Data and Working with the Tinker Console
First, test the connection to MySQL by running the Laravel artisan migrate command, which creates a migrations table in the database from inside the container:
$ docker-compose exec web php artisan migrate
This command will migrate the default Laravel tables. The output confirming the migration will look like this:
#Output
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Once the migration is complete, you can run a query to check if you are properly connected to the database using the tinker command:
$ docker-compose exec web php artisan tinker
#Psy Shell v0.9.9 (PHP 7.2.14 — cli) by Justin Hileman
>>> \DB::table('migrations')->get();
You will see output that looks like this:
=> Illuminate\Support\Collection {#2899
all: [
{#2905
+"id": 1,
+"migration": "2014_10_12_000000_create_users_table",
+"batch": 1,
},
{#2908
+"id": 2,
+"migration": "2014_10_12_100000_create_password_resets_table",
+"batch": 1,
},
],
}
>>>
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as:
- Simple, fast routing engine.
- Powerful dependency injection container.
- Multiple back-ends for session and cache storage.
- Expressive, intuitive database ORM.
- Database agnostic schema migrations.
- Robust background job processing.
- Real-time event broadcasting.
Laravel is accessible, yet powerful, providing tools needed for large, robust applications.
Laravel has the most extensive and thorough documentation and video tutorial library of any modern web application framework, making it a breeze to get started learning the framework.
If you're not in the mood to read, Laracasts contains over 1100 video tutorials on a range of topics including Laravel, modern PHP, unit testing, JavaScript, and more. Boost the skill level of yourself and your entire team by digging into our comprehensive video library.
We would like to extend our thanks to the following sponsors for helping fund on-going Laravel development. If you are interested in becoming a sponsor, please visit the Laravel Patreon page:
- Vehikl
- Tighten Co.
- Kirschbaum Development Group
- 64 Robots
- Cubet Techno Labs
- Cyber-Duck
- British Software Development
- Webdock, Fast VPS Hosting
- DevSquad
- UserInsights
- Fragrantica
- SOFTonSOFA
- User10
- Soumettre.fr
- CodeBrisk
- 1Forge
- TECPRESSO
- Runtime Converter
- WebL'Agence
- Invoice Ninja
- iMi digital
- Earthlink
- Steadfast Collective
- We Are The Robots Inc.
- Understand.io
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the Laravel documentation.
If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [email protected]. All security vulnerabilities will be promptly addressed.
The Laravel framework is open-sourced software licensed under the MIT license.