Where to put the php artisan migrate command

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Title: Executing PHP Artisan Migrate Command in Dockerized Laravel Applications

Body:

In developing Laravel applications using Docker, managing database migrations is a crucial aspect of the process. The php artisan migrate command is used to generate the required tables in your chosen database system. But sometimes you may be confused about where to put this important step in the Docker setup.

First, let's understand how Laravel's migration feature works. The migrate command executes all migrations defined within Laravel's database/migrations directory. It automatically creates new tables, modifies existing ones, or removes them based on the commands contained in each migration file.

Docker-Compose

Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML configuration file to define services, their dependencies, and other configurations. To run the Laravel application with MySQL database in Docker, you can use the following config:

php
version: '3.3'<p>services:<br>    db:<br>        image: mysql:5.7<br>        networks:<br>            - smstake<br>        ports:<br>          - "3306:3306"<br>        volumes:<br>          - db_data:/var/lib/mysql<br>        environment:<br>            MYSQL_ROOT_PASSWORD: password<br>            MYSQL_DATABASE: smstake<br>            MYSQL_USER: root<br>            MYSQL_PASSWORD: password<br>        deploy:<br>            mode: replicated<br>            placement:<br>                constraints:<br>                    - node.role == manager</p>
<pre><code>app:
    image: smstake:latest
    ports:
      - 8000:80
    networks:
        - smstake
    command: docker-compose exec app php artisan migrate --seed
    deploy:
        mode: replicated
        replicas: 1
        placement:
            constraints:
                - node.role == manager

volumes:
db_data:

The command docker-compose exec app php artisan migrate --seed runs the Laravel's migration inside the application container, ensuring that all necessary tables are generated in the database.

Dockerfile

When building a custom image for your application, you can define the PHP artisan migrate command during the Docker build process. However, as shown above, it is more flexible and efficient to run this command within the production environment:

php
FROM alpine<p>ENV <br>    APP_DIR="/app" <br>    APP_PORT="80"</p>
<h1>the "app" directory (relative to Dockerfile) contains your Laravel app...</h1>
<p>COPY app/ $APP_DIR</p>
<h1>or we can make the volume in compose to say use this directory</h1>
<p>RUN apk update && <br>    apk add curl <br>    php7 <br>    php7-opcache <br>    php7-openssl <br>    php7-pdo <br>    php7-json <br>    php7-phar <br>    php7-dom <br>    php7-curl <br>    php7-mbstring <br>    php7-tokenizer <br>    php7-xml <br>    php7-xmlwriter <br>    php7-session <br>    php7-ctype <br>    php7-mysqli <br>    php7-pdo <br>    php7-pdo_mysql<br>    && rm -rf /var/cache/apk/*</p>
<p>RUN curl -sS <a href="https://getcomposer.org/installer">https://getcomposer.org/installer</a> | php -- <br>  --install-dir=/usr/bin --filename=composer</p>
<p>RUN cd $APP_DIR && composer install</p>
<p>WORKDIR $APP_DIR</p>
<p>RUN chmod -R 775 storage<br>RUN chmod -R 775 bootstrap</p>
<p>#CMD php artisan migrate:fresh<br>CMD php artisan serve --host=0.0.0.0 --port=$APP_PORT<br>

While you can add the php artisan migrate:fresh command to your Dockerfile, as it's commented out in the example, there is a risk that this could leave you with inconsistent database states. Instead, use the compose file command execution to perform migrations after starting the application.

Jenkins and Continuous Integration

In your continuous integration pipeline using tools like Jenkins or GitLab, you can run commands on running services. To migrate the database in a Docker environment, you could execute:

php
docker-compose up -d --force-recreate --build<p>#Running commands on already running service<br>docker-compose exec -T app php artisan migrate:fresh --seed --force<br>

When adding this command to your pipeline, ensure that it is run only after all migrations have been completed and tested. This practice ensures a consistent database state across different environments.

Conclusion

To run Laravel's migration commands in a Dockerized application, use the migrate command within the production environment via compose or manually using the shell. This approach ensures that your database remains consistent and provides better control over migration execution.