Skip to content

Commit 23c63b4

Browse files
committed
Add initial Docker setup for Laravel PHP 7.4 application
This commit includes the addition of the following files: .gitignore: This file includes the necessary ignore rules for the docker-compose/mysql and logs directories. Dockerfile: This file contains the instructions for building the PHP 7.4 image for the Laravel application. The Dockerfile installs system dependencies, PHP extensions, npm, and Composer. It also creates a system user to run Composer and Artisan commands and sets the working directory to /var/www. docker-compose.yml: This file is used to configure and run the containers for the Laravel application, MySQL database, and Nginx web server. docker-compose/mysql/.empty: This file is an empty placeholder to ensure that the mysql directory is included in the Git repository. docker-compose/nginx/nginx.conf: This file contains the Nginx configuration for the Laravel application. logs/.empty: This file is an empty placeholder to ensure that the logs directory is included in the Git repository. This commit provides a basic Docker setup for the Laravel PHP 7.4 application and sets the foundation for future development and deployment.
1 parent 371daa3 commit 23c63b4

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
docker-compose/mysql/*
2+
!docker-compose/mysql/.empty
3+
logs/*
4+
!logs/.empty

DockerFile

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Dockerfile for Laravel PHP 7.4 application with PHP-FPM
2+
FROM php:7.4-fpm
3+
4+
# Arguments passed from docker-compose.yml file
5+
ARG user
6+
ARG uid
7+
8+
# Install system dependencies
9+
RUN apt-get update && apt-get install -y \
10+
git \
11+
curl \
12+
libpng-dev \
13+
libonig-dev \
14+
libxml2-dev \
15+
zip \
16+
unzip \
17+
libfreetype6-dev \
18+
libjpeg-dev \
19+
libjpeg62-turbo-dev \
20+
libmagickwand-dev
21+
22+
# Clear cache
23+
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
24+
25+
# Install PHP extensions
26+
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd soap pdo_mysql intl
27+
28+
# Install dependencies for Imagick extension
29+
RUN apt-get update && apt-get install -y libmagickwand-dev libzip-dev
30+
31+
# Install Imagick extension
32+
RUN pecl install imagick \
33+
&& docker-php-ext-enable imagick
34+
35+
# Install Zip extension
36+
RUN docker-php-ext-install zip
37+
38+
# Install npm and node
39+
RUN apt-get update && apt-get install -y \
40+
software-properties-common \
41+
npm
42+
43+
# Update npm and node to latest version
44+
RUN npm install npm@latest -g && \
45+
npm install n -g && \
46+
n 16.13.1
47+
48+
# Get latest Composer
49+
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
50+
51+
# Create system user to run Composer and Artisan commands
52+
RUN useradd -G www-data,root -u $uid -d /home/$user $user
53+
RUN mkdir -p /home/$user/.composer && \
54+
chown -R $user:$user /home/$user
55+
56+
# Set working directory
57+
WORKDIR /var/www
58+
59+
# Copy contents of current directory to /var/www with correct permissions
60+
COPY --chown=$user:$user . /var/www
61+
62+
# Set ownership of /var/www to the system user
63+
RUN chown -R $user:$user /var/www
64+
65+
# Change current user to the system user
66+
USER $user
67+
68+
# Run composer install, generate laravel app key, and install npm packages
69+
RUN composer install && \
70+
php artisan key:generate && \
71+
npm install
72+
73+
# Expose port 9000 for PHP-FPM
74+
EXPOSE 9000
75+
76+
# Start PHP-FPM server
77+
CMD ["php-fpm"]

docker-compose.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
version: "3.7"
2+
services:
3+
app:
4+
# Build the image using the Dockerfile in the current directory.
5+
build:
6+
args:
7+
# Define arguments to pass to the Dockerfile.
8+
user: sammy
9+
uid: 1000
10+
context: ./
11+
dockerfile: Dockerfile
12+
# Give the image a name for reference in other parts of the compose file.
13+
image: laravel
14+
# Give the container a name for reference when running.
15+
container_name: laravel-app
16+
# Restart the container unless it is manually stopped.
17+
restart: unless-stopped
18+
# Set the working directory for the container.
19+
working_dir: /var/www/
20+
# Mount the local directory to the working directory in the container.
21+
volumes:
22+
- ./:/var/www
23+
# Connect the container to the "laravel" network.
24+
networks:
25+
- laravel
26+
27+
db:
28+
# Use the MySQL 8.0 image.
29+
image: mysql:8.0
30+
# Give the container a name for reference when running.
31+
container_name: laravel-db
32+
# Restart the container unless it is manually stopped.
33+
restart: unless-stopped
34+
# Map the local port 3306 to the container's port 3306.
35+
ports:
36+
- 3306:3306
37+
# Set environment variables for the database.
38+
environment:
39+
MYSQL_DATABASE: homestead
40+
MYSQL_ROOT_PASSWORD: secret
41+
MYSQL_PASSWORD: secret
42+
MYSQL_USER: homestead
43+
SERVICE_TAGS: dev
44+
SERVICE_NAME: mysql
45+
# Mount the local "initdb.d" and "data" directories to the corresponding directories in the container.
46+
# Mounting the database /var/lib/mysql makes the database persistant.
47+
# Any .sql files in the mysql/initdb.d/ folder will be automatically loaded into the database
48+
volumes:
49+
- ./docker-compose/mysql/initdb.d:/docker-entrypoint-initdb.d
50+
- ./docker-compose/mysql/data:/var/lib/mysql
51+
# Connect the container to the "laravel" network.
52+
networks:
53+
- laravel
54+
55+
nginx:
56+
# Use the alpine version of the nginx image.
57+
image: nginx:alpine
58+
# Give the container a name for reference when running.
59+
container_name: laravel-nginx
60+
# Restart the container unless it is manually stopped.
61+
restart: unless-stopped
62+
# Map the local port 8000 to the container's port 80.
63+
ports:
64+
- 8000:80
65+
# Mount the local directory, the "nginx" directory, and the "logs" directory to the corresponding directories in the container.
66+
volumes:
67+
- ./:/var/www
68+
- ./docker-compose/nginx:/etc/nginx/conf.d/
69+
- ./logs/:/var/log/nginx/
70+
# Connect the container to the "laravel" network.
71+
networks:
72+
- laravel
73+
# Wait for the "app" service to be up and running before starting this service.
74+
depends_on:
75+
- app
76+
77+
networks:
78+
laravel:
79+
# Use the bridge network driver.
80+
driver: bridge

docker-compose/mysql/.empty

Whitespace-only changes.

docker-compose/nginx/nginx.conf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
server {
2+
index index.php index.html;
3+
server_name app;
4+
error_log /var/log/nginx/error.log;
5+
access_log /var/log/nginx/access.log;
6+
root /var/www/public;
7+
8+
location / {
9+
try_files $uri $uri/ /index.php$is_args$args;
10+
}
11+
12+
location ~ \.php$ {
13+
try_files $uri =404;
14+
fastcgi_split_path_info ^(.+\.php)(/.+)$;
15+
fastcgi_pass app:9000;
16+
fastcgi_index index.php;
17+
include fastcgi_params;
18+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
19+
fastcgi_param PATH_INFO $fastcgi_path_info;
20+
}
21+
}

logs/.empty

Whitespace-only changes.

0 commit comments

Comments
 (0)