AppDividend
Latest Code Tutorials

How To Setup Laravel In Docker Container

Laradock is well-known package in the Laravel community. I will use the Laradock library to show you how to prepare Docker Container and run the laravel application in it. Docker is different from Homestead. Homestead creates a virtual machine while Docker, on the other hand, is pretty much the same as Vagrant; it also gives you the complete development environment but on Virtual Containers instead of Virtual Machines. Since running a virtual Container is much faster than running a full Virtual Machine, LaraDock is quicker than Homestead. Homestead is just a command-line tool that helps you initialize and boot a Virtual Machine via Vagrant, where you SSH into that machine and start coding.

What is Docker Container

Docker is a computer program that performs operating-system-level virtualization, also known as containerization. Docker, Inc had developed it. Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.

A container image is the lightweight, stand-alone, executable package of software that includes everything you needed to run it: code, runtime, system tools, system libraries, settings.

Docker is a tool designed to make it easier to create, deploy, and run applications by using the containers.

Containers allow the developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Docker is an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premises.

Laradock

Laradoc is a full PHP development environment for Docker.

It includes pre-packaged Docker Images, all pre-configured to provide an excellent PHP development environment.

Setup Laravel In Docker Container

First, we will install the Docker on mac, and then we will clone the repository of Laradoc and start configuring it. So let us do that first.

Install and run Docker for Mac.

To download Docker on mac, please use this link Docker Community Edition (CE) to install the stable version of docker. The Docker for Mac install package includes everything you need to run Docker on a Mac. This topic describes pre-install considerations and how to download and install Docker for Mac.

After installation, Double-click Docker.dmg to open the installer, then drag Moby, the whale, to the Applications folder.

Okay, next is double click to that whale icon on the applications, and it will open the installer, then it will start on the right-hand side corner. You can see the Moby whale icon there.

If you have not created your docker id, then you should create one; it is free. Now, verify the docker’s version using the following command.

docker -v

My version is: Docker version 18.03.0-ce, build 0520e24

Quick Note

If you are using Laravel Valet, then please stop the Laravel Valet because otherwise, valet’s nginx and docker’s nginx server will crash at the same port. So before the beginning of the installation process, please close all the nginx and mysql servers. You can stop it using the following command.

valet stop

Other installation Softwares

We will install the different containers. NGINXPHPComposerMySQLRedis and Beanstalkd

Step 1: Install the Laravel in your directory.

You can install the Laravel in your regular project directory using the following command.

composer create-project laravel/laravel doc --prefer-dist

Now, go into the project folder.

cd doc

Open the folder in Visual Studio Code using the following command. You can choose your favorite editor.

code .

Now, we need to change the database settings in the .env file.

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=doc
DB_USERNAME=root
DB_PASSWORD=root

Put your database name. Note that, here, we are using the host as mysql and in the general scenario, we are taking host as localhost or 127.0.0.1. So, just put the above snippet inside the .env file, and you are good to go.

Step 2: Clone the Laradoc repository.

Clone the following repository in the Laravel project. Remember, you have to clone the repo. Inside the Laravel project, and in our case, it is the doc folder.

git clone https://github.com/Laradock/laradock.git

It will create a folder called laradock. Now, your project structure looks like this.

 

Laradoc Tutorial

Go into the laradock folder.

cd laradock

Step 3: Rename the files.

Rename env-example to .env.

cp env-example .env

Step 4: Run your containers.

Type the following command in your terminal.

docker-compose up -d nginx mysql phpmyadmin redis workspace

If it is your first time, then it will take 15-20 minutes to install all the containers one by one and create the docker container environment.

It will start the nginx server, mysql serverphpmyadmin.

If you face any issues related to yarn, then merely install the yarn and rerun the docker-compose command.

You can list your running containers using the following command.

docker ps

Also, you can restart your containers using the following command.

docker restart $(docker ps -q)

You can kill all the containers using the following command.

docker kill $(docker ps -a -q)

You can stop all the containers using the following command.

docker stop $(docker ps -aq)

Now, again restart your container if you have stopped your container.

docker-compose up -d nginx mysql phpmyadmin

Switch to the browser and go to the following URL: http://localhost

laravel docker tutorial

You can see that our Laravel is running on that local server. Yikes!! We are now running the Laravel inside the Docker Container.

Access phpMyAdmin

We can access the phpMyAdmin at 8080 port on the browser: http://localhost:8080.

It will open up the login page of phpmyadmin, and remember, your server is mysql, the username is the root, and the password is the root. 

Create the database, whichever name you have written inside the .env file.

Now, you can access the whole laravel project directory using the command.

docker-compose exec workspace bash

Now, go into that project folder, which is a doc. It seems to be weird because we are already inside the doc >> laradoc. But after the above command, you still need to go doc. It is like we are connected to the remote server, but the reality is that we are on our local project. So it seems like we have created a virtual environment for laravel, but in reality, we have just created small containers. That is why Docker is fast then Homestead. Now, go into the doc directory.

cd doc

Now, type the following command to check the folders in this directory.

ls -la

You can see that the structure is similar to Laravel structure. So we are at the root of our project, now migrate the schema to the database using the following command.

php artisan migrate

If you can migrate it correctly then, all of your configurations are working correctly, and you have installed Laravel successfully on Docker.

If you get any error, please verify your mysql server, database name, username, and password.

 

docker phpmyadmin tutorial

So, we have successfully installed Laravel on Docker. So our How To Setup Laravel In Docker Container Tutorial is over. Thanks for taking it. I hope you like it. Comments are always welcome.

9 Comments
  1. Siripong says

    I have followed up your steps but I am getting the error while trying to login to PhpMyAdmin.
    it gives me this error:
    mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed: Name does not resolve mysqli_real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name does not resolve
    These are the information I filled when I have attempting to login via Phpmyadmin
    Server:mysql
    Username: root
    Password:root

    Can you please help me I am really appreciate for you help 😀

    1. Dimitrios Bantanis-Kapirnas says

      Hello, mysql pulls the latest which means version 8. I solved the same problem by using an older version mysql:5.7.22

      1. Yuki says

        how to change mysql v.8 to mysql v.5.7?

  2. Lykos says

    I’m having a question in regards to step 1 – installing Laravel. Do you run composer create-project laravel/laravel nativelly or when you are in a container (and which)? I haven’t anything (Composer, PHP etc) installed nativelly so I want everything to run through containers

  3. Akhil says

    Thank you for the tutorial.
    You have mentioned BB_Port =3306. But then why is mysql being accessed through 8080? Where is it configured?

  4. Mohammad says

    what is phpmyadmin login details
    server name
    user name
    password

  5. Fajar says

    ERROR: Service ‘workspace’ failed to build: The command ‘/bin/sh -c if [ ${COMPOSER_GLOBAL_INSTALL} = true ]; then composer global install ;fi’ returned a non-zero code: 1

  6. Adam says

    I found this solution on another site which got me entry into my mysql after solving the 500 issue.

    maxkossatz commented
    Had the same issue and this worked! Thanks ?
    downgrade mysql from 8 to 5.7

    by setting MYSQL_VERSION=5.7 in laradock/.env
    after that in your .env file see the var called DATA_PATH_HOST and go to that folder, after that delete the mysql folder there and then do the command

    docker-compose build –no-cache mysql

    and then the command

    docker-compose up -d mysql

  7. Melissa R West says

    Thanks for sharing these tips, Krunal.

    These are some in detailed steps to setup laravel in the docker containers. Laravel aims to make the process of development pleasing for a developer without sacrificing the functionality of an app. A happy Laravel developer makes the best code. Laravel is accessible, but powerful and provides powerful tools for robust, huge apps.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.