Hello,
I have a Laravel project and I want to run it in a container.
The startup.sh
file is:
#!/bin/sh
set -e
echo "Waiting for the database to be ready..."
while ! nc -z db 3306; do
sleep 1
done
echo "Database is ready!"
composer install --no-dev --no-scripts
php artisan package:discover
# Start PHP-FPM
exec php-fpm
The Dockerfile
is:
FROM php:fpm-alpine3.20
WORKDIR /var/www/html
RUN apk update && apk add --no-cache \
libzip-dev \
zip \
unzip \
libpng-dev \
libjpeg-turbo-dev \
freetype-dev \
oniguruma-dev \
curl \
git \
mysql-client \
autoconf \
dpkg-dev \
dpkg \
file \
g++ \
gcc \
libc-dev \
make \
re2c
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd pdo_mysql zip mbstring
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
COPY . .
COPY startup.sh /usr/local/bin/startup.sh
RUN chmod +x /usr/local/bin/startup.sh
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
EXPOSE 9000
ENTRYPOINT ["startup.sh"]
The docker-compose.yml
is:
services:
app:
build:
context: .
dockerfile: Dockerfile
container_name: laravel_app
restart: unless-stopped
volumes:
- .:/var/www/html
networks:
- laravel_network
depends_on:
- db
environment:
DB_CONNECTION: mysql
DB_HOST: db
DB_PORT: 3306
DB_DATABASE: laravel_db
DB_USERNAME: laravel_user
DB_PASSWORD: secret
db:
image: mysql:latest
container_name: laravel_db
restart: unless-stopped
environment:
MYSQL_DATABASE: laravel_db
MYSQL_USER: laravel_user
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: rootsecret
TZ: UTC
volumes:
- mysql_data:/var/lib/mysql
networks:
- laravel_network
deploy:
resources:
limits:
memory: 1G # Increase memory limit
cpus: '1.0' # Allocate CPU
phpmyadmin:
image: phpmyadmin:latest
container_name: laravel_phpmyadmin
restart: unless-stopped
ports:
- "8081:80"
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: rootsecret
depends_on:
- db
networks:
- laravel_network
volumes:
mysql_data:
driver: local
networks:
laravel_network:
driver: bridge
And the .env
file contains the following section:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=secret
When I run the containers I get the following error:
Installing dependencies from lock file
Verifying lock file contents can be installed on current platform.
Nothing to install, update or remove
Generating optimized autoload files
55 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
In Connection.php line 825:
SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select *
from `permissions` where `permissions`.`deleted_at` is null)
In Connector.php line 67:
SQLSTATE[HY000] [2002] Connection refused
Installing dependencies from lock file
Verifying lock file contents can be installed on current platform.
Nothing to install, update or remove
Generating optimized autoload files
55 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
In Connection.php line 825:
SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select *
from `permissions` where `permissions`.`deleted_at` is null)
In Connector.php line 67:
SQLSTATE[HY000] [2002] Connection refused
Installing dependencies from lock file
Verifying lock file contents can be installed on current platform.
Nothing to install, update or remove
Generating optimized autoload files
55 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
In Connection.php line 825:
SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select *
from `permissions` where `permissions`.`deleted_at` is null)
In Connector.php line 67:
SQLSTATE[HY000] [2002] Connection refused
Why?
Thank you.