reflexions / docker-laravel

Docker container for Laravel 5 applications
https://reflexions.co
42 stars 9 forks source link

reflexions/docker-laravel

by Reflexions

Instructions

1.) Install Docker Toolbox to get docker, docker-compose, and the Kitematic GUI. Open a terminal with the docker env variables via Kitematic -> File -> Open Docker Command Line Terminal

2.) Create a docker-compose.yml in the project directory. Define the laravel service and any desired database services:

laravel:
  image: reflexions/docker-laravel:latest
  ports:
    - 80:80
  env_file: .env
  links:
    - database
  volumes:
    - .:/var/www/laravel

database:
  image: postgres:9.6
  ports:
    - 5432:5432
  env_file: .env
  environment:
    LC_ALL: C.UTF-8

3.) Obtain a Github Personal Access Token. Create an .env file in the project directory. Configure laravel and other services as desired. The database service above corresponds to DB_HOST=database below:

# laravel service
GITHUB_TOKEN=Your_Github_Token
APP_KEY=SomeRandomString
DB_CONNECTION=pgsql
DB_HOST=database
DB_DATABASE=application
DB_USERNAME=username
DB_PASSWORD=password

# database service
POSTGRES_DB=application
POSTGRES_USER=username
POSTGRES_PASSWORD=password

4.) With one command download the images, create the service containers, and start the application:

docker-compose up

5.) (optional) APP_KEY

$ docker exec -it $(docker ps | grep reflexions/docker-laravel | awk '{print $1}') bash
root@4c0491540409:/var/www/laravel# php artisan key:generate

6.) (optional) Tinker

$ docker exec -it $(docker ps | grep reflexions/docker-laravel | awk '{print $1}') bash
root@4c0491540409:/var/www/laravel# php artisan tinker

Overview

Front-end build systems

Front-end build systems (gulp, grunt, bower, etc) are best installed outside of docker. The resulting assets will be readily accessible via the volume mapping defined on the laravel service.

Elastic Beanstalk

Add a Dockerfile to the root of the project to deploy with Elastic Beanstalk:

FROM reflexions/docker-laravel:latest

MAINTAINER "Your Name" <your@email.com>

COPY . /var/www/laravel
WORKDIR /var/www/laravel

EXPOSE 80
ENTRYPOINT ["/usr/share/docker-laravel/bin/start.sh"]

This will define an application container. Use RDS to create the database. Add all variables from the .env file (including the APP_KEY, DB_HOST, etc) into the AWS Management Console -> Elastic Beanstalk -> Your-Environment -> Configuration -> Software Configuration.

Troubleshooting

Problem: Mac OS X: Couldn't connect to docker daemon
$ docker-compose up
ERROR: Couldn't connect to Docker daemon - you might need to run `docker-machine start default`.
$

Solution: Open terminal with Kitematic -> File -> Open Docker Command Line Terminal.

Problem: Don't like the Docker Command Line Terminal

Solution: Run Kitematic -> Install Docker Commands. Then add the following line _~/.bashprofile:

eval "$(docker-machine env dev)"
Problem: Changes to .env file apparently ignored by laravel

Solution: Restart cluster. Settings in the .env file are only read on start.

$ docker-compose restart
Problem: Mac OS X: Illegal Instruction 4
$ docker-compose up
Illegal instruction: 4
$

Solution: Known issue with the Docker Toolbox on older CPUs. Install docker-compose using pip

Problem: Can't connect to database

Solution:

Problem: RuntimeException: No supported encrypter found. The cipher and / or key length are invalid.

Solution:

Problem: Want to use mysql instead of postgres

Solution:

database: image: mysql:5.6 ports:

database service

MYSQL_ROOT_PASSWORD=password MYSQL_DATABASE=application MYSQL_USER=username MYSQL_PASSWORD=password


##### **Problem:** Want to use mysql already running on local machine (not docker)

_**Solution:**_
  - Modify `docker-config.yml` to drop the unnecessary database service:
```yaml
laravel:
  image: reflexions/docker-laravel:latest
  ports:
    - 80:80
  env_file: .env
  volumes:
    - .:/var/www/laravel