1

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?

1 Answer 1

2

what I normally do is to put all the commands I want to run in a bash script file, and execute this file .this also helped me when I wanted to create a basic CI/CD pipeline

docker-compose down --remove-orphans
docker-compose build //in case I changed docker-compose file
docker-compose up -d

docker exec {container-name} bash -c "composer update"
docker exec {container-name} bash -c "php artisan migrate"

as for the schedulerphp artisan schedule run,the most straight forward way I found was to add

docker exec {container-name} bash -c "php artisan schedule:run" >> /home/{user}/output.txt

where output.txt is just a file that will show you the output of the command.

Hope this would be of any help to you.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer! I tried this way. I mean using shell script. But when I try to up my project I can see some problems with DB. I think there is something wrong with user permissions: Illuminate\Database\QueryException SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel1 and table_name = migrations and table_type = 'BASE TABLE')
docker-compose up --build -d docker exec php bash -c "php artisan migrate" docker exec php bash -c "php artisan queue:work" Migrations don't run
Thanks so very much. After a day of trying I can finally move on to coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.