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

Added Docker Setup Instructions and Automation Script #99

Open
wants to merge 2 commits into
base: develop
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
68 changes: 68 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# syntax=docker/dockerfile:1
FROM php:7.1-fpm

# Set the working directory
WORKDIR /var/www

# Install necessary dependencies and diagnostic tools
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
libmagickwand-dev --no-install-recommends \
libonig-dev \
nano \
iputils-ping \
procps \
net-tools \
iproute2 \
lsof \
strace \
htop

# Clean up the apt cache to reduce the image size
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install necessary PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd tokenizer ctype json xml

# Install and enable the Imagick extension
RUN pecl install imagick && docker-php-ext-enable imagick

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Copy the content of the current directory to the working directory in the container
COPY . /var/www

# Set ownership and permissions for the copied files
COPY --chown=www-data:www-data . /var/www

# Create PHP-FPM log directory and set permissions
RUN mkdir -p /var/log/php-fpm && chown -R www-data:www-data /var/log/php-fpm

# Add custom PHP-FPM configurations
RUN echo "catch_workers_output = yes" >> /usr/local/etc/php-fpm.d/www.conf
RUN echo "pm = ondemand" >> /usr/local/etc/php-fpm.d/www.conf
RUN echo "pm.max_requests = 500" >> /usr/local/etc/php-fpm.d/www.conf
RUN echo "request_slowlog_timeout = 5s" >> /usr/local/etc/php-fpm.d/www.conf
RUN echo "slowlog = /var/log/php-fpm/www-slow.log" >> /usr/local/etc/php-fpm.d/www.conf
RUN echo "rlimit_files = 65535" >> /usr/local/etc/php-fpm.d/www.conf

# Set permissions for Laravel directories
RUN chown -R www-data:www-data /var/www && chmod -R 755 /var/www/storage /var/www/bootstrap/cache

# Switch to the www-data user
USER www-data

# Expose port 9000 and start the PHP-FPM server
EXPOSE 9000
CMD ["php-fpm"]
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,103 @@ email: [email protected]
password: password
```

### Docker Method

1. Clone to your server root
```sh
git clone -b master [email protected]:lubusIN/laravel-gymie.git
```
> For faster updates and bleeding edge features, or if you want to help test the next version, use the `develop` branch instead of the `master` branch.

2. Ensure Docker and Docker Compose are installed on your system.

3. Build and start Docker containers:
```sh
docker-compose up --build -d
```

4. Wait for MySQL to be ready (can take a few moments). Ensure MySQL is ready before proceeding.

5. Create necessary directories and set permissions:
```sh
docker-compose exec app sh -c "mkdir -p /var/www/vendor && chown -R www-data:www-data /var/www/vendor"
docker-compose exec app sh -c "mkdir -p /var/www/storage && chown -R www-data:www-data /var/www/storage"
docker-compose exec app sh -c "mkdir -p /var/www/bootstrap/cache && chown -R www-data:www-data /var/www/bootstrap/cache"
docker-compose exec app sh -c "mkdir -p /var/www/html && chown -R www-data:www-data /var/www/html"
```

6. Install composer dependencies:
```sh
docker-compose exec app composer install
```

7. Copy `.env.example` to `.env` and update it:
```sh
cp .env.example .env
sed -i "s/DB_CONNECTION=.*/DB_CONNECTION=mysql/" .env
sed -i "s/DB_HOST=.*/DB_HOST=mysql/" .env
sed -i "s/DB_PORT=.*/DB_PORT=3306/" .env
sed -i "s/DB_DATABASE=.*/DB_DATABASE=gymie/" .env
sed -i "s/DB_USERNAME=.*/DB_USERNAME=gymie/" .env
sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=password/" .env
```

8. Generate application key:
```sh
docker-compose exec app php artisan key:generate
```

9. Run migrations and seed the database:
```sh
docker-compose exec app php artisan migrate --seed
```

10. Set permissions for storage and cache inside container:
```sh
docker-compose exec app sh -c "chown -R www-data:www-data storage bootstrap/cache && chmod -R ug+rwx storage bootstrap/cache"
```

11. Modify php-fpm listen directive:
```sh
docker-compose exec app sh -c "sed -i 's/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/' /usr/local/etc/php-fpm.d/www.conf"
```

12. Restart php-fpm to apply changes:
```sh
docker-compose exec app sh -c "pkill -o -USR2 php-fpm"
```

13. Create test PHP file:
```sh
docker-compose exec app sh -c "echo '<?php phpinfo(); ?>' > /var/www/html/test.php"
```

14. Add cron job for scheduled tasks:
```sh
(crontab -l 2>/dev/null; echo "* * * * * cd $(pwd) && docker-compose exec app php artisan schedule:run >> /dev/null 2>&1") | crontab -
```

15. All set! Use the following credentials to log in:
```
email: [email protected]
password: password
```

#### Particularities for Docker

- We are using Nginx as a reverse proxy to access the PHP application due to compatibility issues with the PHP version.

#### Using the setup script

If you prefer to use a script to automate the setup process, you can use `setup_gymie_docker.sh` located in the project directory:

```sh
./setup_gymie_docker.sh
```
#### Particularities for Docker

- We are using Nginx as a reverse proxy to access the PHP application due to compatibility issues with the PHP version.

## Troubleshooting

**APP_KEY not getting added to .env**
Expand Down
73 changes: 73 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
version: '3.9'
services:
app:
build:
context: .
dockerfile: Dockerfile
image: laravel-gymie
container_name: laravel_gymie_app
restart: unless-stopped
working_dir: /var/www
volumes:
- ./:/var/www
depends_on:
- mysql
networks:
- laravel_gymie
deploy:
resources:
limits:
cpus: '0.50'
memory: 512M

mysql:
image: mysql:5.7
container_name: laravel_gymie_db
restart: unless-stopped
environment:
MYSQL_DATABASE: gymie
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: gymie
MYSQL_PASSWORD: password
volumes:
- mysql_data:/var/lib/mysql
ports:
- "3306:3306"
networks:
- laravel_gymie
deploy:
resources:
limits:
cpus: '0.50'
memory: 512M
ulimits:
nofile:
soft: 1048576
hard: 1048576

nginx:
image: nginx:latest
container_name: laravel_gymie_nginx
restart: unless-stopped
ports:
- "8081:80"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- .:/var/www
depends_on:
- app
networks:
- laravel_gymie
deploy:
resources:
limits:
cpus: '0.25'
memory: 256M

networks:
laravel_gymie:
driver: bridge

volumes:
mysql_data:
driver: local
25 changes: 25 additions & 0 deletions nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
server {
listen 80;

server_name localhost;
root /var/www/public;

index index.php index.html;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}

location ~ /\.ht {
deny all;
}
}
100 changes: 100 additions & 0 deletions setup_gymie_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#! /bin/bash

# Define environment variables
APP_ENV=local
APP_DEBUG=true
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=gymie
DB_USERNAME=gymie
DB_PASSWORD=password

# Define project path
PROJECT_PATH=$(pwd)

# Set permissions for the local project directory
echo "Setting permissions for the project directory..."
sudo chmod -R 777 $PROJECT_PATH

# Stop and remove containers, networks, volumes, and images created by `up`
echo "Stopping and removing existing containers, networks, and volumes..."
docker-compose down -v

# Build and start Docker containers
echo "Building and starting Docker containers..."
docker-compose up --build -d

# Wait for MySQL to be ready
echo "Waiting for MySQL to be ready..."
MYSQL_READY=false
for i in {1..30}; do
if docker-compose exec mysql mysqladmin ping -h"$DB_HOST" --silent; then
MYSQL_READY=true
break
fi
echo "MySQL is unavailable - sleeping ($i/30)"
sleep 5
done

if [ "$MYSQL_READY" = false ]; then
echo "MySQL failed to start."
docker-compose logs mysql
exit 1
fi

# Create necessary directories and set permissions
echo "Creating necessary directories and setting permissions..."
docker-compose exec app sh -c "mkdir -p /var/www/vendor && chown -R www-data:www-data /var/www/vendor"
docker-compose exec app sh -c "mkdir -p /var/www/storage && chown -R www-data:www-data /var/www/storage"
docker-compose exec app sh -c "mkdir -p /var/www/bootstrap/cache && chown -R www-data:www-data /var/www/bootstrap/cache"
docker-compose exec app sh -c "mkdir -p /var/www/html && chown -R www-data:www-data /var/www/html"

# Install composer dependencies
echo "Installing composer dependencies..."
docker-compose exec app composer install

# Copy .env.example to .env
echo "Copying .env.example to .env..."
cp .env.example .env

# Update .env with database details
echo "Updating .env with database details..."
sed -i "s/DB_CONNECTION=.*/DB_CONNECTION=$DB_CONNECTION/" .env
sed -i "s/DB_HOST=.*/DB_HOST=$DB_HOST/" .env
sed -i "s/DB_PORT=.*/DB_PORT=$DB_PORT/" .env
sed -i "s/DB_DATABASE=.*/DB_DATABASE=$DB_DATABASE/" .env
sed -i "s/DB_USERNAME=.*/DB_USERNAME=$DB_USERNAME/" .env
sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=$DB_PASSWORD/" .env

# Generate application key
echo "Generating application key..."
docker-compose exec app php artisan key:generate

# Run migrations and seed the database
echo "Running migrations and seeding the database..."
docker-compose exec app php artisan migrate --seed

# Set permissions for storage and cache inside container
echo "Setting permissions for storage and cache inside the container..."
docker-compose exec app sh -c "chown -R www-data:www-data storage bootstrap/cache && chmod -R ug+rwx storage bootstrap/cache"

# Modify php-fpm listen directive
echo "Modifying PHP-FPM listen directive..."
docker-compose exec app sh -c "sed -i 's/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/' /usr/local/etc/php-fpm.d/www.conf"

# Restart php-fpm to apply changes
echo "Restarting PHP-FPM to apply changes..."
docker-compose exec app sh -c "pkill -o -USR2 php-fpm"

# Create test PHP file
echo "Creating test PHP file..."
docker-compose exec app sh -c "echo '<?php phpinfo(); ?>' > /var/www/html/test.php"

# Add cron job for scheduled tasks
echo "Adding cron job for scheduled tasks..."
(crontab -l 2>/dev/null; echo "* * * * * cd $PROJECT_PATH && docker-compose exec app php artisan schedule:run >> /dev/null 2>&1") | crontab -

echo "Laravel Gymie setup complete. Use the following credentials to log in:"
echo "Email: [email protected]"
echo "Password: password"