vivasoft/laravel-docker

If you want to use docker with your laravel project, this package will help you to create the container. It contains most of the require software to run a laravel application; it also provides flexibility to customize your container based on your need.

dev-master 2021-01-11 04:45 UTC

This package is auto-updated.

Last update: 2024-09-11 12:39:53 UTC


README

Vivasoft Ltd

If you want to use docker with your laravel project, this package will help you to create the container. It contains most of the require software to run a laravel application; it also provides flexibility to customize your container based on your need.

Table of contents

Prerequisite

  • Docker Engine >= 17.04.0

Use with new laravel installation

#Installation Process:

Youtube link: https://youtu.be/7qyH4sO3mcs

Step 1:
Open your terminal, navigate to your project directory and run the following command to install the latest version.

docker run --rm --interactive --tty --volume ${PWD}:/app composer create-project --prefer-dist laravel/laravel .

Step 2:
Install vivasoft/laravel-docker package using the following command:

docker run --rm --interactive --tty --volume ${PWD}:/app composer require vivasoft/laravel-docker:dev-master

Step 3:
Run the following command to publish your docker files into your root project directory.

docker run --rm --interactive --tty --volume ${PWD}:/app composer php artisan vivasoft:dockerInstall

A new folder .docker along with two other files docker-compose.yml and Dockerfile should copy to your root installation directory.

NOTE

Before building the docker image you should update your database credential.

For advanced usages you may want to update your nginx configuration.

Step 4:
Run the application:

docker-compose up -d

It may take some time, so grab a cup of ☕ 😬

When done - update your .env file and visit your IP address: http://your_ipaddress:port [default is: 80]

Use with existing project

Step 1:
Install vivasoft/laravel-docker package:

composer require vivasoft/laravel-docker:dev-master

Step 2:
Publish docker components by running the following command:

php artisan vivasoft:dockerInstall

A new folder .docker along with two other files docker-compose.yml and Dockerfile should copy to your root installation directory.

NOTE

Before building the docker image you should update your database credential.

For advanced usages you may want to update your nginx configuration.

Step 3:
Run the application:

docker-compose up -d

When done - update your .env file and visit your IP address: http://your_ipaddress:port [default is: 80]

Update .env file

Open your docker-compose.yml file and use the related value.

Suppose your docker-compose.yml settings:

  #MySQL
  db:
    image: mysql:5.7.28
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: one_database
      MYSQL_ROOT_PASSWORD: root
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql
      - ./.docker/mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

In your .env file you have to update the following value:

The DB_HOST should be the container_name of your #MySQL Container.
The DB_DATABASE should be same as MYSQL_DATABASE.
The DB_PORT should be same as 3306.
The DB_PASSWORD should be same as MYSQL_ROOT_PASSWORD.

See the advanced usages section for more options.

EXAMPLE

BEFORE UPDATE:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

AFTER UPDATE:

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=one_database
DB_USERNAME=root
DB_PASSWORD=root

PHP

Default PHP settings:

  #PHP
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: vivasoft/php
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./.docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network
  • php.ini: you can modify or add any settings on your host machine's .docker/php/local.ini file and it should apply the changes on your application.
  • Dockerfile: contains all the require tools to build the vivasoft/php image. If you need any additional piece of software or another php extension you can easily add them in this file. See the official documentation for more information. After modifying the file you have to rebuild the image.

Database

Default settings:

  #MySQL
  db:
    image: mysql:5.7.28
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: one_database
      MYSQL_ROOT_PASSWORD: root
      
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql
      - ./.docker/mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network
  • You can use more environment variables. MYSQL_USER and MYSQL_PASSWORD are most important among them.

See more option on docker mysql official page.

Nginx

Default settings:

  #Nginx
  webserver:
    image: nginx:latest
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./.docker/nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

Run application into another ports: update the docker-compose.yml file.

  #Nginx
  webserver:
    ...
    ports:
      - "NEW_PORT:80"
      - "443:443"
    ...

Add SSL Certificate: Coming Soon.

Redis

Connecting via SSH

When your container up and running. You can SSH into your container by using the following command:

docker exec -it CONTAINER_NAME bash

Running php artisan command

There are two options to execute your php artisan command.

Option 1:
Run the following command from your project root directory.

docker-compose exec app php artisan

n.b: app is name of your PHP container.

Option 2: SSH into your PHP container then run php artisan

Connect To Database

See Update .env file section for current settings:

The DB_HOST should be your IP Address
The DB_DATABASE should be MYSQL_DATABASE.
The DB_PORT should be same as 3306.
The DB_USERNAME should be MYSQL_ROOT_PASSWORD or MYSQL_USER
The DB_PASSWORD should be MYSQL_ROOT_PASSWORD or MYSQL_PASSWORD.

Advance Usages

Rebuilding Image:

  • You can rebuild the image using docker-compose up -d --build command.

Coming Soon

Troubleshooting

  • Command Prompt for Windows User:
    • Windows user should use Power Shell.
    • If you want to use command prompt instead of Power Shell, you have to specify the path e.g: "(c:\User\ProjectPath)" instead of ${PWD}.
  • Got permission denied while trying to connect to the Docker daemon socket
  • PORT Binding Error:
    • Open docker-compose.yml file and update host machine port. (Example: See nginx run application into another ports.)

Deployment

Coming Soon

Contributor

License

The Vivasoft Laravel Docker is licensed under the terms of the MIT License