Create a Docker container that contains NGINX and mariaDB

In order to create a docker container, we must first know that it is a docker because in recent years, Docker has become a frequently used solution for deploying applications thanks to the way they simplify the operation and deployment of containerized applications. When using a LEMP application stack, for example, with PHP, Nginx, MySQL, and the Laravel framework, Docker can greatly simplify the configuration process.

Docker Compose has further simplified the development process by allowing developers to define their infrastructure, including application services, networks, and volumes, in a single file. Docker Compose offers an efficient alternative to running various commands such as: docker container create and docker container run.

For this reason, I inform you that the easiest way to create Docker containers that contain the Nginx web server and PHP installed together with the extensions we need and other common services in the development of web applications, is by using the PhpDocker.io container generator.

Upon entering the page, it shows us a brief description of the service:

PhpDocker.io is a tool that helps you build the typical PHP development environment in a Docker container with just a few clicks. It supports the most common services (MySQL / MariaDB, Redis, Elasticsearch…), and more to come. It supports PHP 7.1, in addition to version 7.0 and 5.6.

To generate our container, we click on the Generator link.

In the first block, we select the basic configuration of our container:

  • Project name: The name of our project.
  • Base port: The port where our container will be executed.
  • Application type: The PHP application type (Generic, Symfony, Phalcon 3, Silex). In this option, if for example we choose Symfony, it will configure Nginx to work with a Symfony application (linking the app.php file as a front controller, etc.).
  • Max upload size (MB): Size limit per file for uploads to the server.

In the second block, we must specify the version we want of PHP and the extensions we need.

Below, the generator allows us to add other services to our multiple container. These services are MySQL, MariaDB, Postgres, Elasticsearch, Memcached, Redis, and Mailhog.

And finally, we click on the Generate project archive button, which will generate a .zip file that includes the following:

  • docker-compose.yml
  • phpdocker: This folder includes the services that we have added, together with their configuration files.
  • Readme.html, An HTML file where it tells us how to run our container.
  • Readme.md

I have created a repository on GitHub where you can see how I have configured a project with Symfony using Nginx and PHP-FPM 7.1.

Also, inside the PHP-FPM container I have installed the PHP MySQL and LDAP extensions along with GIT and Composer.

This way you will create a web application using Laravel framework with Nginx as web server and MySQL as database; all within Docker containers. You will define all the stack configuration in a docker-compose file, along with the configuration files for PHP, MySQL, and Nginx.

Before you begin, you will need the following:

• An Ubuntu 18.04 server and a non-root user with sudo privileges. Follow each of these steps to follow. Initial setup for Ubuntu 18.04 servers to prepare for this.

• Docker installed.

• Docker Compose installed.

Step 1: Download Laravel and install dependencies

As a first step, we will get the latest version of Laravel and install the project dependencies. We will include Composer, the application level package manager for PHP. We will install these dependencies with Docker to avoid installing Composer globally.

First, check that it is in your home directory, clone the latest version of Laravel and put it in a directory called laravel-app:

cd ~
git clone https://github.com/laravel/laravel.git laravel-app

Position yourself in the laravel-app directory:

cd ~/laravel-app

Next, use the Docker composer image to mount the directories you will need for your Laravel project and avoid the overhead of installing Composer globally:

docker run --rm -v $ (pwd):/app composer install

 

Using the -v and –rm flags with docker run, an ephemeral container is created for which a bind mount is applied to the current command list before it is removed. This will copy the contents of your ~/laravel-app directory to the container, and also ensure that the vendor folder created by Composer inside the container is copied to your current directory.

As a final step, set permissions on the project directory to be owned by your non-root user:

sudo chown -R $ USER: $ USER ~/laravel-app

 

This will be important when writing the Dockerfile for your application image in step 4, as it will allow you to work with the application code and run processes in your container as a non-root user.

Once your application code is established, you can proceed to define your services with Docker Compose.

Step 2: Create the Docker Compose file

Developing your applications with Docker Compose simplifies the process of configuring and versioning your infrastructure. To configure our Laravel application, we will write a docker-compose file that defines our web server, our database, and our application services.

Open the file:

nano ~/laravel-app/docker-compose.yml

 

In the docker-compose file, you will define three services: app, web server and db. To add the following code to the file, be sure to replace the root password of MySQL_ROOT_PASSWORD, defined as an environment variable under the db service, with a strong password of your choice:

~/laravel-app/docker-compose.yml

version: ‘3’

services:

#PHP Service

app:

build:

context: .

dockerfile: Dockerfile

image: digitalocean.com/php

container_name: app

restart: unless-stopped

tty: true

environment:

SERVICE_NAME: app

SERVICE_TAGS: dev

working_dir: /var/www

networks:

- app-network

#Nginx Service

webserver:

image: nginx:alpine

container_name: webserver

restart: unless-stopped

tty: true

ports:

- "80:80"

- "443:443"

networks:

- app-network

#MySQL Service

db:

image: mysql:5.7.22

container_name: db

restart: unless-stopped

tty: true

ports:

- "3306:3306"

environment:

MYSQL_DATABASE: laravel

MYSQL_ROOT_PASSWORD: your_mysql_root_password

SERVICE_TAGS: dev

SERVICE_NAME: mysql

networks:

- app-network

#Docker Networks

networks:

app-network:

driver: bridge

The services defined here include the following:

• app: This service definition contains the Laravel application and runs a custom Docker image. It also sets / var / www to working_dir in the container.

# Install extensions

RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl

RUN docker-php-ext-configure gd --with-gd --with-freetype-dir = /usr/include/ --with-jpeg-dir = /usr/include/ --with-png-dir = /usr/include/

RUN docker-php-ext-install gd

# Install composer

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

# Add user for laravel application

RUN groupadd -g 1000 www

RUN useradd -u 1000 -ms /bin/bash -g www www

Anyway. With this, you will have a LEMP stack application running on your server, which you tested by accessing the Laravel welcome page and creating MySQL database migrations.

The key to simplicity in this installation is Docker Compose, which allows you to create a group of Docker containers defined in a single file using one command.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.