/docker-compose-laravel

A docker-compose workflow for local Laravel development

Primary LanguageDockerfileMIT LicenseMIT

Docker-Compose-Laravel-Stack (MySql-main branch, PostgreSQL-LEPP-stack branch)

A pretty simplified Docker Compose workflow that sets up a LEMP network of containers for local Laravel development. You can view the full article that inspired this repo here. The beauty of Docker for local Laravel development

GitNFT

If you want to use this repository as a base for another project

1 - Clone this repository using the final project name. This will create a folder called myNewGitHubRepositoryName.

git clone -b LEPP-stack git@github.com:igoralves1/docker-compose-laravel.git myNewGitHubRepositoryName

2 - Check the what is the remote upstream.

git remote -v

//In this case the repository that we just cloned:
origin	git@github.com:igoralves1/docker-compose-laravel.git (fetch)
origin	git@github.com:igoralves1/docker-compose-laravel.git (push)

3 - Go to myNewGitHubRepositoryName folder and remove the origin link that points to docker-compose-laravel repository just cloned.

git remote remove origin

4 - Check again to confirm that the origin is gone.

git remote -v

//In this case should return nothing

5 - Check your current working branch.

git branch

//In this case should return LEPP-stack - The branch just cloned

6 - Create a main branch. Note: git branch -m main (or the same with -M) only works once you have an initial commit.

git branch -M main

7 - Check if the Main branch was created

git branch

//In this case should return `main`

8 - Go to GitHub and create a rempository named myNewGitHubRepositoryName (same name as your local repository folder)

9 - Go to your myNewGitHubRepositoryName folder (local repository) and add the new remote origin link

git remote add origin git@github.com:igoralves1/myNewGitHubRepositoryName.git

10 - Check again to confirm that the new origin is pointing to the correct repository.

git remote -v

5 - Set user.name and user.email for this repository

git config user.name "igoralves1"
git config user.email "igoralves1@gmail.com"

6 - Now you have a Docker-Compose-Laravel-Stack project pointing to the myNewGitHubRepositoryName github repository. Do the first commit and push.

git remote remove origin
git remote add origin git@github.com:igoralves1/adsaf.git
git branch -M main
git push -u origin main

Usage

To get started, make sure you have Docker installed on your system, and then clone this repository.

How To Install and Use Docker Compose on Ubuntu 22.04

Note: Starting with Docker Compose v2, Docker has migrated towards using the compose CLI plugin command, and away from the original docker-compose as documented in our previous Ubuntu 20.04 version of this tutorial. While the installation differs, in general the actual usage involves dropping the hyphen from docker-compose calls to become docker compose. For full compatibility details, check the official Docker documentation on command compatibility between the new compose and the old docker-compose.

docker-compose ... ==> docker compose ...

Docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

Note: if you need a PostgreSQL database follow these steps in order to checkot to the LEPP-stack branch.

Next, navigate in your terminal to the directory you cloned this, and spin up the containers for the web server by running docker compose up -d --build site.

After that completes, follow the steps from the src/README.md file to get your Laravel project added in (or create a new blank one).

  • Option 1 - Clone a Laravel repository:
      $ cd src/
      $ rm README.md
      $ git clone git@github.com:xxxxxx/laravel-prj.git
    
  • Option 2 - Create a new Laravel project:
      $ cd src/
      $ rm README.md
      $ docker compose run --rm composer create-project laravel/laravel .
    

At this point just go to the browser URL and put: localhost OR localhost:80. You should be able to see the Laravel website.

Bringing up the Docker Compose network with site instead of just using up, ensures that only our site's containers are brought up at the start, instead of all of the command containers as well. The following are built for our web server, with their exposed ports detailed:

  • nginx - :80
  • mysql - :3306
  • php - :9000
  • redis - :6379
  • mailhog - :8025

Three additional containers are included that handle Composer, NPM, and Artisan commands without having to have these platforms installed on your local computer. Use the following command examples from your project root, modifying them to fit your particular use case. From inside docker-compose-laravel/src(master)$ run the following commands:

1 - Generate the key

docker compose run --rm artisan key:generate

2 - Install dependences with composer

docker compose run --rm composer install

3 - Update composer

docker compose run --rm composer update

4 - Run npm

docker compose run --rm npm run dev

5 - Run migrations

docker compose run --rm artisan migrate

6 - To SSH into the MySql container

docker exec -it <mysql container ID>  /bin/bash

Permissions Issues

If you encounter any issues with filesystem permissions while visiting your application or running a container command, try completing one of the sets of steps below.

If you are using your server or local environment as the root user:

  • Bring any container(s) down with docker compose down
  • Rename docker compose.root.yml file to docker compose.root.yml, replacing the previous one
  • Re-build the containers by running docker compose build --no-cache

If you are using your server or local environment as a user that is not root:

  • Bring any container(s) down with docker compose down
  • In your terminal, run export UID=$(id -u) and then export GID=$(id -g)
  • If you see any errors about readonly variables from the above step, you can ignore them and continue
  • Re-build the containers by running docker compose build --no-cache

Then, either bring back up your container network or re-run the command you were trying before, and see if that fixes it.

Persistent MySQL Storage

By default, whenever you bring down the Docker network, your MySQL data will be removed after the containers are destroyed. If you would like to have persistent data that remains after bringing containers down and back up, do the following:

  1. Create a mysql folder in the project root, alongside the nginx and src folders.
  2. Under the mysql service in your docker-compose.yml file, add the following lines:
volumes:
  - ./mysql:/var/lib/mysql

It is available some startup MySql configuration at docker-compose.yml in the mysql session. By default it is created a database called homestead, a user called homestead with the password secret. The MySql root password is also created with a password secret. To get SSH access to the MySql container docker exec -it <mysql container ID> /bin/bash

If having SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE') just replace DB_HOST=127.0.0,1 or DB_HOST=0.0.0.0 to DB_HOST=mysql. This is the final configuration in .env:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

Issues with Mysql connection refer to https://github.com/aschmelyun/docker-compose-laravel/issues/48 or https://github.com/aschmelyun/docker-compose-laravel/issues/70

Note: using MySql-Workbench this is the configuration:

Hostname: 127.0.0.1
Port: 3306
Username: homestead
Default Schema: homestead
Password: secret

1 - Check Containers

docker ps -a

2 - Stop all Containers

docker stop $(docker ps -a -q)

3 - Remove all Containers

docker rm $(docker ps -a -q)

4 - Check Containers

docker ps -a

5 - Check Images

docker images

6 - Remove all Images

docker rmi -f $(docker images -a -q)

7 - Check Images

docker images

8 - Build the site

docker compose up -d --build site

9 - Remove all containers at once

docker ps -a && docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q) && docker ps -a && docker images && docker rmi -f $(docker images -a -q) && docker images

How to pull or create a Laravel project

  1. Clone your project OR Create a new project OR Copy all of the files directly into this src/ directory.
  2. Build the Docker service:
docker compose up -d --build site
  1. In your terminal go to the docker-compose-laravel/src folder:
cd docker-compose-laravel/src
  1. Remove the README.md:
cd src/ && rm README.md
  1. Create a new Laravel project
docker compose run --rm composer create-project laravel/laravel .

Example: ~/vhosts/docker-compose-laravel/src(master)$ docker compose run --rm composer create-project laravel/laravel .

Using BrowserSync with Laravel Mix

If you want to enable the hot-reloading that comes with Laravel Mix's BrowserSync option, you'll have to follow a few small steps. First, ensure that you're using the updated docker-compose.yml with the :3000 and :3001 ports open on the npm service. Then, add the following to the end of your Laravel project's webpack.mix.js file:

.browserSync({
    proxy: 'site',
    open: false,
    port: 3000,
});

From your terminal window at the project root, run the following command to start watching for changes with the npm container and its mapped ports:

docker compose run --rm --service-ports npm run watch

That should keep a small info pane open in your terminal (which you can exit with Ctrl + C). Visiting localhost:3000 in your browser should then load up your Laravel application with BrowserSync enabled and hot-reloading active.

MailHog

The current version of Laravel (8 as of today) uses MailHog as the default application for testing email sending and general SMTP work during local development. Using the provided Docker Hub image, getting an instance set up and ready is simple and straight-forward. The service is included in the docker-compose.yml file, and spins up alongside the webserver and database services.

To see the dashboard and view any emails coming through the system, visit localhost:8025 after running docker compose up -d site.

  • Look at the local branches in your repository after clone this repository:

      $ git branch
      * master
    
  • But there are other branches hiding in your repository! See these using the -a flag:

      $ git branch -a
      * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/LEPP-stack
      remotes/origin/master
    
  • To take a quick peek at an upstream branch, check it out directly:

      $ git checkout origin/LEPP-stack
    
  • To work on that branch, create a local tracking branch, which is done automatically by:

      $ git checkout LEPP-stack
    
      Branch experimental set up to track remote branch experimental from origin.
      Switched to a new branch 'experimental'
    
  • Here, "new branch" simply means that the branch is taken from the index and created locally for you. As the previous line tells you, the branch is being set up to track the remote branch, which usually means the origin/branch_name branch.

    Your local branches should now show:

      $ git branch
      * LEPP-stack
        master
    
  • You can track more than one remote repository using git remote:

      $ git remote add win32 git://example.com/users/joe/myproject-win32-port
      $ git branch -a
      * master
        remotes/origin/HEAD
        remotes/origin/master
        remotes/origin/v1.0-stable
        remotes/origin/experimental
        remotes/win32/master
        remotes/win32/new-widgets
    

References