Step 1: Download Laravel to your local machine
If you don’t have Laravel in your local machine, install it first by cloning it by git command.
git clone https://github.com/laravel/laravel.git laravelapp
Step 2 : Create Docker Compose File
After installing Laravel in your local machine, proceed to your Laravel project directory by typing:
$ cd laravelapp
You can open you Visual Studio Code so you can see the file structure of Laravel and to observe what is the purpose of the following commands.
To create docker-compose.yml and its related directory files, type the following commands in terminal:
$ cp .env.example .env $ touch docker-compose.yml $ mkdir .docker
Go to .docker directory and create required files by typing:
$ cd .docker
$ touch Dockerfile
$ touch vhost.conf
Open Visual Studio Code and go to Laravel app. Navigate to the files you created earlier then copy and paste the following then save.
FROM php:7.4.1-apache USER root WORKDIR /var/www/html RUN apt-get update && apt-get install -y \ libpng-dev \ zlib1g-dev \ libxml2-dev \ libzip-dev \ libonig-dev \ zip \ curl \ unzip \ && docker-php-ext-configure gd \ && docker-php-ext-install -j$(nproc) gd \ && docker-php-ext-install pdo_mysql \ && docker-php-ext-install mysqli \ && docker-php-ext-install zip \ && docker-php-source delete COPY .docker/vhost.conf /etc/apache2/sites-available/000-default.conf RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN chown -R www-data:www-data /var/www/html \ && a2enmod rewrite
The docker-compose.yml should look like the below example. Copy and paste the following then save (CTRL + S):
version: '3.7' services: app: build: context: . dockerfile: .docker/Dockerfile image: 'laravelapp' ports: - 8080:80 volumes: - ./:/var/www/html db: image: mysql:5.7 restart: always ports: - "3307:3306" environment: MYSQL_DATABASE: 'laraapp_db' MYSQL_ALLOW_EMPTY_PASSWORD: 1 MYSQL_ROOT_PASSWORD: "" volumes: - ./db:/var/lib/mysql
Under the .docker directory, open vhost.conf and paste the following then save:
<VirtualHost *:80> DocumentRoot /var/www/html/public <Directory "/var/www/html"> AllowOverride all Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Step 3: Start Laravel Application with Docker Compose
Now that you saved the changes in the required files, go to your terminal then type:
$ docker-compose up -d
If it results in error, try putting sudo before it:
$ sudo docker-compose up -d
Check it is it running by typing the following. If it doesn’t show, try putting sudo before it.
$ docker ps
Access the hosting machine through the container id i.e. 83ad4ef2b5dc as per the screenshot by typing the following. Try adding sudo if it doesn’t work and make sure to change the container ID.
$ docker exec -it container_id bash
Then type ls -la to check its existing files. install composer by typing the following and wait for it to finish:
# composer install
After the installation, type EXIT. Open your browser then navigate your localhost with port 8080.
localhost:8080
If it shows you the Laravel page, you successfully containerized your Laravel app. But if it shows the following error, proceed to next steps.
In your terminal, type the following:
$ sudo docker-compose down $ sudo chmod 666 /var/run/docker.sock
Then run the laravelapp containers by typing the following without using sudo:
$ docker-compose up -d
Type again in your browser your localhost with port 8080.
localhost:8080
Step 4: Laravel Database Connection in Docker
Now we have to connect the database to our Laravel application. Which is pretty simple though the .env
file. Go to Visual Studio Code and change the following according to database we created earlier.
Go to your terminal and type:
$ docker exec -it container_id bash
Then migrate by typing:
# php artisan migrate # exit
You have now successfully installed Laravel in a docker container.
When running composer install, I get an error about an outdated version of PHP. New version of laravel requires 8.1 not 7.4