I'm trying to make a Laravel project image(for local using at first) with the docker-compose. So, I made the following files:
docker-compose.yml:
version: '3.9'
networks:
laravel:
services:
nginx:
build:
context: .
dockerfile: docker/nginx/Dockerfile
container_name: nginx
ports:
- 8020:80
volumes:
- ./:/var/www/swdocker
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql
container_name: mysql
restart: always
volumes:
- ./docker/mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
networks:
- laravel
php:
build:
context: .
dockerfile: docker/php/Dockerfile
container_name: php
volumes:
- ./:/var/www/swdocker
- ./storage/app/public:/var/www/public/storage
#entrypoint: sh -c 'sleep 30 && php artisan migrate'
depends_on:
- mysql
networks:
- laravel
Dockerfile for nginx:
FROM nginx
ADD docker/nginx/conf.d /etc/nginx/conf.d
WORKDIR /var/www/swdocker
Dockerfile for php:
FROM php:8-fpm
RUN apt-get update && apt-get install -y \
&& docker-php-ext-install pdo_mysql
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
USER 1000
WORKDIR /var/www/swdocker
And the tuned default.conf file:
server {
listen 80;
server_name localhost;
root /var/www/swdocker/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
If I up the containers without migrations(they are commented) I need to run migrations manually(with docker-compose exec from terminal). And it works. Is it the best practice? I would like to up the project running only once docker image. I need to run artisan queue and scheduler for my project as well.
I tried to run migrations as entrypoint, but unsuccessfully. In this case I see my php container exits after migrations. I cannot understand how to solve this problem. Could anyone help me?