How to run laravel app inside docker container?

by darrion.kuhn , in category: PHP Frameworks , 4 months ago

How to run laravel app inside docker container?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 3 months ago

@darrion.kuhn 

To run a Laravel app inside a Docker container, follow these steps:

  1. Create a Dockerfile in the root directory of your Laravel application. The Dockerfile should include the following code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Use the official PHP image as the base image
FROM php:7.4-fpm

# Set the working directory inside the container
WORKDIR /var/www/html

# Install dependencies
RUN apt-get update && apt-get install -y     git     zip     unzip

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Copy the Laravel application files to the container
COPY . .

# Install Laravel dependencies
RUN composer install

# Expose port 8000 to access the Laravel app
EXPOSE 8000

# Run the Laravel application
CMD php artisan serve --host=0.0.0.0 --port=8000


  1. Build the Docker image using the following command:
1
docker build -t my-laravel-app .


  1. Run the Docker container using the following command:
1
docker run -p 8000:8000 my-laravel-app


  1. Access the Laravel application running inside the Docker container by navigating to http://localhost:8000 in your web browser.


Your Laravel app should now be up and running inside a Docker container.