How to Setup Docker for Laravel

Dockerize your laravel application using laradock.

Surbhi Vyas , 16 August, 2019

Changes are, and will continue to be, a necessary part of the development of any project. As an IT engineer or developer, Have you ever spent lots of time to setup your project after updating your system or dependencies or libraries contain within it ? In these cases switching between dependencies or projects can be very time consuming. To avoid these kind of problems we have some tools that can help us and docker is one of them.

Here I will show you how you can set up Docker for Laravel(Laradock) and what are the common problem you may face. But before I explain this, let me first define what Docker actually do.

Docker:

Docker is a software which allows us to run applications in different environments called containers.With Docker, it will be very easy to set up a virtual environment without dependencies from your system, this way projects can run on any given system or OS.

Laradock:

If you are using docker for PHP environment like Laravel, thenLaradock will be great for development as well as production environnement .You need to dockerize your laravel application using laradock.

Prerequisites

Before you start, you must have:

  1. Docker installed.
  2. Docker Compose installed.
  3. Laravel installed.

How To Setup Laradock for laravel project:

Step 1- Clone laradock in your laravel project by this command:
$ git clone https://github.com/laradock/laradock.git

After cloning Laradock, you will end up with a structure : your-project/laradock

Step 2- If you want to modify environmental settings then get into your laradock directory and copy the .env.example to .env. $ cp env-example .env

Step 3- First time when you run docker-compose up, it will download all of the necessary Docker images.
$ sudo docker-compose up

Step 4 - You need to modify laravel.conf.example file located in laradock/nginx/sites folder. In this file change the server_name and project’s root path. Example-



Step 5- Now you need to run all the services by this command:
$ sudo docker-compose up -d mysql phpmyadmin nginx

Step 6- If you want to build custom images for your application you can change docker-compose.yml file, now you can build custom image for your application.

Step 7- To enter into the workspace container, go up to laradock directory from terminal and run command
$ sudo docker-compose up -d mysql phpmyadmin nginx

Step 8- If you want to stop using all this docker services then run this command
$ sudo docker-compose down

Step 9- You can connect to the phpmyadmin by http://your-project.com:8080/

Common problems you may face:

Port already in use error :

When you up docker services you may get error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use. Here are two solutions.

First is, you can stop your local HTTP Server eg: $ sudo /etc/init.d/apache2 stop
Second is, you can change the default port in the docker-compose.yml


If you cannot connect to Phpmyadmin or getting mysql connection error :

Stop all the docker services by this command: $ docker-compose down
Edit laradock/.env file set MYSQL_VERSION=5.7 or MYSQL_VERSION=latest

If it not works, then follow these steps:

  1. Delete mysql database $ rm -rf ~/.laradock/data/mysql
  2. Rebuild mysql image $ docker-compose build mysql
  3. Again up all services $ docker-compose up -d nginx mysql phpmyadmin


Overall, this is how Docker (Laradock) works. LaraDock supports more services than just mysql,phpmyadmin and nginx. Check out the documentation of Laradock and Docker for more supported services. Give it a try, and let us know how we can help you. :-)

blog comments powered by Disqus