The one thing about setting up a container for Laravel on the internet is a lot of plugins or scripts like laradock.io or others.
But sometimes we need something simpler and more minimal, to use services like web and database.
If you have a docker installed then let's get to work, if you don't, install it, see the link.
We create a file in the root folder of the project and call it docker-compose.yml:
version: '3'
services:
web:
environment:
- APACHE_RUN_USER=#1000
build:
context: ./docker/web
ports:
- 10000:80
volumes:
- ./:/var/www/html
database:
image: mariadb
restart: always
ports:
- 33061:3306
environment:
MYSQL_ROOT_PASSWORD: 123
volumes:
- ./docker/database:/var/lib/mysql
So, we have two services: web and database.
For web service we set the port 10000, meaning it will be available at the address http://127.0.0.1:10000, you can change it whenever you want, just check that it is not busy in your system.
Now, let's create a folder in the root folder of the project and call it docker
Inside this folder we will create 2 folders: database and web:
- the database folder is for saving all data from container
- the web folder contains configuration script for running the apache server
So, inside the web folder create a file default.conf
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Then create a new file without extension and call it Dockerfile
FROM php:7.2-apache
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libpng-dev \
libwebp-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libzip-dev \
zip \
git \
mysql-client \
&& docker-php-ext-install \
pdo_mysql \
gd \
zip \
&& a2enmod \
rewrite
# Add the user UID:1000, GID:1000, home at /app
RUN groupadd -r app -g 1000 && useradd -u 1000 -r -g app -m -d /app -s /sbin/nologin -c "App user" app && \
chmod 755 /var/www/html
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
#upload
RUN echo "file_uploads = On\n" \
"memory_limit = 500M\n" \
"upload_max_filesize = 500M\n" \
"post_max_size = 500M\n" \
"max_execution_time = 600\n" \
> /usr/local/etc/php/conf.d/uploads.ini
USER app
WORKDIR /var/www/html
USER root
COPY default.conf /etc/apache2/sites-enabled/000-default.conf
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
EXPOSE 80
A summary with reference to what is in this file:
- I uploaded the php: 7.2-apache image, then installed some libraries for basic php.
- We also install php composer to be able to run laravel commands from the container.
- We configure php.ini and set max_upload = 500MB in case the application is in development, if it is production then change the value you need.
In your terminal execute:
docker-compose up --build
*Parameter --build uses it only the first time or if you made any changes to one of the created files.
If you did not receive errors in the terminal, then your application is available at: http://127.0.0.1:10000
But do not rush, we need to configure the settings for the database in the .env file from Laravel project.
DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=inovector
DB_USERNAME=root
DB_PASSWORD=123
Now you can open your application in browser: http://127.0.0.1:10000
To execute Laravel commands, you must log in to the container:
docker-compose exec --user app web bash