Skip to content

Instantly share code, notes, and snippets.

@jtn7
Last active November 12, 2020 22:07
Show Gist options
  • Save jtn7/dfce2eb3fa7be967676e3aedeb223293 to your computer and use it in GitHub Desktop.
Save jtn7/dfce2eb3fa7be967676e3aedeb223293 to your computer and use it in GitHub Desktop.
FAQBot Development Environment Setup
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=pgsql
DB_HOST=database
DB_PORT=5432
DB_DATABASE=dbname
DB_USERNAME=example
DB_PASSWORD=example
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync
REDIS_HOST=cache
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
DIALOGFLOW_API_KEY=
SCOUT_ELASTIC_HOST=elastic:9200
SLACK_TOKEN=
version: '3'
services:
php:
build:
context: ./
dockerfile: php.dockerfile
container_name: php_project
volumes:
- ./:/code
nginx:
image: nginx:1.12-alpine
container_name: nginx_project
links:
- database
volumes:
- ./:/code
- ./nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- 80:80
database:
image: postgres:10-alpine
container_name: postgres_project
environment:
POSTGRES_DB: dbname
POSTGRES_USER: example
POSTGRES_PASSWORD: example
cache:
image: redis:3-alpine
container_name: redis_project
elastic:
image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
container_name: elastic_project
environment:
discovery.type: single-node
server {
listen 80;
server_name nginx_docker;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code/public;
index index.php;
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
log_not_found off;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000; # may also be: 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
FROM php:7-fpm-alpine
RUN apk update && apk --no-cache add \
postgresql-dev
RUN docker-php-ext-install pdo_pgsql
EXPOSE 9000
CMD ["php-fpm"]

Laravel Dockerization for FAQ

Introduction

This guide will walk you through the process of running laravel from Docker containers for our project.

To accomplish our goal we need a total of three resources:

And of course this tutorial will require you to have a working installation of Docker (if compatible with your OS) or Docker Toolbox

Downloading the Project Source Code

Currently, the development branch is located at gast499/faqbot . Download the repo as a zip or clone it into a directory that you feel comfortable working in for the rest of this tutorial.

Note that this project uses Laravel 5.6

Get Project Dependencies

The next thing we have to is install Laravel dependencies. To install Laravel dependencies we are going to make use of the composer Docker image. This way you don't have to worry about installing composer on your machine. But if you wish to use an installation of composer on your host machine you can use that as well.

Setting up aliases

By creating an alias we can shorten the command to run programs through docker. If you are using Docker Toolbox or Docker on Linux/Mac you can create aliases to make using docker images as applications easier. If you are using Docker on Windows 10 Pro you can accomplish the same thing through powershell see here. Aliases are temporary if they are set in a terminal session, so using a file that contains these aliases can be utilized to save them for the future. See LINK HERE to learn more about saving aliases.

Run the commands in this tutorial using Docker Quickstart Terminal if you are using Docker Toolbox and a plain terminal if you are using Linux/Mac.

Linux/Mac users make sure that the Docker dameon is running before you run any commands using Docker. Docker Toolbox users need to make sure to follow the instructions for linking drives to the boot2docker VM

Alias command to run composer through docker:

alias composer='docker run --rm -v `pwd`:/app composer'

Note that using composer through docker is not necessary if you have it installed on your system

Now that we have an alias for composer we need to use it to install the dependencies of our project. Navigate to the directory where you have downloaded the project source code (if you are new to using a terminal see here) and run the following commands.

If you are running the composer command with Docker for the first time it may take a few seconds to download, but after downloaded should run smoothly.

Add Botman package:

$ composer require botman/installer 

Add Predis package:

$ composer require predis/predis

Install the rest of the packages listed in composer.json with:

$ composer install

Setting up .env

Note that this step is not necessary if you have php installed on your system

Before we can run our project we need to change its settings. To accomplish that we use a file named .env. In our project source we have a file named .env.example . We'll use that as a base. Either make a copy of .env.example or rename it so that you end up with a file named .env with the default values contained in .env.example

Alias for php:

alias php='docker run --rm -v `pwd`:/p -w /p php:7-fpm-alpine php'

Generate APP_KEY:

$ php artisan key:generate

Although we use artisan here with a temporary container, in the future we are going to use artisan through a different container that will be connected to our running project and database.

The rest of the values should be set correctly if you got .env.example from the project source.

Running Project with Docker

Docker Compose

To run the project we are going to make use of Docker Compose to allow us to start our containers for php, nginx, postgres, redis, and elastic-search.

For those using Docker for Windows or Docker Toolbox you should already have the command docker-compose. Other operating systems should refer here for instructions to install Compose.

To get started place the docker-compose.yml file attached to this tutorial in your project folder. You also need to place php.dockerfile in that same directory for Compose to work correctly.

Once that is done, you can run:

$ docker-compose up

If you've done everything correctly up to this point, then you should see the console start downloading the images for the containers, extensions needed, and finally the standard output of each container with a label telling you which container the output is comming from.

Docker Compose may take a while, but should start up much faster after it's first run.

To view the project use your browser and enter localhost in your address bar. You should now see the Laravel defualt landing page.

If you are using Docker Toolbox however, you will need to type:

docker-machine ip

And enter that ip address in your address bar in place of localhost

If Laravel gives you an error, check that you have correctly setup your .env file.

To stop all the containers from running use the key bind Ctrl-C. If you want to run docker-compose in the background (without standard output) use the command:

$ docker-compose up -d

To stop the running containers in the background use this command:

$ docker-compose stop

Final Steps

Running artisan commands

This alias will be used to run artisan commands that interface with our database as well as phpunit:

alias phpcont="docker exec -w /code php_project"

Migrating to database:

$ phpcont php artisan migrate

Seeding database:

$ phpcont php artisan db:seed

Running phpunit:

$ phpcont ./vendor/bin/phpunit

Note that this command will not work while the project containers are not running

Coding for the Project

Now that you have working Dockerized servers running our project its time to code.

Run the container using Docker and change code in the source to see your changes reflected in your browser (through localhost or your docker-machine ip).

I recommend using Visual Studio Code for development, but you can use any text editor of your choice.

Using Docker Toolbox

For Docker Toolbox there are a few things that are different than the native version. This is due to the fact that Docker runs in a VM with Docker Toolbox through Virtual Box. Because Docker is running in a VM you will need to setup a link between your local drive and the virtual one. Another difference is that you have to use Docker Quickstart Terminal (which basically git bash) instead of a native terminal like the ones available on Mac and Linux and also PowerShell for Windows.

Create a link between Drives

  1. Open Virtual Box

VirtualBox

  1. Right Click the boot2docker VM and open settings

boot2docker

  1. In the shared folders tab create a new shared folder

Shared Folders

  1. Select the drive to link and enter the same letter with a forward slash after it. Don't forget to also tick the checkboxes for Auto-mount and Make Permanent.

Add Share

This step is necessary for sharing files with your containers

Using Docker Quickstart Terminal

Open Docker Quickstart Terminal

DQT Shortcut

The terminal will then start up the boot2docker VM and assign and IP to your "docker machine"

DQT Startup

After its finished you can use the bash how you would normally in a Mac or Linux environment. Also since the terminal is built on top of git bash you can also use git commands for source version control.

Whale

Exiting Docker Quickstart Terminal

For some reason my installation of Docker Quickstart Terminal does not shutdown the boot2docker VM when I exit the window. To solve this problem I made an alias to shutdown the boot2docker VM and close the window. If you have the same issue you will notice that Windows will let you know that Virtual Box is still running upon attempting to shutdown your PC.

Alias to shutdown VM

alias ex="docker-machine stop && exit"

Saving aliases

In a bash terminal aliases can be set up fairly easy. To save an alias just take the command for making aliases (as shown earlier in the tutorial) and append it to your .bashrc file located in your home directory ~/.

Navigate to your home directory and open your .bashrc file

Navigation in bash

Add aliases

Add aliases

Note that my aliases may be different from yours, but be sure to use the ones described earlier in the tutorial

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment