This repository is a template to help setup medium-sized laravel application deployed in a docker container. For example you can use the docker-compose.yml file to setup a local development environment:
flowchart RL
client
subgraph VM
direction TB
mysql
nginx
php_service
php_service <--> mysql
php_service <--> nginx
end
client <--> nginx
php_service- php serviceclient- your local machineVM- virtual machine (production)nginx- networking servermysql- mysql database
For large application, consider using infrastructure as code tools to deploy your application on a virtual machine.
- PHP 8.2
- Composer (phar file)
- Docker
To get started, copy the files in this repository to your project.
PROJECT_PATH=../laravel
cp Dockerfile docker-compose.yml .dockerignore .env.docker $PROJECT_PATH
cd $PROJECT_PATH
docker compose up -dDockerfile- builds the docker imagedocker-compose.yml- defines the network and services.dockerignore- defines the files to be ignored by docker on build.env.docker- defines the environment variablesgithub/workflows/docker-release.yml- defines the github actions for release
To test this repository you can run the following:
git clone https://github.com/miguelaperez/laravel-docker-template.git
cd laravel-docker-template
docker compose up -d
docker compose exec bash
> php artisan migrate
> php artisan db:seedVisit http://localhost:8080 to see the application.
To build the docker image, run the following command:
DOCKER_VERSION=$(jq -r '.version' composer.json)
DOCKER_DESCRIPTION=$(jq -r '.description' composer.json)
DOCKER_REPOSITORY=$(jq -r '.name' composer.json)
docker build \
--build-arg DOCKER_VERSION=$DOCKER_VERSION \
--build-arg DOCKER_DESCRIPTION=$DOCKER_DESCRIPTION \
-t $DOCKER_REPOSITORY:$DOCKER_VERSION \
-t $DOCKER_REPOSITORY:latest \
.To push your image to github container registry, run the following command:
DOCKER_VERSION=$(jq -r '.version' composer.json)
DOCKER_REPOSITORY=$(jq -r '.name' composer.json)
docker push $DOCKER_REPOSITORY:$DOCKER_VERSION
docker push $DOCKER_REPOSITORY:latestNote: Make sure your
composer.jsonfile is setup with the correctname,version, anddescription.
If you'd like to enable automatic docker releases for your repository, you can copy the github/workflows/docker-release.yml file to your repository. This will automatically build and push a new docker each time you create a new tag. It uses the name and version field in the composer.json file to determine the image tags.
cp .github/workflows/docker-release.yml ../laravelLearn more in the release section below.
To build for a specific PHP version, you can fork this repository and update the FROM line in the Dockerfile to use the desired PHP version.
Versioning is handled by semantic-release. To release a new build, you can bump the version in the composer.json file and create a new GitHub release.
To update release conditions update the github/workflows/docker-release.yml config.
Thank you for taking the time to contribute to this project! Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Create an issue or submit a pull request to contribute.
If you discover a security vulnerability, please create a new issue on GitHub. All security vulnerabilities will be promptly addressed.
This project is licensed under the MIT License.