From 52d7c4e3e1e68b5d5330ff9f5f6ac49fb71152ca Mon Sep 17 00:00:00 2001 From: bizmate Date: Thu, 13 Mar 2025 14:54:17 +0400 Subject: [PATCH 1/3] Adding a certain level of automation with make. Undocumented to gather opinions first --- Makefile | 47 +++++++++++++++++++++++++ bin/build_env.sh | 7 ++++ bin/wait_for_docker.bash | 30 ++++++++++++++++ docker/development/workspace/Dockerfile | 5 +++ 4 files changed, 89 insertions(+) create mode 100644 Makefile create mode 100755 bin/build_env.sh create mode 100755 bin/wait_for_docker.bash diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2bae039 --- /dev/null +++ b/Makefile @@ -0,0 +1,47 @@ +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 images + +build: composer install_artisan_encryption_key npm_run_dev + +docker_build: + docker compose -f compose.dev.yaml build + +composer: + docker compose -f compose.dev.yaml exec workspace bash -c "composer install" + +install_artisan_encryption_key: + docker compose -f compose.dev.yaml exec workspace bash -c "php artisan key:generate" + +npm_run_dev: + docker compose -f compose.dev.yaml exec -it workspace bash -c "/usr/local/bin/npm install && /usr/local/bin/npm run dev" + +bash: + docker compose -f compose.dev.yaml exec workspace bash + +up: build_env up_nobuild build + +up_nobuild: + 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 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/bin/build_env.sh b/bin/build_env.sh new file mode 100755 index 0000000..48a3c1c --- /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/docker/development/workspace/Dockerfile b/docker/development/workspace/Dockerfile index 9e5d90e..d0578dd 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 -s /home/www/.nvm/versions/node/v22.0.0/bin/node /usr/local/bin/node && \ + ln -s /home/www/.nvm/versions/node/v22.0.0/bin/npm /usr/local/bin/npm \ + # Set the working directory WORKDIR /var/www From 67f582fd705d2450b8f5a336a8a8bb3a62237b40 Mon Sep 17 00:00:00 2001 From: bizmate Date: Sat, 15 Mar 2025 19:52:16 +0400 Subject: [PATCH 2/3] Adding migrations automation and fixing make up --- Makefile | 15 +++++++++------ README.md | 13 ++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 2bae039..9a26e70 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ docker_config: build_env images: docker compose -f compose.dev images -build: composer install_artisan_encryption_key npm_run_dev +build: composer docker_build: docker compose -f compose.dev.yaml build @@ -18,7 +18,10 @@ composer: docker compose -f compose.dev.yaml exec workspace bash -c "composer install" install_artisan_encryption_key: - docker compose -f compose.dev.yaml exec workspace bash -c "php artisan key:generate" + 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 "/usr/local/bin/npm install && /usr/local/bin/npm run dev" @@ -26,17 +29,17 @@ npm_run_dev: bash: docker compose -f compose.dev.yaml exec workspace bash -up: build_env up_nobuild build +up: build_env up_nobuild build artisan_run_migrations npm_run_dev -up_nobuild: +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 down + docker compose down down_ci: - docker-compose -f compose.dev.yaml down || exit 0 + docker compose -f compose.dev.yaml down || exit 0 #docker_clean_dangling_images_and_volumes: docker_clean: 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. From 6fcf1da9ffb4b59e4edda29a0b19c9deaf12470b Mon Sep 17 00:00:00 2001 From: bizmate Date: Wed, 19 Mar 2025 16:00:52 +0400 Subject: [PATCH 3/3] Fix up commands - add extra argument to sed so it works on Mac - force creation of symlinks - specify workdir in docker compose file in case docker starts runs from root instead - fix step in make file to ensure artisan is present before running key generation commands --- Makefile | 14 ++++++-------- bin/build_env.sh | 2 +- compose.dev.yaml | 1 + docker/development/workspace/Dockerfile | 4 ++-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 9a26e70..5d2259c 100644 --- a/Makefile +++ b/Makefile @@ -7,15 +7,13 @@ docker_config: build_env bin/build_env.sh && docker compose -f compose.dev config images: - docker compose -f compose.dev images - -build: composer + docker compose -f compose.dev.yaml images docker_build: docker compose -f compose.dev.yaml build composer: - docker compose -f compose.dev.yaml exec workspace bash -c "composer install" + 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" @@ -24,19 +22,19 @@ 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 "/usr/local/bin/npm install && /usr/local/bin/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 exec workspace bash + docker compose -f compose.dev.yaml run --rm workspace bash -up: build_env up_nobuild build artisan_run_migrations npm_run_dev +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 down + docker compose -f compose.dev.yaml down down_ci: docker compose -f compose.dev.yaml down || exit 0 diff --git a/bin/build_env.sh b/bin/build_env.sh index 48a3c1c..5c96b58 100755 --- a/bin/build_env.sh +++ b/bin/build_env.sh @@ -4,4 +4,4 @@ uid="$( id -u )" gid="$( id -g )" sed "s/UID=1000/UID=$uid/" .env.example > .env -sed -i "s/GID=1000/UID=$gid/" .env +sed -i "" "s/GID=1000/UID=$gid/" .env 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 d0578dd..8bb5b9d 100644 --- a/docker/development/workspace/Dockerfile +++ b/docker/development/workspace/Dockerfile @@ -79,8 +79,8 @@ RUN echo 'export NVM_DIR="$HOME/.nvm"' >> /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 -s /home/www/.nvm/versions/node/v22.0.0/bin/node /usr/local/bin/node && \ - ln -s /home/www/.nvm/versions/node/v22.0.0/bin/npm /usr/local/bin/npm \ +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