0

I have a PHP Laravel project. I'm new to Docker.

I have a Dockerfile created from php-apache image, which first installs some php packages, the php composer, copies all the files from my project to the docker image apache folder and here... as you can see below (see the Dockerfile) I have RUN composer install --optimize-autoloader --no-dev; \ ..., multiple lines of php artisan commands. For some unknown reason they don't work properly.

When I do docker-compose up -d, the php artisan key:generate --force (see the Dockerfile) doesn't add a value to APP_KEY in the .env file in the docker image (I've checked that with docker exec -it IMAGE_ID bash && cat .env after image was built and launched). Altough it says Application key set successfully.

Also, php artisan migrate fails to migrate the tables, it says:

In Connection.php line 664:

  SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_s  
  chema.tables where table_schema = homestead and table_name = migrations)     


In Connector.php line 70:

  SQLSTATE[HY000] [2002] Connection refused

But... after the image is built and launched, I do login to its bash -> docker exec -it IMAGE_ID bash, then I do php artisan migrate and it migrates all tables successfully. That's so weird..

Can you please help me figure out why Docker fails to:

  1. generate the app key?
  2. migrate?

This is my Dockerfile:

FROM php:7.2-apache

RUN apt-get update; \
    apt-get install -y zlib1g-dev git zip unzip; \
    docker-php-ext-install zip pdo_mysql;

COPY --from=composer:1.7.3 /usr/bin/composer /usr/bin/composer

WORKDIR /var/www

COPY ./ ./
COPY .env.example .env
COPY apache-config.conf /etc/apache2/sites-available/000-default.conf

RUN composer install --optimize-autoloader --no-dev; \
    php artisan key:generate --force; \
    php artisan migrate; \
    php artisan db:seed; \
    php artisan cache:clear; \
    php artisan config:cache; \
    php artisan route:cache; \
    php artisan storage:link; \
    chown -R www-data:www-data /var/www;

CMD ["apache2ctl", "-D" , "FOREGROUND"]

My apache-config.conf:

ServerName localhost

<VirtualHost *:80>
  ServerAdmin beqalomadze@gmail.com
  ServerName localhost
  DocumentRoot /var/www/public

  <Directory /var/www/public>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order deny,allow
      Allow from all
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

My docker-compose.yml

version: '3'

services:
  mysql:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: posts
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - "./data/mysql:/var/lib/mysql"

  app:
    depends_on:
      - mysql
    build:
      context: ./
      dockerfile: Dockerfile
    volumes:
      - "./data/storage-app:/var/www/storage/app"
    ports:
      - 3030:80

My .env.example:

...
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=posts
DB_USERNAME=root
DB_PASSWORD=root
...
Beqa
  • 101
  • 2
  • 14
  • Did you change the ENV variable DB_HOST and set it to `mysql`? – Simone Cabrino Jan 21 '19 at 14:45
  • @SimoneCabrino, yeah, please check out my question's end one more time, I've just updated it. – Beqa Jan 21 '19 at 14:50
  • 1
    The "build" step happens separately from the rest of the Docker Compose sequence. Inside the Dockerfile there is no database at all, and trying to run database migrations there won't work. – David Maze Jan 21 '19 at 15:15
  • @DavidMaze, wow, how interesting :) What would be a good solution in this situation? Where should I put that php artisan commands then? – Beqa Jan 21 '19 at 15:22
  • Possible duplicate of [Where to put the php artisan migrate command](https://stackoverflow.com/questions/48850813/where-to-put-the-php-artisan-migrate-command) – David Maze Jan 21 '19 at 15:27
  • The first answer to that question should work for you. I'd structure it slightly differently, but "in a script run at container startup" is the right answer. – David Maze Jan 21 '19 at 15:27
  • @DavidMaze Could you please write a correct answer down here? Would be highly appreciated. – Beqa Jan 21 '19 at 17:54

1 Answers1

0

You can try to add the port in your MYSQL conf :

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: posts
      MYSQL_ROOT_PASSWORD: root
    ports:
      - 3306:3306
    volumes:
      - "./data/mysql:/var/lib/mysql"
Vincent Decaux
  • 9,857
  • 6
  • 56
  • 84
  • 1
    Default user-defined bridge network, which is being created by `docker-compose` by default, exposes all ports inside network. So exposing port will have no effect inside network - only when you need to access service from outside – grapes Jan 21 '19 at 15:04