docker-compose learning: A simple LNMP is constructed by specifying mirrors through image instructions

Keywords: Operation & Maintenance Docker PHP Nginx MySQL

docker-compose understands:

Each service must specify mirrors or build instructions (requiring Docker files) through image instructions to automatically build the generated mirrors.
This time, we directly specify the mirror to build a simple LNMP, as an introduction to learning docker-compose.
The actual operation must require Dockerfile configuration to meet their own personalized needs.

1. Install docker-compose

Official website https://docs.docker.com/compose/ A good introductory website https://docker_practice.gitee.io/compose/compose_file.html

  1. Pre-set Aliyun Docker Acceleration Copy it again so that you don't forget it every time and look it up every time. * * below https://2xxxxxxx.mirror.aliyuncs.com It's my exclusive accelerator address after I applied. Remember to change it to my own address.

     1. Install/Upgrade Docker Client
     It is recommended to install Docker client with version 1.10.0 or above, refer to document docker-ce.
    
     2. Configuring Mirror Accelerator
     Users with Docker client version greater than 1.10.0
    
     You can use the accelerator by modifying the daemon configuration file / etc/docker/daemon.json
     sudo mkdir -p /etc/docker
     sudo tee /etc/docker/daemon.json <<-'EOF'
     {
       "registry-mirrors": ["https://2xxxxxxx.mirror.aliyuncs.com"]
     }
     EOF
     sudo systemctl daemon-reload
     sudo systemctl restart docker
    

    The screenshots are as follows:

  2. Install docker-compose Operate according to the official website

     sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    

    It's too slow! So, I went to look for the domestic mirror image again. Domestic Mirror Reference Articles https://blog.csdn.net/huiyanghu/article/details/82253886

Copy from the above reference article

	curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

Sure enough! Next, set up to run

chmod +x /usr/local/bin/docker-compose

Confirm:

$docker-compose --version
	docker-compose version 1.24.0, build 0aa59064

2. Try to build the first project: LNMP

  1. Create a separate compose learning directory mkdir compose && cd compose mkdir lnmp1 && cd lnmp1 Create a mysql data storage directory and a nginx/php storage directory mkdir mysql mkdir nginx && cd nginx Create a conf.d directory and a www directory according to nginx website habits Write an index.php in advance at www/html *** Adding a few words to the front just to make sure it's convenient!

     <h3> wellcome WZH!
     <h4> this is a docker demo
     <h5> 20190416
     <p>==================</p>
    
     <?php
     phpinfo();
     ?>
    

    The final directory structure is as follows

    $ tree lnmp -d lnmp ├── mysql │ └── data │ ├── mysql │ ├── performance_schema │ └── sys └── nginx ├── conf.d └── www └── html

3. Start writing templates

  1. Reference resources: https://www.jianshu.com/p/7fff488604c1 https://www.cnblogs.com/blogscc/p/9473123.html

  2. Confirm port occupancy in advance

    ss -tlnp |grep 3306 LISTEN 0 80 127.0.0.1:3306 0.0.0.0:*

ss -tlnp |grep 80
LISTEN   0         80                127.0.0.1:3306             0.0.0.0:*       
LISTEN   0         128                       *:80                     *:*  

** My ports 3306 and 80 have been occupied, so the host ports in the following scripts have been replaced by 3307 and 81.

  1. Testing nginx, php, mysql image and container operation respectively

  2. Write template file docker-compose.yml
    $ cat docker-compose.yml

     version: "3"
     services:
       web_server:
         image: nginx:latest
         container_name: nginx
         links:
             - php:php
         ports:
             - "81:80"
             - "443:443"
         volumes:
             - ./nginx/www/html:/var/www/html
             - ./nginx/conf.d:/etc/nginx/conf.d  
    
       db_server:
         image: mysql:5.7.21
         container_name: mysql
         environment:
             MYSQL_ROOT_PASSWORD: 123456
         ports:
             - "3307:3306"
         volumes:
             - ./mysql/data:/var/lib/mysql
    
       php:
         image: php:5.6-fpm
         container_name: php
         links:
             - db_server:mysql
         ports:
             - "9000:9000"
         volumes:
             - ./nginx/www/html:/var/www/html
             - ./nginx/conf.d:/etc/nginx/conf.d
    
    1. Operation test In the current catalogue $ docker-compose up -d mysql is up-to-date php is up-to-date nginx is up-to-date

      ** If docker-compose.yml is modified, stop before restarting $ docker-compose down Stopping nginx ... done Stopping php ... done Stopping mysql ... done Removing nginx ... done Removing php ... done Removing mysql ... done

      Verify whether the container is started $ docker ps -s

       CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                      NAMES               SIZE
       6bb7eb2c7665        nginx:latest        "nginx -g 'daemon of..."   About an hour ago   Up About an hour    0.0.0.0:443->443/tcp, 0.0.0.0:81->80/tcp   nginx               2B (virtual 109MB)
       47f3ed37ac21        php:5.6-fpm         "docker-php-entrypoi..."   About an hour ago   Up About an hour    0.0.0.0:9000->9000/tcp                     php                 0B (virtual 344MB)
       41d22b0b79f0        mysql:5.7.21        "docker-entrypoint.s..."   About an hour ago   Up About an hour    0.0.0.0:3307->3306/tcp                     mysql   
      

      curl 127.0.0.1:81

Browser Open http://192.168.1.193:81/

4. Error handling

  1. curl 127.0.0.1:81/index.php is always 404 Not Found Because conf.d's default.conf copies the actual environment, php parsing fastcgi_pass 127.0.0.1:9000; Now you need to change to php container name fastcgi_pass php:9000;

  2. When docker ps -s is viewed, the nginx container always does not start The php container name was not correctly written

Following is a revised copy of the reference article default.conf

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /var/www/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/html;
    }
    location ~ \.php$ {
        root           /var/www/html;
        # fastcgi_pass   127.0.0.1:9000;
        # The php container name in the compose template file must be changed
        fastcgi_pass   php:9000
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; # Modify to $document_root
        include        fastcgi_params;
    }
}

Posted by ridckie_rich on Mon, 06 May 2019 03:55:38 -0700