Docker Compose layout tool to deploy lnmp practice and theory (detail)

First, the theoretical overview

Docker Compose is a definition and run multiple tools Docker container itself we can be simply understood as is a binary file (command), we can more easily build complex applications based on manageable docker's

  • Use Compose process: three steps
  1. Use the Dock and file defines a single application environment to replicate anywhere.
  2. In docker-compose.yml composition as defined in the application service, the application of a single composition as a whole, each of which in a relatively isolated environment, but we can work together to perform port mapping.
  3. After running docker-compose started writing and running the entire application

Arrange

The role of the status of the command, according to the coupling between objects are deployed, and the environment depend on the object to be deployed, the deployment process in order to develop the implementation of the various actions to prevent and access; this information will be in the orchestration tool to specify the format in the configuration defined in the file, to ensure that this process can always be reproduced in an orderly and reliable new environment

deploy

Specific roles and status of implementation, in accordance with the schedule specified content and processes in the implementation of the target container, everything is good to develop specific implementation in accordance with the choreography.

Compose principle

Overview simple : The user performs a docker-compose up operation instruction indicates the tasks defined in the docker-compose.yml, build or run mirror container, and some other operations. So if the current host machine corresponding to the application container (i.e., repeated), docker-compose logic will determine if the user has specified to restart the service, which executes service module container already exists restart otherwise it will start direct method the existing container. Difference between the two is that the former is restarted, it will create a new container, the container will remove it out. The definition of the parameters of the process to create a container compose instructions are specified in the file and .yml good.

Second, the use docker compose deployment lnmp

  • Community Edition deployment docker
[root@localhost ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
#安装依赖

[root@localhost ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
#下载docker的repo

[root@localhost ~]# yum -y install docker-ce

[root@localhost ~]# mkdir /etc/docker
[root@localhost ~]# vim /etc/docker/daemon.json

{
        "registry-mirrors":["https://*******.mirror.aliyuncs.com"]
}
#阿里云镜像加速

Address the needs of individuals get access to Ali cloud, refer to this document

  • Installation compose Tools
[root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose 


[root@localhost ~]# mkdir compose_lnmp
[root@localhost ~]# cd compose_lnmp/
  • Write and compose documents dockerfile formats rigorous
[root@localhost compose_lnmp]# tree
.
├── docker-compose.yml
├── mysql
│   ├── conf
│   │   └── my.cnf
│   └── data
├── nginx
│   ├── Dockerfile
│   ├── nginx-1.12.1.tar.gz
│   ├── nginx.conf
│   └── run.sh
├── php
│   ├── Dockerfile
│   ├── php-5.6.39.tar.gz
│   └── php.ini
└── wwwroot
    ├── index.html
    └── index.php


[root@localhost compose_lnmp]# cat docker-compose.yml 
version: 3
services:
  nginx:
#在 services 标签下的第二级标签是 web,这个名字是用户自己自定义,它就是服务名称。
    hostname: nginx
#相当于提前预定义容器的主机名,会写入到容器的/etc/hostname中
 
    build:
#该标签标示基于dockerfile来构建一个镜像
      context: ./nginx
#指定构建镜像所需的dockerfile目录,我这里指定的是相对目录,该文件(docker-compose.yml)所处位置,也可以绝对目录,
      dockerfile: Dockerfile

    ports:
      - 80:80
#和宿主机映射的端口

    networks:
      - lnmp
#指定的网络环境????

    volumes:
      - ./wwwroot:/usr/local/nginx/html:ro
#将宿主机目录挂载到该容器当中(只读)

  php:
    hostname: php
    build:
      context: ./php
      dockerfile: Dockerfile
    ports:
      - 9000:9000
    networks:
      - lnmp
    volumes:
      - ./wwwroot:/usr/local/nginx/html

  mysql:
    hostname: mysql
    image: mysql:5.6
#image标签表示是基于mysql:5.6镜像构建一个新的
    ports:
      - 3306:3306
    networks:
      - lnmp
    volumes:
      - ./mysql/conf:/etc/mysql/conf.d
      - ./mysql/data:/var/lib/mysql
    command: --character-set-server=utf8
    environment:
      MYSQL_ROOT_PASSWORD: 123456
#???mysql中root用户密码
      MYSQL_DATABASE: wordpress
#新建一个数据库名为WordPress
      MYSQL_USER: user
#新建一个用户名为user
      MYSQL_PASSWORD: user123
#新建user用户密码为user123

networks:
  lnmp:
#创建一个网络,lnmp为该网络的别名
  • dockerfile write the Nginx
[root@localhost compose_lnmp]# cat nginx/Dockerfile 
FROM centos:7
#如果有该docker镜像则基于该镜像进行构建,没有则自行从镜像仓库pull
MAINTAINER joinbestbest
ENV TIME_ZOME Asia/Shanghai
RUN useradd -s /sbin/nologin -M nginx && yum -y install gcc gcc-c++ make openssl-devel pcre-devel
#运行的命令,安装依赖
ADD nginx-1.12.1.tar.gz /tmp
#将宿主机源码包传送到容器内tmp目录
RUN cd /tmp/nginx-1.12.1 && \
        ./configure --prefix=/usr/local/nginx && \
        make -j 2 && \
        make install
#编译;make -j 2使用两个进程来make,加速
RUN rm -rf /tmp/nginx* && yum clean all && \
        echo "${TIME_ZOME}" > /etc/timezone && \
        ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime 

EXPOSE 80
COPY nginx.conf /usr/local/nginx/conf/
WORKDIR /root/nginx
ADD run.sh /run.sh
RUN chmod 775 /run.sh
CMD ["/run.sh"]
#一系列命令操作
  • Nginx configuration files directly copied into the containers
user nginx nginx;
worker_processes 1;
worker_rlimit_nofile 102400;
error_log logs/error.log;
pid logs/nginx.pid; 

events {
    use epoll;
    worker_connections 4096;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    
    server {
        listen       80;
        server_name  localhost;
    charset utf-8;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location ~ \.php$ {
            root html;
            fastcgi_index index.php;
            fastcgi_pass 192.168.111.3:9000;
            #ip修改为宿主机的,端口是php程序映射到宿主机的端口
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}

daemon off;


[root@localhost compose_lnmp]# vim nginx/run.sh 
#在dockerfile中指定运行的脚本传送到容器中
#!/bin/bash

/usr/local/nginx/sbin/nginx
  • mysql configuration file
    mysql operation is relatively small, basically in the development of good compose, it is more convenient
[root@localhost compose_lnmp]# cat mysql/conf/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/tmp/mysql.sock
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/tmp/mysql.pid
  • PHP documents
[root@localhost compose_lnmp]# cat php/Dockerfile 
FROM centos:7
MAINTAINER crushlinux
ENV TIME_ZOME Asia/Shanghai
RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel
ADD php-5.6.39.tar.gz /tmp/

RUN cd /tmp/php-5.6.39 && \
        ./configure --prefix=/usr/local/php \
        --with-config-file-path=/usr/local/php/etc \
        --with-mysql --with-mysqli \
        --with-openssl --with-zlib --with-curl --with-gd \
        --with-jpeg-dir --with-png-dir --with-iconv \
        --enable-fpm --enable-zip --enable-mbstring && \
        make -j 4 && \
        make install

RUN cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
        sed -i 's/127.0.0.1/0.0.0.0/g' /usr/local/php/etc/php-fpm.conf && \
        sed -i "21a daemonize=no" /usr/local/php/etc/php-fpm.conf && \
        echo "${TIME_ZOME}" > /etc/timezone && \
        ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime

COPY php.ini /usr/local/php/etc/
RUN rm -rf /tmp/php* && yum clean all
WORKDIR /usr/local/php/
EXPOSE 9000
#将容器内该端口进行映射
CMD ["./sbin/php-fpm","-c","/usr/local/php/etc/php-fpm.conf"]
  • wwwroot directory page test document
[root@localhost compose_lnmp]# cat wwwroot/index.html 
www.join.com

[root@localhost compose_lnmp]# cat wwwroot/index.php 
<?php
phpinfo();
?>

Third, the test

  • Deploy WordPress
直接将相应的代码文件放到宿主机的wwwroot目录下即可,因为这里已经做好了映射

[root@localhost compose_lnmp]# tar xf wordpress-5.0.2-zh_CN.tar.gz 

[root@localhost compose_lnmp]# mv wordpress wwwroot/

Then access to the test browser 192.168.111.3/wordpressoperation

IV Summary

  1. Former docker knowledge learned are blurred impression
  2. dockercompose tool will not use most of the production environment
  3. Regardless, the basics of the docker must actively keep up
  4. And the difference between malpractice dockercompose and other tools will then be posted to the blog
  5. The basic theory docker finishing posted to the blog

Guess you like

Origin www.cnblogs.com/joinbestgo/p/11075056.html