Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automation would help in the bootup process #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
This project is licensed under the MIT License. See the LICENSE file for more details.
7 changes: 7 additions & 0 deletions bin/build_env.sh
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions bin/wait_for_docker.bash
Original file line number Diff line number Diff line change
@@ -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 "$@"
1 change: 1 addition & 0 deletions compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ services:
- .env
volumes:
- ./:/var/www
working_dir: /var/www
networks:
- laravel-development

Expand Down
5 changes: 5 additions & 0 deletions docker/development/workspace/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down