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:
- generate the app key?
- 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
...