diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5d2259c --- /dev/null +++ b/Makefile @@ -0,0 +1,48 @@ +SHELL := /usr/bin/env bash + +build_env: + bin/build_env.sh + +docker_config: build_env + bin/build_env.sh && docker compose -f compose.dev config + +images: + docker compose -f compose.dev.yaml images + +docker_build: + docker compose -f compose.dev.yaml build + +composer: + docker compose -f compose.dev.yaml run --rm workspace bash -c "composer install" + +install_artisan_encryption_key: + docker compose -f compose.dev.yaml run --rm workspace bash -c "php artisan key:generate" + +artisan_run_migrations: + docker compose -f compose.dev.yaml exec workspace php artisan migrate + +npm_run_dev: + docker compose -f compose.dev.yaml exec -it workspace bash -c "/home/www/.nvm/versions/node/v22.0.0/bin/npm install && /home/www/.nvm/versions/node/v22.0.0/bin/npm run dev" + +bash: + docker compose -f compose.dev.yaml run --rm workspace bash + +up: build_env composer up_nobuild artisan_run_migrations npm_run_dev + +up_nobuild: install_artisan_encryption_key + docker compose -f compose.dev.yaml up -d --force-recreate --remove-orphans + bin/wait_for_docker.bash "database system is ready to accept connections" + +down: + docker compose -f compose.dev.yaml down + +down_ci: + docker compose -f compose.dev.yaml down || exit 0 + +#docker_clean_dangling_images_and_volumes: +docker_clean: + docker rmi $(docker images --filter "dangling=true" -q --no-trunc) +#docker volume rm $(docker volume ls -qf dangling=true) + +logs_tail: build_env + docker compose -f compose.dev.yaml logs -f diff --git a/README.md b/README.md index 32ba721..cccf0b0 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,17 @@ git clone https://github.com/rw4lll/laravel-docker-examples.git cd laravel-docker-examples ``` +### On Linux, Mac or shell with make + +If you are using a Linux, Mac or Windows with a shell such as Cygwin or similar you can run all the following steps in +one just by using `make` . + +- To start the development stack run `make up` +- You can get on the workspace container by running `make bash` +- To stop the containers run `make down`. + +You can check the Makefile to see how all the steps below are automated. + ### Setting Up the Development Environment 1. Copy the .env.example file to .env and adjust any necessary environment variables: @@ -246,4 +257,4 @@ git commit -m "Description of changes" ## License -This project is licensed under the MIT License. See the LICENSE file for more details. \ No newline at end of file +This project is licensed under the MIT License. See the LICENSE file for more details. diff --git a/bin/build_env.sh b/bin/build_env.sh new file mode 100755 index 0000000..5c96b58 --- /dev/null +++ b/bin/build_env.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +uid="$( id -u )" +gid="$( id -g )" + +sed "s/UID=1000/UID=$uid/" .env.example > .env +sed -i "" "s/GID=1000/UID=$gid/" .env diff --git a/bin/wait_for_docker.bash b/bin/wait_for_docker.bash new file mode 100755 index 0000000..59f1c73 --- /dev/null +++ b/bin/wait_for_docker.bash @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +waitForDocker () { + # wait until we see the required logs + WAIT_LOG=$1 + + MAX_ATTEMPTS=150 + ATTEMPTS=0 + while [ $ATTEMPTS -le $MAX_ATTEMPTS ]; do + ATTEMPTS=$(( ATTEMPTS + 1 )) + + echo "Waiting for container log FRASE $WAIT_LOG - (attempt: $ATTEMPTS)..." + DOCKER_LOGS=$(docker compose -f compose.dev.yaml logs ) + if grep -q "$WAIT_LOG" <<< "$DOCKER_LOGS" ; then + echo "Containers are Ready!" + break + else + echo "Containers NOT Ready!" + fi + sleep 2 + done + + # https://github.com/koalaman/shellcheck/wiki/SC2181 + if ! grep -q "$WAIT_LOG" <<< "$DOCKER_LOGS" ; then + echo "Containers are not ready, tests will not run!" + exit 1 + fi +} + +waitForDocker "$@" diff --git a/compose.dev.yaml b/compose.dev.yaml index 263480a..d7c6bd7 100644 --- a/compose.dev.yaml +++ b/compose.dev.yaml @@ -65,6 +65,7 @@ services: - .env volumes: - ./:/var/www + working_dir: /var/www networks: - laravel-development diff --git a/docker/development/workspace/Dockerfile b/docker/development/workspace/Dockerfile index 9e5d90e..8bb5b9d 100644 --- a/docker/development/workspace/Dockerfile +++ b/docker/development/workspace/Dockerfile @@ -77,6 +77,11 @@ RUN echo 'export NVM_DIR="$HOME/.nvm"' >> /home/www/.bashrc && \ echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> /home/www/.bashrc && \ echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> /home/www/.bashrc +# Bash completion does not work in one line docker compose exec commands so forcing the installation of symlinks +USER root +RUN ln -sf /home/www/.nvm/versions/node/v22.0.0/bin/node /usr/local/bin/node && \ + ln -sf /home/www/.nvm/versions/node/v22.0.0/bin/npm /usr/local/bin/npm + # Set the working directory WORKDIR /var/www