I am trying to run php artisan migrate
on docker compose but this error occur in my mysql container:
sh: php: command not found
docker-compose.yml
version: "3"
services:
# Application & web server
app:
build:
context: .
working_dir: /var/www
volumes:
- ./:/var/www
depends_on:
- "mysql"
ports:
- 8001:80
mysql:
image: mysql:latest
volumes:
- dbdata:/var/lib/mysql
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
ports:
- 3306:3306
entrypoint: sh -c "sleep 30 && php artisan migrate"
phpmyadmin:
image: phpmyadmin:latest
ports:
- 9001:80
environment:
- PMA_ARBITRARY=1
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
Docker file
FROM php:8.0-apache
RUN apt-get update && apt-get install -y
libfreetype6-dev
libjpeg-dev
libpng-dev
libwebp-dev
--no-install-recommends
&& docker-php-ext-enable opcache
&& docker-php-ext-configure gd --with-freetype --with-jpeg
&& docker-php-ext-install pdo_mysql -j$(nproc) gd
&& apt-get autoclean -y
&& rm -rf /var/lib/apt/lists/*
# Update apache conf to point to application public directory
ENV APACHE_DOCUMENT_ROOT=/var/www/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
# Update uploads config
RUN echo "file_uploads = Onn"
"memory_limit = 1024Mn"
"upload_max_filesize = 512Mn"
"post_max_size = 512Mn"
"max_execution_time = 1200n"
> /usr/local/etc/php/conf.d/uploads.ini
# Enable headers module
RUN a2enmod rewrite headers
2
Answers
Your mysql container does not have PHP installed.
You need to run the command in your "app" container.
The
mysql
image only contains the database. Theentrypoint:
setting says "run this command instead of what the container would have normally run, ignoring even its default setup". Since you’ve attached theentrypoint:
to thedatabase:
, you try to runphp artisan migrate
instead of running MySQL, and you run it in a database-only image.The migration step needs to be attached to your application container, not the database container. I tend to write this as an entrypoint wrapper script, a script compiled into the image that runs as the image’s default
ENTRYPOINT
but that accepts a normalCMD
as the actual main container process. The simplest form of this would look likeThe one complication here is that setting
ENTRYPOINT
in a Dockerfile also resets theCMD
, and thephp:apache
images come with a defaultCMD
. If you look at the Docker Hubphp
image page and then click through to the "Dockerfile
links" page, you can find the official Dockerfile forphp:8.0-apache
. If you look at the end of that file, it has both anENTRYPOINT
and aCMD
, so you need to replicate both of these in your setup.Now in your Compose setup, you don’t need to set a
command:
orentrypoint:
for any of these containers, or mount the application code into the container; all of these details are already set in the image.