Skip to content

Commit

Permalink
Containerized the Application (#41)
Browse files Browse the repository at this point in the history
Co-authored-by: Omotayo Obafemi <[email protected]>
  • Loading branch information
obafemitayor and omotayor-testlio authored Dec 9, 2024
1 parent b4aded0 commit 872c895
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,19 @@ php artisan db:seed
```bash
php artisan serve
```

### Running the application via docker
You can also run the BE via a container if you are experiencing difficulty with the local setup

#### Prerequiste
Before you begin make sure you have the docker daemon installed and running on your PC. If you don't have docker installed, you have to either download [docker desktop](https://www.docker.com/products/docker-desktop/) or [OrbStack](https://orbstack.dev/download), and install it. Note OrbStack only works on Mac

#### Building and Running the container using docker-compose
Open a new terminal, navigate to the root directory of the repo, and run the next set of commands to configure and start the container.
- `docker-compose build` to build the container
- `docker-compose run app composer install` to install the PHP packages
- `docker-compose run app npm install` to install the node packages
- `docker-compose run app php artisan migrate` to run the migrations
- `docker-compose run app php artisan db:seed` to seed the database
- `docker-compose up -d app` to start the container
- Launch the app by opening `http://localhost:8000/` on your web browser
52 changes: 52 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Use the official PHP image with CLI
FROM php:8.2-cli

# Install required extensions and tools
RUN apt-get update && apt-get install -y \
git \
unzip \
libpng-dev \
libonig-dev \
libxml2-dev \
libzip-dev \
curl \
&& docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip

# Install Node.js and npm
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - \
&& apt-get install -y nodejs

# Copy Composer into the image
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /var/www/whatcompstack-BE

# Copy application files
COPY . /var/www/whatcompstack-BE

# Add Git safe directory to avoid "dubious ownership" errors
RUN git config --global --add safe.directory /var/www/whatcompstack-BE

# Create .env file if it doesn't exist
RUN cp .env.example .env || true

# Run Composer to install dependencies
RUN composer install

# Run npm to install frontend dependencies
RUN npm install

# Set permissions for Laravel
RUN chown -R www-data:www-data /var/www/whatcompstack-BE \
&& chmod -R 775 /var/www/whatcompstack-BE/storage \
&& chmod -R 775 /var/www/whatcompstack-BE/bootstrap/cache

# Run Laravel commands
RUN php artisan key:generate

# Expose Laravel's default port
EXPOSE 8000

# Command to start Laravel's built-in development server
CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]
37 changes: 37 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: '3.8'

services:
app:
build:
context: .
dockerfile: Dockerfile
container_name: whatcompstack-backend
ports:
- "8000:80"
volumes:
- .:/var/www/whatcompstack-BE
environment:
- APP_ENV=local
- APP_DEBUG=true
- APP_KEY=
- DB_HOST=db
- DB_PORT=3306
- DB_DATABASE=whatcompstack
- DB_USERNAME=root
- DB_PASSWORD=root
depends_on:
- db

db:
image: mysql:8.0
container_name: whatcompstack-db
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: whatcompstack
MYSQL_ROOT_PASSWORD: root
volumes:
- db_data:/var/lib/mysql

volumes:
db_data:

0 comments on commit 872c895

Please sign in to comment.