Skip to content

Instantly share code, notes, and snippets.

@hmbilal
Created December 4, 2021 13:11
Show Gist options
  • Save hmbilal/1da71acc9f0e303347eba038a4dbd5d2 to your computer and use it in GitHub Desktop.
Save hmbilal/1da71acc9f0e303347eba038a4dbd5d2 to your computer and use it in GitHub Desktop.
The most simple php 8, mysql 8, nginx docker setup.

Notes

  • You can replace dockerproject with whatever project namespace you are using.
  • Be sure to use same value for network in docker-compose.
  • In docker-compose files are referenced in an existing directory docker.
    • Make sure to create these files inside docker directory on same level of docker-compose.yml file and replace - with subdirectory from this gist. For exmaple:
      • Create docker/nginx/dockerproject.conf file to use docker-nginx-dockerproject.conf.
  • Make sure that the root path matches public directory of your framework/code that will serve the fronte page of application.
  • Access mysql database on port 3307 assuming you might already have a local instance running at 3306.
  • Access application in your browser at port 8001. You can change this under nginx section in docker-compose file.
FROM php:8.0-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Set working directory
WORKDIR /var/www
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# Install system dependencies
RUN apt-get update \
&& apt-get install -y git curl libpng-dev libonig-dev libxml2-dev zip unzip gnupg \
&& curl -sL https://deb.nodesource.com/setup_16.x | bash - \
&& apt-get install -y nodejs yarn mariadb-client
# Clear cache
RUN apt-get autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Install php packages
RUN pecl channel-update https://pecl.php.net/channel.xml \
&& pecl install swoole \
&& pecl clear-cache \
&& rm -rf /tmp/* /var/tmp/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
USER $user
version: "3.7"
services:
app:
build:
args:
user: dockerproject
uid: 1000
context: ./
dockerfile: ./docker/app/Dockerfile
image: php:alpine
container_name: dockerproject
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
depends_on:
- db
networks:
- docker-project
db:
image: 'mysql:8.0'
container_name: dockerproject-db
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: dockerproject
MYSQL_USER: docker
MYSQL_PASSWORD: docker
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
ports:
- 3307:3306
volumes:
- ./docker/database/data:/var/lib/mysql
networks:
- docker-project
nginx:
image: nginx:alpine
container_name: dockerproject-nginx
restart: always
ports:
- "8001:80"
volumes:
- ./:/var/www
- ./docker/nginx:/etc/nginx/conf.d
links:
- app:dockerproject
networks:
- docker-project
depends_on:
- app
networks:
docker-project:
driver: bridge
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass dockerproject: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 / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment