Docker is a hot topic for software developers and it is in trend. It is an amazing tool for moving application and for setting up new environment. So in this article i will let you know about to create docker setup for Laravel application with Nginx and MySQL and Docker compose on Ubuntu 18.04 in easy steps.
Before start anything you should have ubuntu 18.04 machine with root privileges because you will need to execute multiple commands in this process.
Now we can start with the following steps:
First thing in the process of docker setup for an application we will have docker and docker compose installed on the machine in which we are working.
Update repository on your ubuntu machine with the command below:
sudo apt update
After updating the repository we will need to install docker and docker compose . Execute these two command for doing same
sudo apt install docker.io -y
sudo apt install docker-compose -y
After successfully executing above commands you will have installed docker and docker compose on your ubuntu machine. You can check if it is installed on your machine or not with the following commands
docker version
docker-compose version
After executing docker version command you will see docker client and server details with version.
Next, we will need to assign a non-root user to the docker group in order to run the Docker container for non-root users.
For the demo purpose i am taking user “testuser” you can choose your own. and execute this command
usermod -a -G docker testuser
And after that, log in to the ‘testuser’ user shell and run the docker ‘hello-world’ command like below
su - testuser
docker run hello-world
After running docker hello world, you will get hello world message from docker
Now your docker installation step has completed and docker has installed successfully in your ubuntu machine.
In this step first thing you will need to login into your system as a non root user then we will download laravel by cloning laravel from git repository.
Download laravel into myapp directory using the command below
git clone https://github.com/laravel/laravel.git myapp/
cd myapp/
After that run the following command to install Laravel dependencies
docker run --rm -v $(pwd):/app composer install
By executing the above command we have created a temporary docker container and mount the ‘myapp’ project directory to the ‘/app’ directory on the container. This container is based on composer docker image and here we are installing composer dependencies to the temporary container.
Now after successfully installation of dependencies we will need to change the owner of the myapp directory to our own user using the sudo command as mentioned below
sudo chown -R $USER:$USER ~/myapp
In the above steps we have installed docker in our ubuntu machine, download and installed laravel and its dependencies successfully, now in this third step we will dockerize to our laravel application.
So here we will create docker compose file and a docker file to our application’s root directory
cd myapp/
vim docker-compose.yml
Here i have used vim editor to edit docker-compose.yml file. You can choose your own editor which suits to you.
Now we will add some code to define laravel application, nginx, mysql into docker compose file as three different services. And the final code of docker-compose.yml file will be look a like below.
version: '3'
services:
#Laravel App
app:
build:
context: .
dockerfile: Dockerfile
image: hakase-labs/laravel
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www/html
volumes:
- ./:/var/www/html
networks:
- mynet
#Nginx Service
nginx:
image: nginx:alpine
container_name: nginx
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www/html
- ./nginx/conf.d/:/etc/nginx/conf.d/
- ./nginx/ssl/:/etc/nginx/ssl/
networks:
- mynet
#MySQL Service
db:
image: mysql:5.7
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laraveldb
MYSQL_USER: laraveluser
MYSQL_PASSWORD: laravel_db_password
MYSQL_ROOT_PASSWORD: mysql_root_password
volumes:
- mysqldata:/var/lib/mysql/
networks:
- mynet
#Docker Networks
networks:
mynet:
driver: bridge
#Volumes
volumes:
mysqldata:
driver: local
Virtual Host for Laravel application –
Now we will create nginx virtual host for our application. So let’s create nginx directory in myapp directory and two other directory under this nginx directory by running the following command.
mkdir -p nginx/{conf.d,ssl}
Now we will create laravel.conf file under conf.d directory and put following code into this file.
nginx/conf.d/laravel.conf
server {
listen 80;
server_name domain_name;
# Redirect all request to https
return 301 https://domain_url;
}
server {
listen 443 ssl http2;
server_name domain_name;
//SSL Certificate file
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privetkey.pem;
# Log files for Debug
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
# Laravel application's root directory
root /var/www/html/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
# Nginx Pass requests to PHP-FPM
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
};
Now copy the ssl files as mentioned above into nginx/ssl directory using this command
sudo cp /path/to/ssl/fullchain.pem nginx/ssl/
sudo cp /path/to/ssl/privkey.pem nginx/ssl/
Create Dockerfile
Now we will create Dockerfile to the laravel application’s root directory. And add following line of code into this file.
# Set master image for php
FROM php:7.2-fpm-alpine
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/html/
# Set working directory
WORKDIR /var/www/html
# Install Additional dependencies
RUN apk update && apk add --no-cache \
build-base shadow vim curl \
php7 \
php7-fpm \
php7-common \
php7-pdo \
php7-pdo_mysql \
php7-mysqli \
php7-mcrypt \
php7-mbstring \
php7-xml \
php7-openssl \
php7-json \
php7-phar \
php7-zip \
php7-gd \
php7-dom \
php7-session \
php7-zlib
# Add and Enable PHP-PDO Extenstions
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-enable pdo_mysql
# Install PHP Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Remove Cache
RUN rm -rf /var/cache/apk/*
# Add UID '1000' to www-data
RUN usermod -u 1000 www-data
# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www/html
# Change current user to www
USER www-data
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
Now we have completed application dockerizing step. And ready to create build.
Now we will create docker image build for our laravel application using the following docker compose command.
docker-compose build
After creating successful build, we will need to download all the docker images and run container services defined in cdocker-compose.yml file. So run following command.
docker-compose up -d
After that we can also check if container services are running or not using this command.
docker-compose ps
After completing 4th step you can check laravel application is runnig fine then why we need to do this step. Actually in this fifth step we will create .env file, database configuration and artisan key etc.
So run the command to copy .env file from .env.example file.
cp .env.example .env
docker-compose exec app vim .env
And we can put database and configuration details into this .env file.
Now we will need to generate artisan key, cache clearing, run migrations. So we can do these things through the following docker compose command.
docker-compose exec app php artisan key:generate
docker-compose exec app php artisan config:cache
docker-compose exec app php artisan migrate
After following all the steps above, you have completed docker setup for laravel application with nginx, php and mysql.
#docker #laravel #nginx #mysql