2

I try to run laravel in docker with external mysql. In docker-compose.yml I use depends_on for db. entrypoint.sh works perfectly with exec, but in Dockerfile ENTRYPOINT["/etc/entrypoint.sh"] does nothing.

My Dockefile

FROM php:7.4-fpm as build-stage

WORKDIR /app

RUN apt-get update && apt-get install -y nginx && apt-get install -y redis-server && apt install -y nodejs && apt install -y npm && apt-get install -y \
    libonig-dev libpq-dev \
    build-essential \
    libzip-dev \
    libpng-dev \
    libjpeg62-turbo-dev \
    libwebp-dev libjpeg62-turbo-dev libpng-dev libxpm-dev \
    libfreetype6 \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

RUN apt-get clean && rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-install pdo pdo_mysql mysqli mbstring zip exif pcntl

COPY ./nginx.conf /etc/nginx/conf.d/app.conf

COPY . /app

COPY nginx.conf /etc/nginx/sites-enabled/default
COPY entrypoint.sh /etc/entrypoint.sh

RUN chmod +x /etc/entrypoint.sh

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN cd /app && mkdir storage storage/framework && cd storage/framework && mkdir sessions views cache logs

RUN chmod -R 777 /app/storage/
RUN chmod -R 777 /app/public/images/
RUN mkdir -p /app/public/docs && chmod -R 777 /app/public/docs/

RUN cd /app && composer install

EXPOSE 80 443

ENTRYPOINT ["/etc/entrypoint.sh"]

docker-compose.yml

version: '3.2'

services:
  db:
    image: mysql:8
    restart: always
    container_name: laravel_db
    env_file:
      - .env
    ports:
      - "3307:3306"
    environment:
      MYSQL_DATABASE: db
      MYSQL_ROOT_PASSWORD: pass
      MYSQL_ROOT_HOST: '%'
    volumes:
      - ./public/docs/:/app/public/docs/
      - ./public/images/:/app/public/images/
      - ./mysql.cnf:/etc/mysql/conf.d/mysql.cnf
      - /var/lib/mysql/:/var/lib/mysql/

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: laravel_api
    restart: always
    volumes:
      - ./.env:/app/.env
    depends_on:
      - db
    env_file:
      - .env
    ports:
      - "9000:80"

entrypoint.sh

#!/usr/bin/env bash
service nginx start
service mysql start
php-fpm
service redis-server start

cd /app && php artisan migrate --force
cd /app && php artisan key:generate --force
cd /app && /usr/bin/npm install laravel-echo-server
cd /app && /usr/bin/npm install pm2
cd /app && /app/node_modules/pm2/bin/pm2 start laravel-echo-server.js
cd /app && php artisan passport:install

The below works. entrypoint.sh is executed

sudo docker-compose build --no-cache && sudo docker-compose up -d && sudo docker exec -it `sudo docker ps -q --filter ancestor=laravel-docker_app` /bin/bash -c '/etc/entrypoint.sh'

The below does not work. entrypoint.sh is not executed

sudo docker-compose build --no-cache && sudo docker-compose up -d

How I can debug it?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Are you sure it does something? This image does not ship with `systemd` nor `init system`, so I really doubt `service nginx start` would work or do anything other than crash. `service mysql start` would not work either, and since you have a dedicated container for it, it seems strange to me. Have you tried doing `docker-compose logs -f` to see the errors coming? – β.εηοιτ.βε Jan 11 '21 at 21:28
  • 2
    To a first approximation `service` and similar commands don't work in Docker at all. Your `docker-compose.yml` already launches MySQL in a separate container; you should launch Nginx and Redis in separate containers as well. Why do you think the script isn't running? (Does the script run everything up through `php-fpm`, and then block waiting for the FPM server to complete?) – David Maze Jan 11 '21 at 21:55
  • i found my mistake.php-fpm run as foreground proccess, then another commands do not running. moving php-fpm to bottom script resolved problem. thx p.s. i remove line with mysql service. previously mysql was started inside a container with a laravel, and then it was taken out separately – Дмитрий Фомин Jan 11 '21 at 22:28

0 Answers0