Detail building the LNMP environment using Docker

  • 2020-06-12 11:15:33
  • OfStack

Configure a simple LNMP environment

Here, docker-ES6en is used. The configuration is as follows:


docker-compose.yml
version: "2"
services: 
 # Nginx 1.11.10: https://hub.docker.com/_/nginx/
 web:
  image: nginx:1.11.10
  ports:
   #  The host of  8080  Port mapped to the container  80  port 
   - 8080:80
  depends_on:
   - php
  #  with  php 1 The Shared  ./apps  directory 
  volumes_from:
   - php
  #  create 1 A data volume, by  ./images/nginx/config => /etc/nginx/conf.d
  volumes:
   - ./images/nginx/config:/etc/nginx/conf.d:ro

 # PHP 7.1-fpm: https://hub.docker.com/_/php/
 php:
  image: php:7.1-fpm
  volumes: 
   - ./apps:/mnt/apps

 # MySQL 5.7: https://hub.docker.com/_/mysql/
 database: 
  image: mysql:5.7
  #  configuration 1 Some environment variables, the details of the environment variables can be visited at the above url 
  environment:
   MYSQL_ROOT_PASSWORD: "root"
   MYSQL_USER: "dailybird"
   MYSQL_PASSWORD: "dailybirdo"
  volumes:
   - ./database:/var/lib/mysql

default.conf

in docker-compose.yml , the configuration directory for Nginx has been placed /etc/nginx/conf.d Map to ./images/nginx/config Directory. According to the configuration of Nginx, all are in this directory .conf The files will all be configuration files, so we can create a new one default.conf Configure.


server{
  listen     80;
  server_name localhost;
  root     /mnt/apps;
  index     index.php index.html index.htm;

  location / {
    index    index.php index.html;
    try_files   $uri $uri/ /index.php?$query_string;
  }

  location ~ \.php$ {
    #  Used here  php  As an internal domain name connection  php  The container 
    fastcgi_pass   php:9000;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include     fastcgi_params;
  }
}

Create a container

in docker-compose.yml To create a container group that supports the LNMP service:


docker-compose up -d

After that, Docker creates one bridge Type of network and connect the three containers to the network.

Moreover, as a result of docker-compose.yml Data volume configuration, in the current directory, is also created database Directory for mapping database file directories; create apps Directory for web applications; create /etc/nginx/conf.d 0 Directory mapping Nginx profile directory.

test

in docker-compose.yml In, we did 8080 = > Port mapping for 80; In addition, in ES49en.conf, the Nginx request is directed to the container /mnt/apps Directory while the directory is associated with the host ./apps The directory forms a map so we can go through it localhost:8080 Access the services to the Nginx container. Of course, you should also be aware of host port usage, if necessary docker-compose.yml Change port 8080 to something else.

At this point, we just need to be in ./apps Create a simple PHP file in the directory, such as test.php And then you can go through localhost:8080/test.php The visit.

Add extensions to the PHP image

If you need to add extensions to PHP, you need to use Dockerfile and install the extensions as shown in the official image, as shown in Docker-PHP7.1-ES73en. You can add it to the PHP mirror as follows gd pdo_mysql zip opcache Extension.


# Dockerfile  file 

FROM php:7.1-fpm

MAINTAINER dailybird <dailybird@mail.com>

RUN apt-get update && apt-get install -y libpng12-dev libjpeg-dev \
  && rm -rf /var/lib/apt/lists/* \
  && docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
  && docker-php-ext-install gd pdo_mysql zip opcache

Accordingly, we also need to change image in ES80en-ES81en. yml file to build, that is, created by Dockerfile:


# ...
 php:
  # image: php:7.1-fpm
  build: 
   #  Pointed out that  Dockerfile  The directory and filename of the file you are in 
   context: ./images/php
   dockerfile: Dockerfile
# ...

Overrides the PHP configuration

We can go through Dockerfile File, copy part 1 configuration file to /usr/local/etc/php/conf.d , so that the corresponding configuration can be overwritten when the mirror is started:


# Dockerfile  Other content ...

#  Copies the corresponding configuration file into the container  PHP  Configure the directory to override the original  PHP  configuration 
COPY ./config/php.ini /usr/local/etc/php/conf.d/
COPY ./config/opcache-recommended.ini /usr/local/etc/php/conf.d/

The contents of these two profiles are:

php.ini


memory_limit = 512M
post_max_size = 1024M
upload_max_filesize = 1024M

More configuration items visible: http: / / php net/manual/zh/ini...

opcache-recommended.ini


opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=4
opcache.enable_cli=1

In addition, we can add a data volume to map the configuration file directory to the host to be modified at any time.

Make tool image

We can make a container for installing command tools such as git composer so that we can use them to manipulate project files. At this point, we need to modify the following files:

docker-compose.yml


services:
 #  other ...
  
 #  Used for  Git . Composer  Tools such as 
 console:
  build: 
   context: ./images/console
   dockerfile: Dockerfile
  volumes_from: 
   - php
  tty: true

Since it inherits the same data volume as the PHP image, the 1 tool container can also implement the pair ./apps Operation of the project file directory.

Dockerfile

And then we need to add Dockerfile File corresponding to the docker-compose.yml In the build That is as follows:


FROM php:7.1-fpm

MAINTAINER dailybird <dailybird@mail.com>

#  The installation  git curl vim zip
RUN apt-get update && apt-get install -y git curl vim libfreetype6-dev \
  && rm -rf /var/lib/apt/list* \
  && pecl install zip \
  && docker-php-ext-enable zip

#  The installation  composer
RUN curl -o composer.phar https://getcomposer.org/download/1.4.1/composer.phar \
  && chmod +x composer.phar

#  configuration  composer  Change the mirror image source to China 
RUN mv composer.phar /usr/local/bin/composer \
   && echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc \
   && . ~/.bashrc \
   && composer config -g repo.packagist composer https://packagist.phpcomposer.com
   
#  Set up the  Git  The identity of the 
RUN git config --global user.name "username" \
  && git config --global user.email "user@mail.com"

Note that the Git information is modified as it is.

Quickly build the LNMP environment

Since there are many download commands in Dockerfile before, use docker-compose up --build -d It will take a long time to execute due to the network speed problem, so we can pull out the download part, make a new image and use it directly. In this way, the build process will be very fast.

I have created such a project on Github, in which those images that need to be downloaded in large quantities have been made and uploaded to the mirror warehouse, you can clone them by the following ways:


server{
  listen     80;
  server_name localhost;
  root     /mnt/apps;
  index     index.php index.html index.htm;

  location / {
    index    index.php index.html;
    try_files   $uri $uri/ /index.php?$query_string;
  }

  location ~ \.php$ {
    #  Used here  php  As an internal domain name connection  php  The container 
    fastcgi_pass   php:9000;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include     fastcgi_params;
  }
}

0

Please refer to the following steps for usage.

The custom

Before creating the container, there are a few things that need to be changed as it is:

docker-compose.yml

Modify the port number in the web container configuration to change 8080 to another port; Modify the information of database username and password 1 in database container configuration;

./images/console/Dockerfile

The Git identity information needs to be modified.

Create a container

In the cloned directory:


server{
  listen     80;
  server_name localhost;
  root     /mnt/apps;
  index     index.php index.html index.htm;

  location / {
    index    index.php index.html;
    try_files   $uri $uri/ /index.php?$query_string;
  }

  location ~ \.php$ {
    #  Used here  php  As an internal domain name connection  php  The container 
    fastcgi_pass   php:9000;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include     fastcgi_params;
  }
}

1

After execution, use docker ps You can find four containers running, including:

PHP container, containing fpm and 1 some PHP extensions; web container: Nginx container; MySQL container; console containers: Utility class containers, including Git, Composer, etc.

Directory features

After the container is created, there are three subdirectories in the original directory:

apps

For storing project files, the directory is Shared by PHP containers, Nginx containers, and utility class containers.

database

This directory is a database directory and maps to the data directory of MySQL.

images

This directory contains the mirrored Dcokerfile file and configuration directory, where the config subdirectory maps to the corresponding configuration file directory of the service class container.

Remove the container

When no longer in use, you can remove the container using the following command. Note: Data volumes will not be deleted along with them.


server{
  listen     80;
  server_name localhost;
  root     /mnt/apps;
  index     index.php index.html index.htm;

  location / {
    index    index.php index.html;
    try_files   $uri $uri/ /index.php?$query_string;
  }

  location ~ \.php$ {
    #  Used here  php  As an internal domain name connection  php  The container 
    fastcgi_pass   php:9000;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include     fastcgi_params;
  }
}

2

Related articles: