Build and Run Laravel 7 in Docker Part 5 - Final Steps
Add the Volumes
At the bottom of your docker-compose.yml you will need to add a volumns section in the same column as the services header. Paste the following:
volumes:
dbdata:
Your final docker-compose.yml should look like this and you can see the repo here Laravel Docker Tutorial:
version: "3.7"
services:
# The Application
app:
container_name: app
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
CONTAINER_ROLE: app
depends_on:
- database
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
# The Web Server
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
depends_on:
- app
ports:
- 80:80
database:
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
ports:
- "33061:3306"
volumes:
dbdata:
That's it!! Wow you've just build a Laravel App that will run on Docker. Ok so to run this in you root directory using terminal type:
docker-compose up
This will build your containers and allow you to access your Laravel app at http://localhost:80
.
Running commands inside containers: (App is the name of the container)
docker-compose exec app php artisan make:controller MyController
docker-compose exec app php artisan migrate
Troubleshooting
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
Make sure you have a password set in you .env file in Laravel
database_1 | ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'
Remove MYSQL_USER: root from your docker-compose
Categories: Posts