DEV Community

Pierangelo
Pierangelo

Posted on

Dockerize an existing Laravel application with docker-compose

in the main folder of your Laravel app, create a file named Dockerfile and insert this code:

FROM php:7 RUN apt-get update -y && apt-get install -y openssl zip unzip git RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN docker-php-ext-install pdo pdo_mysql WORKDIR /app COPY . /app RUN composer install CMD php artisan serve --host=0.0.0.0 --port=8181 EXPOSE 8181 
Enter fullscreen mode Exit fullscreen mode

In the same main folder of Dockerfile, create a file named docker-compose.yml and insert this code:

version: '2' services: app: build: . ports: - "8009:8000" volumes: - .:/app env_file: .env working_dir: /app command: bash -c 'php artisan migrate && php artisan serve --host 0.0.0.0' depends_on: - db links: - db db: image: "mysql:5.7" environment: - MYSQL_ROOT_PASSWORD=yourpassword - MYSQL_DATABASE=yourdbname - MYSQL_USER=root - MYSQL_PASSWORD=yourpassword volumes: - ./data/:/var/lib/mysql ports: - "3306:3306" phpmyadmin: depends_on: - db image: phpmyadmin/phpmyadmin restart: always ports: - 8090:80 environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: yourpassword 
Enter fullscreen mode Exit fullscreen mode

Open the terminal command line and go inside the laravel folder, and launch this commands:

docker.compose build

docker-compose up -d

if have need to create and migrate the db, or use other commands, launch the Laravel commands in this way:
docker-compose run app php artisan

The app will available at the address http://0.0.0.0:8009

Top comments (6)

Collapse
 
hamzarana07 profile image
Hamza Rana

Warning: require(/app/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /app/bootstrap/autoload.php on line 17

Fatal error: require(): Failed opening required '/app/bootstrap/../vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /app/bootstrap/autoload.php on line 17
ERROR: 255

Help please. Tried these dockerfile and docker-compose.yml on 2 different laravel projects to no avail.

Collapse
 
pierangelo1982 profile image
Pierangelo

this tutorial was based on old version of laravel, what version are you using?
Probably you must update in Dockerfile some version of PHP etc...

Collapse
 
hamzarana07 profile image
Hamza Rana

8.56 Laravel. I was able to fix this issue by adding "composer install" in Dockerfile RUN command. Thanks

Collapse
 
kkr77 profile image
KKR77

I need to dockerize my Laravel 4.1 Project with PHP 5.6 and MySQL 5.7.20. when I tried using above docker file and compose.yml file , it ended with this error message,

failed to solve: process "/bin/sh -c composer install" did not complete successfully: exit code: 1

how should I solve this..?

Collapse
 
pierangelo1982 profile image
Pierangelo

probably tou need to use an Image with php5.6 or build a new one using ubuntu and installing php5.6... something like that:

`

Use an official PHP 5.6 base image

FROM php:5.6-apache

Install required PHP extensions

RUN apt-get update && \
apt-get install -y \
libmcrypt-dev \
mysql-client \
libmagickwand-dev --no-install-recommends && \
pecl install imagick && \
docker-php-ext-enable imagick && \
docker-php-ext-install pdo_mysql mcrypt

Install Composer

RUN curl -sS getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

Set up the working directory

WORKDIR /var/www/html

Copy the Laravel application files

COPY . /var/www/html

Install Laravel dependencies

RUN composer install --no-scripts

Set proper permissions

RUN chown -R www-data:www-data /var/www/html/storage

Expose port 80 and start Apache

EXPOSE 80
CMD ["apache2-foreground"]
`

Collapse
 
kkr77 profile image
KKR77

Thank you