A detailed tutorial on using Docker to build a complete development environment

A detailed tutorial on using Docker to build a complete development environment

Introduction to DNMP

DNMP (Docker + Nginx + MySQL + PHP7/5 + Redis) is a full-featured LNMP one-click installer . It supports rich services, powerful functions, simple to use, and compatible with multiple platforms. It is a complete set of PHP environments for docker development and production environments suitable for multiple terminals.

DNMP project features:

  • 100% open source
  • 100% compliant with Docker standards
  • Supports coexistence of multiple versions of PHP , which can be switched at will (PHP5.4, PHP5.6, PHP7.1, PHP7.2, PHP7.3)
  • Support binding any number of domain names
  • Support for HTTPS and HTTP/2
  • PHP source code, MySQL data, configuration files, and log files can all be modified and viewed directly in the Host
  • Built-in complete PHP extension installation command
  • By default, it supports popular extensions such as pdo_mysql , mysqli , mbstring , gd , curl , opcache etc., and can be flexibly configured according to the environment
  • Common services can be selected with one click:
  1. Multiple PHP versions: PHP5.4, PHP5.6, PHP7.1-7.3
  2. Web services: Nginx, Openresty
  3. Database: MySQL5, MySQL8, Redis, memcached, MongoDB, ElasticSearch
  4. Message Queue: RabbitMQ
  5. Auxiliary tools: Kibana, Logstash, phpMyAdmin, phpRedisAdmin, AdminMongo
  6. Applied in actual projects to ensure 100% availability
  7. All images are from the official Docker repository, safe and reliable
  8. One-time configuration, available for Windows, Linux, and MacOs
  9. Support quick installation of extension command install-php-extensions apcu
  10. For specific usage documents, please refer to: https://gitee.com/bruce_qiq/dnmp/blob/master/README.md. There is already a clear usage tutorial in the documentation. Next, we will make a simple demonstration of some basic operations after installation, because some operations are not very detailed in the source documentation and have obvious pitfalls. Mainly record several functions:

1. Fix the problem of being unable to connect to MySQL.

2. Configure the NGINX virtual domain name.

3. Install PHP extensions.

Because I plan to continue using this package as my own development environment, I will summarize different issues later, and will also make iterative updates based on the original owner's repository. Welcome to follow. The technical circle of the public account card II

Fix MySQL connection problem

Because dnmp maps the local port 3305 to the MySQL container's port 3306 during installation, we need to connect to port 3305 when connecting.

Under the www directory of the sample code, there is a localhost directory. We find a piece of code for testing MySQL, as follows:

// Detect MySQL service function getMysqlVersion()
{
  if (extension_loaded('PDO_MYSQL')) {
    try {
      $dbh = new PDO('mysql:host=192.168.2.103;dbname=mysql', 'root', '123456');
      $sth = $dbh->query('SELECT VERSION() as version');
      $info = $sth->fetch();
    } catch (PDOException $e) {
      return $e->getMessage();
    }
    return $info['version'];
  } else {
    return 'PDO_MYSQL extension is not installed ×';
  }
}

We just need to modify the code to the following:

// Detect MySQL service function getMysqlVersion()
{
  if (extension_loaded('PDO_MYSQL')) {
    try {
      $dbh = new PDO('mysql:host=192.168.2.103;port=3305;dbname=mysql', 'root', '123456');
      $sth = $dbh->query('SELECT VERSION() as version');
      $info = $sth->fetch();
    } catch (PDOException $e) {
      return $e->getMessage();
    }
    return $info['version'];
  } else {
    return 'PDO_MYSQL extension is not installed ×';
  }
}

Here 192.168.2.103 is the IP address of our host machine. You can fill it out according to your actual situation. You can view it through ifconfig. If it is a Windows system, use ipconfig to check.

 ifconfig

Configure NGINX domain name

Before configuration, we need to clarify a few things. The location of our source code directory, NGINX configuration file, and NGINX log file. Special Note:

1. We can see from the screenshot that there are two configuration files under the conf.d directory. These two are the domain name files I configured in advance. This file is mapped to the local disk through the NGINX container. Therefore, it can be viewed through the host machine.

2. The logs file is NGINX's error, access and other related log files, which are also mapped to the host in the NGINX container.

3. The www here is the program source code path, and all our program source codes can be placed here.

For the NGINX domain name configuration file, it is recommended to enter the container to write it instead of writing it on the host machine.

Step 1: Go to our www directory and install an application. It is recommended to use compser for installation. For the use of composer, you can also refer to the documentation for simple configuration.

ert@ertiaodeMac-mini  ~/develop/env/dnmp/www   master ●  composer create-project topthink/think=5.1.35 tp5
Creating a "topthink/think=5.1.35" project at "./tp5"
Installing topthink/think (v5.1.35)
 - Installing topthink/think (v5.1.35): Loading from cache
Created project in /app/tp5
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
 - Installing topthink/think-installer (v2.0.0): Loading from cache
 - Installing topthink/framework (v5.1.39): Loading from cache
Writing lock file
Generating autoload files
 ert@ertiaodeMac-mini  ~/develop/env/dnmp/www   master ●  ll
total 0
drwxr-xr-x 3 ert staff 96B 9 6 22:02 localhost
drwxr-xr-x 19 ert staff 608B 9 8 22:26 tp5

In this way, a tp5 framework is set up locally. Step 2: Enter the NGINX container, view the container-id of the Docker container through docker ps, and use the docker exec command to enter the container.

ert@ertiaodeMac-mini  ~/develop/env/dnmp/www   master ●  docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f5f9cd08aa2 dnmp_php "docker-php-entrypoi…" 27 hours ago Up About an hour 9000/tcp, 9501/tcp php
8bc44fc8c578 dnmp_nginx "/docker-entrypoint.…" 27 hours ago Up 26 hours 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nginx
f02ad53bf14d mysql:5.7.28 "docker-entrypoint.s..." 27 hours ago Up 26 hours 33060/tcp, 0.0.0.0:3305->3306/tcp mysql5
 ert@ertiaodeMac-mini  ~/develop/env/dnmp/www   master ●  docker exec -it 8bc44fc8c578 /bin/sh
/www#ls
localhost tp5

Enter the NGINX configuration file path

/ # cd /etc/nginx/
/etc/nginx # ls -la
total 60
drwxr-xr-x 1 root root 4096 Sep 8 20:23 .
drwxr-xr-x 1 root root 4096 Sep 7 19:34 ..
drwxr-xr-x 5 root root 160 Sep 8 20:05 conf.d
-rw-r--r-- 1 root root 429 Sep 6 22:02 fastcgi-php.conf
-rw-r--r-- 1 root root 1077 Jul 8 00:14 fastcgi.conf
-rw-r--r-- 1 root root 1062 Sep 6 22:02 fastcgi_params
-rw-r--r-- 1 root root 2837 Jul 8 00:14 koi-utf
-rw-r--r-- 1 root root 2223 Jul 8 00:14 koi-win
-rw-r--r-- 1 root root 5231 Jul 8 00:14 mime.types
lrwxrwxrwx 1 root root 22 Jul 11 ​​04:27 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root 760 Sep 6 22:02 nginx.conf
drwxr-xr-x 2 root root 4096 Sep 8 20:23 rewrite
-rw-r--r-- 1 root root 636 Jul 8 00:14 scgi_params
-rw-r--r-- 1 root root 664 Jul 8 00:14 uwsgi_params
-rw-r--r-- 1 root root 3610 Jul 8 00:14 win-utf

conf.d is our domain name configuration path, and nginx is the default configuration file.

Create a domain configuration file.

/etc/nginx # cd conf.d/ && touch tp5.conf
/etc/nginx/conf.d # ls -la
total 16
drwxr-xr-x 5 root root 160 Sep 8 20:05 .
drwxr-xr-x 1 root root 4096 Sep 8 20:23 ..
-rw-r--r-- 1 root root 48 Sep 6 22:02 .gitignore
-rw-r--r-- 1 root root 2254 Sep 6 22:02 localhost.conf
-rw-r--r-- 1 root root 505 Sep 8 20:29 tp5.conf

Configure the tp5.conf file. Copy the following content:

server {
  listen 80;
  server_name tp5.com;
  root /www/tp5/public;
  index index.php;
  #NGINX log file. If it is another domain name, just replace the file name nginx.tp5.com.xx.log.
  access_log /var/log/nginx/nginx.tp5.com.access.log;
  error_log /var/log/nginx/nginx.tp5.com.error.log;
  #Thinkphp5's URL rewrite file avoids repeated writing and puts it in a separate file. Other thinkphp projects can be directly imported in the future.
  include rewrite/thinkphp.conf;
  location ~ \.php$ {
      # Here php is the name of the php container, which can be changed to the actual IP. It is not recommended because the IP will change.
      fastcgi_pass php:9000;
      include fastcgi-php.conf;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
    }
}

Test the domain name configuration and restart the NGINX service.

/etc/nginx/conf.d # nginx -t && nginx -s reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
/etc/nginx/conf.d #

Add the domain name to the host file of the host machine.

Access Test

PHP extension installation

When building a Docker environment, the Redis extension of PHP is not installed by default. Here we take the Redis extension as an example.

Since some extensions are built-in (for specific extensions, refer to the usage documentation mentioned above), you can directly install them using the command.

install-php-extensions redis

Testing Extensions

/www # php --ri redis
redis
Redis Support => enabled
Redis Version => 5.3.1
......

Summarize

This is the end of this article about using docker to build a complete development environment. For more information about using docker to build a complete development environment, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to quickly build an Oracle development environment using Docker
  • Detailed explanation of setting up Docker development environment on MAC OSX
  • Ubuntu builds a LNMP+Redis development environment based on Docker (picture and text)
  • A detailed tutorial on building a PHP development environment based on Docker

<<:  This article will help you understand the life cycle in Vue

>>:  A brief analysis of MySQL locks and transactions

Recommend

Detailed explanation of JavaScript onblur and onfocus events

In HTML pages, visual elements such as buttons an...

Solution to mysql error when modifying sql_mode

Table of contents A murder caused by ERR 1067 The...

How to implement nginx smooth restart

1. Background During the server development proce...

React realizes the whole process of page watermark effect

Table of contents Preface 1. Usage examples 2. Im...

The whole process of installing mysql5.7.22 under ARM64 architecture

MySQL download address: https://obs.cn-north-4.my...

HTML markup language - reference

Click here to return to the 123WORDPRESS.COM HTML ...

How to use css overflow: hidden (overflow hiding and clearing floats)

Overflow Hide It means hiding text or image infor...

A brief discussion on JavaScript scope

Table of contents 1. Scope 1. Global scope 2. Loc...

Summary of MySQL ALTER command knowledge points

When we need to change the table name or modify t...

js to achieve interesting countdown effect

js interesting countdown case, for your reference...

IE8 provides a good experience: Activities

Today I had a sneak peek at IE8 beta 1 (hereafter...

About ROS2 installation and docker environment usage

Table of contents Why use Docker? Docker installa...