Use docker-compose to set up the Web Dev environment

Tram Ho

I. Introduction

Over the years, Docker has become a frequently used solution for quick deployment of applications by simplifying the running and deploying of applications in container (containers). When we use LEMP , for example, with PHP , Nginx , Mysql and Laravel framework , Docker can simplify the setup process.

Docker Compose further simplifies the development process by allowing developers to define their infrastructure, including application services , networks and networks ( networks ) volums ) in a single file. Docker Compose provides an effective alternative to running multiple docker container creation and docker container running commands.

In this tutorial, you will build a Laravel web application, with Nginx being the web server and MySQL as the database, all inside the Docker containers. You will define the entire configuration in the docker editor file, along with the configuration files for PHP, MySQL and Nginx.

II. Application building

1. Download Laravel and install the Dependencies

In this step, we install Laravel, you can go to Laravel’s doc to review how to install here

First, go to your working directory (here I choose /var/www ) and then proceed to clone the Laravel code with the command

Go to the newly created laravel-app directory

Next, use the Docker composer image to mount the folders you need in the project and avoid having to install Composer globally.

Using -v and -rm with the docker run creates containers, these containers will be connected to your current directory before being deleted. This will copy your laravel-app folder into the container and make sure that the vendor directory created inside is a copy of your root directory.

Finally, we grant permissions on the project directory with non-root user:

2. Create Docker Compose File

Building your applications with Docker Compose will help you to simplify the infrastructure setup. To establish Laravel application, we will file docker-compose to determine web server, database and application service.

In the laravel-app directory, we create the docker-composer.yml file with the following content:

Explain file docker-compose.yml 1 bit, here we identify there are 3 services: app , webserver and database

  • app : This definition contains the Laravel application and runs a custom Docker image : digitalocean.com/php , as shown in Step 4, it sets working_dir in the container to / var / www.
  • webserver : The definition of this service will pull the nginx: alpine image from docker and run on ports 80 and 443
  • db : The definition of the service is pulled from image mysql: 5.7.22 from Docker and has identified several environment variables including the database set for your application named laravel (this is the name database), along with the root password for the database, you can give another name to the database and set the password you want. This service will map port 3306 on the host to port 3306 on the container.
  • container_name : To determine the name of the container, corresponding to the name of the service, if you do not specify a name, Docker will assign a name to each container.
  • app-network : To create a connection between containers, services will be connected via app-network.
  • dbdata : is a volume with the contents of the / var / lib / mysql folder inside the container. This allows you to stop and start the db service without losing data, at the end of the docker-compose.yml file we also see dbdata, with this definition we can use this volume through many services.

3. Create Dockerfile

You create a file called Dockerfile and stored in ~ / laravel-app , the content of the file is as follows:

Explain the Dockerfile file a bit:

  • First, Dockerfile creates an image on the top of a Docker image, php: 7.2-fpm , this image is pre-installed with php-fpm.
  • RUN to perform the update, install and configuration within a container, including specifying the user and the www group
  • WORKDIR points to the working directory for the container application.
  • Creating a dedicated user and group with limited permissions will minimize the inherent vulnerability when running Docker containers, because the default is root . Instead of running this container as root, we created the user www , who has read / write access to /var/www via the COPY command and the – chown to copy the permissions of the application directory.
  • Finally, the EXPOSE command displays a port in the container, 9000, for the php-fpm server. CMD specifies the command to run when the container is created. Here, CMD specifies “php-fpm”, which will start the server.

4. Configure PHP

Now that you define the infrastructure via docker-compose.yml, you can configure the PHP service to handle requests from Nginx.

To configure PHP, you will create a local.ini file inside the ~ / laravel-app / php directory. This is the file you associated with /usr/local/etc/php/conf.d/local.ini inside the container in section 2. Creating this file will allow you to override the default php.ini file that PHP Read when it starts booting:

5. Configuring Nginx

To configure Nginx, create an app.conf file inside the ~ / laravel-app / nginx / conf.d / directory , with the following content:

6. Configure MySQL

Create my.cnf file inside ~ / laravel-app / mysql directory with content

7. Run the containers and correct the environment configuration

Now that you have defined all your services in docker-compose and created configuration files for these services, you can start the containers. However, as the final step, we will create the file. env from .env.example to define the environment in the Laravel application.

We will configure in detail when we start running the container.

For all services defined in the docker-compose file, you only need to issue a single command to start all containers, create volumes and set up and connect networks:

When you run docker-compose for the first time, it will download all the necessary Docker images. When the image is downloaded and stored in your local machine, Compose will create containers. -d flag to say that running containers is underground.

You can modify the .env configuration in the container with the example docker-compose exec command to open the .env file we use:

Also you run more

The results you will see are as follows:

Good luck ??? !

Source: https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose

Share the news now

Source : Viblo