This guide outlines the steps to build and run a Laravel application using Docker for DevOps assignemnt 1 & 2.
- Docker installed on your system
- Laravel project stored in
src/directory - MySQL data storage in
data/directory
Build the necessary images for the Laravel application, MySQL database, Composer, and Node.js:
# Build Laravel app image
docker build -t myapp .
# Build MySQL image
docker build -t mydb -f dockerfile.mysql .
# Build Composer image
docker build -t mycomposer -f dockerfile.composer .
# Build Node.js image
docker build -t mynode -f dockerfile.node .Create a bridge network named larwork to enable communication between containers:
docker network create -d bridge larworkRun Composer and Node.js inside temporary containers to install Laravel dependencies:
# Install PHP dependencies using Composer
docker run --rm -v ${PWD}/src/:/var/app/ mycomposer
# Install JavaScript dependencies using Node.js
docker run --rm -v ${PWD}/src/:/var/app/ mynodeRun the MySQL container and mount the data/ directory for persistent storage:
docker run --name mydb --network larwork -d -v ${PWD}/data/:/var/lib/mysql/ mydbRun the Laravel application container and expose it on port 8080:
docker run --name myapp --network larwork -d -p 8080:80 -v ${PWD}/src/:/var/www/html/ myappEnsure Laravel has the necessary permissions for the storage/ directory:
docker exec myapp /bin/bash -c 'chown -R www-data:www-data ./storage/'
docker exec myapp /bin/bash -c 'chmod -R 775 storage'Execute Laravel migrations inside the running application container:
docker exec myapp php artisan migrateOnce all steps are completed, access your Laravel project in a web browser at:
http://localhost:8080
This guide expands on Assignment 1 by introducing Docker Compose to manage the multi-container environment for the Laravel application, MySQL database, Composer, and Node.js.
- Docker and Docker Compose installed on your system
- Laravel project stored in
src/directory - MySQL data storage in
data/directory
To build and start the environment, run the following command in the terminal:
docker-compose up -d (--build)This will automatically build the containers defined in the docker-compose.yml file and start them. The --build is optional: the flag ensures that Docker rebuilds any images before starting the services.
Once everything is set up, you can access the Laravel application in a browser at:
http://localhost:8080