Methods and steps of deploying LNMP & phpMyAdmin with docker


Environmental preparation:

LNMP is deployed on one host based on multiple containers
Nginx service:
MySQL service:
PHP service:

To solve the problem of container fixed IP address:

Note: when the container is stopped or deleted, and the same container is run again, its IP address is no longer the original address, so we need to customize a network segment to specify the IP address of the container.

Project operation:

(1) First, customize a network:

[[email protected]docker01 ~]# docker network create -d bridge --subnet --gateway mynet1

(2) Download nginx, php:fpm , mysql-5.7 image:

[[email protected] ~]# docker pull nginx 
[ [email protected]  ~]# docker pull  php:7.2-fpm  <! -- using php:7.2-fpm The mirror image of -- >
[ [email protected]  ~]# docker pull  mysql:5.7   <! -- using mysql:5.7 The mirror image of -- >

(3) Run a container based on nginx image#####

#First, run the nginx service to copy the configuration file of nginx

Parameter explanation:

  • Run: create a new container
  • -d: Background operation
  • –Name: the custom name is test1

Create the directory where files need to be mounted on the host computer:

Website home directory / wwwroot;

Nginx configuration file / docker;

[[email protected] ~]# mkdir /wwwroot
[[email protected] ~]# mkdir /docker

##Use the docker CP command to copy the directory where nginx stores configuration files to the local computer,

Note: the docker CP command can copy the files on the host computer to the container, or reverse copy the files or directories in the container to the local computer

[[email protected] ~]# docker cp test1:/etc/nginx /docker/ #Use docker CP to copy the directory where nginx stores configuration files to the host computer

[[email protected] ~]# ls /docker/nginx/conf.d/ 


#Note: what needs to be modified is the default.conf Files, not nginx.conf Documents.

Copy nginx webpage Directory:

[[email protected] ~]# docker cp test1:/usr/share/nginx/html /wwwroot/
[[email protected] ~]# ls /wwwroot/html/
50x.html index.html

Modify the default nginx test page:

[[email protected] html]# echo "<h1>hello welcome to nginx web</h1>" > index.html

(4) Run the nginx container:

Mount the directory in the container to the local directory, and specify the IP address

[[email protected] ~]# docker run -itd --name nginx --network my_net1 --ip -p 80:80 -v /docker/nginx:/etc/nginx -v /wwwroot/html:/usr/share/nginx/html nginx:latest

Parameter explanation:

  • Run: run a container
  • -ITD: I: Interactive
  • t: Pseudo terminal
  • d: Keep container running in the background
  • –Network: Based on which network card to create a network
  • –IP: defines the IP address of the container
  • -V = — volume data volume, a mount
  • Mount format: host: in container
  • p: Mapping port, host port: in container port

(5) Run the MySQL container:

[[email protected] ~]# docker run --name mysql -e -d -p 3306:3306 --network my_net1 --ip mysql:5.7

-E is to set the environment variable in the container. We set the password environment variable of MySQL, which will be passed into the container to set the password of MySQL

Test whether the root user can log in to MySQL locally

First, you need to download the MySQL client:

[[email protected] ~]# yum -y install mysql

(6) Run the PHP container:

[[email protected] ~]# docker run -itd -p 9000:9000 --name phpfpm -v /wwwroot/html:/usr/share/nginx/html --network my_net1 --ip php:7.2-fpm

be careful:
Make sure that nginx and PHP share a directory for storing web pages. PHP creates the same default web page root directory as nginx (- when mounting, if there is no such directory in the container, it will be created automatically).

(7) To configure nginx and PHP FPM:

We need to configure PHP to parse nginx

Create a simple test page:

Next, you need to modify the nginx configuration file:

[[email protected] html]# vim /docker/nginx/conf.d/default.conf

(8) Configure MySQL:

Build phpMyAdmin:

PhpMyAdmin is a MySQL database management tool based on PHP and web base, which allows managers to manage MySQL database with web interface. With this web interface, it can be a better way to input complicated SQL syntax in a simple way, especially to handle the import and export of a large amount of data. One of the bigger advantages is that phpMyAdmin runs on the web server just like other PHP programs, but you can use the HTML pages generated by these programs anywhere, that is, to remotely manage the MySQL database, so that you can easily create, modify and delete databases and tables

[[email protected] html]# pwd

Unzip to the current directory:

[[email protected] html]# unzip


[[email protected] html]# mv phpMyAdmin-4.9.1-all-languages phpmyadmin

Modify nginx configuration file — configure the connection with phpMyAdmin:

[[email protected] html]# vim /docker/nginx/conf.d/default.conf

Copy the original location configuration item, and then add the following two location configuration segments.

After modifying the nginx configuration file, restart nginx:

Visit the test website:

The port in the container has been mapped to the host, so you can directly access the host address:

Visit the PHP parsing nginx web page:

Next, test and log in to phpMyAdmin

Access URL:

The MySQL I module is missing, so it can’t be accessed, so you need to add PHP to support the MySQL configuration module

How to add an extension module to a container? We can log in to dockerhub to query related documents

Link path:

Copy the above dockerfile script and install it locally:

Note that some contents need to be added:

[[email protected] ~]# vim Dockerfile

FROM php:7.2-fpm
RUN apt-get update && apt-get install -y \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
  && docker-php-ext-install -j$(nproc) iconv \
  && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
  && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install mysqli pdo pdo_mysql

Build dockerfile:

[[email protected] ~]# docker build -t phpfpm-mysqli .

Parameter explanation:

  • Build: build
  • -t: Specify label
  • . indicates the dockerfile in the current directory

After the completion of the construction, delete the original PHP image and run the image supporting mysqli module that has just been successfully constructed

[[email protected] ~]# docker stop phpfpm 
[[email protected] ~]# docker rm phpfpm 
[[email protected] ~]# docker run --name phpfpm -d -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network my_net1 --ip phpfpm-mysqli:latest

#Image specifies the name of the image just generated by building the dockerfile.

Modify the default sample (sample file) of phpMyAdmin:

If you want to use the configuration file, you must rename it and remove the sample.

[[email protected] phpmyadmin]# pwd
[[email protected] phpmyadmin]# cp

Modify the configuration file:

[[email protected] phpmyadmin]# vim

After modifying the configuration file, restart PHP:

[[email protected] phpmyadmin]# docker restart phpfpm 

Visit phpMyAdmin website:

Enter URL:

#The user name and password are the login password of the database

Successful access to MySQL database……

So far, the deployment of LNMP between multiple containers in docker is completed.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.