Provides Docker and Elastic Beanstalk integration for a Laravel 5 project
by Reflexions, (*1)
chmod 777
1.) Install Docker 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)
2.) Create a docker-compose.yml and docker-compose.override.yml in the project directory. Define the laravel service and any desired database services:, (*3)
version: '3' services: laravel: image: reflexions/docker-laravel-fedora:9 env_file: .env links: - database database: image: postgres:13 env_file: .env environment: LC_ALL: C.UTF-8
The override file shouldn't be committed to git. It's for settings that differ between environments., (*4)
version: '3' services: laravel: ports: - "80:80" volumes: - .:/var/www/laravel database: ports: - "5432:5432"
3.) Create an .env file in the project directory., (*5)
The database
service above corresponds to DB_HOST=database
below.
You'll have to fill in the blanks in the .env example:, (*6)
# laravel service APP_KEY= DB_CONNECTION=pgsql DB_HOST=database DB_DATABASE= DB_USERNAME= DB_PASSWORD= # database service POSTGRES_DB= POSTGRES_USER= POSTGRES_PASSWORD=
4.) Obtain a Github Personal Access Token. Provide that in the GITHUB_TOKEN env var, (*7)
Alternatively, create a GitHub user and ssh key that are only used by builds, and cp that to /root/.ssh/id_rsa, (*8)
4.) With one command download the images, create the service containers, and start the application:, (*9)
docker-compose up
5.) (optional) APP_KEY, (*10)
$ 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, (*11)
$ docker exec -it $(docker ps | grep reflexions/docker-laravel | awk '{print $1}') bash root@4c0491540409:/var/www/laravel# php artisan tinker
reflexions/docker-laravel-fedora
composer packageReflexions\DockerLaravel\DockerApplication
to prevent permissions errorsThese (webpack, gulp, etc) should be configured in your project's Dockerfile, (*12)
Add a Dockerfile to the root of the project to deploy with Elastic Beanstalk:, (*13)
FROM reflexions/docker-laravel-fedora:latest MAINTAINER "Your Name" <your@email.com> RUN /usr/share/docker-laravel-scripts/setup.sh 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
., (*14)
$ 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
., (*15)
Solution: Run Kitematic -> Install Docker Commands
. Then add the following line ~/.bash_profile:, (*16)
eval "$(docker-machine env dev)"
Solution: Restart cluster. Settings in the .env file are only read on start., (*17)
$ docker-compose restart
$ docker-compose up Illegal instruction: 4 $
Solution: Known issue with the Docker Toolbox on older CPUs. Install docker-compose using pip, (*18)
Solution: - Check that the DB_CONNECTION corresponds to the correct laravel db driver - Check that the DB_HOST corresponds to the name of the service listed in docker-compose.yml (i.e. "database" in the example above), (*19)
Solution:, (*20)
php artisan key:generate
to update APP_KEY on .env, then restart the container.Solution:
- Modify docker-config.yml
to reference MySQL:, (*21)
version: '3' services: laravel: image: reflexions/docker-laravel-fedora:26 env_file: .env links: - database database: image: mysql:5.6 env_file: .env environment: LC_ALL: C.UTF-8
version: '3' services: laravel: ports: - "80:80" volumes: - .:/var/www/laravel database: ports: - "3306:3306"
.env
to reference MySQL:DB_CONNECTION=mysql # database service MYSQL_ROOT_PASSWORD= MYSQL_DATABASE= MYSQL_USER= MYSQL_PASSWORD=
Solution:
- Modify docker-config.yml
to remove references to database
- Modify .env
to connect to MySQL via the docker-machine host ip address (192.168.99.1):, (*22)
DB_CONNECTION=mysql DB_HOST=192.168.99.1 DB_DATABASE= DB_USERNAME= DB_PASSWORD=
my.cnf
, or it can be hard coded in your startup script. To check the value use this sql:show variables like 'bind_address';
CREATE USER 'username'@'192.168.99.100' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON application.* TO 'username'@'192.168.99.100'; FLUSH PRIVILEGES;