Detailed explanation of Docker-compose and actual combat of LNMP construction

Table of contents

1. Introduction to Docker-compose

1 Introduction

2. Overview

Two, Docker-compose installation

3. YAML file format and writing precautions

1 Introduction

2. How to use

4. Common commands of Docker Compose

Five, Docker Compose configuration common fields

6. Docker-compose builds LNMP actual combat


1. Introduction to Docker-compose

1 Introduction

        We know that a separate application container can be defined using a Dockerfile template file, and if multiple containers need to be defined, service orchestration is required. There are many technical solutions for service orchestration, such as Docker Compose, the official product of Docker.

        Dockerfile allows users to manage a single application container; while Compose allows users to define a set of associated application containers (called a project) in a template (YAML format), such as a Web service container plus On the back-end database service container, etc.

2. Overview

        The Docker-Compose project is an official open source project of Docker, which is responsible for the rapid orchestration of Docker container clusters .

        Docker-Compose divides the managed containers into three layers, namely project (project) , service (service) and container (container) . All the files in the Docker-Compose running directory (docker-compose.yml, extends file or environment variable file, etc.) form a project. If there is no special designation, the project name is the current directory name. A project can contain multiple services, and each service defines the image, parameters, and dependencies of the container running. A service can include multiple container instances. Docker-Compose does not solve the problem of load balancing, so other tools are needed to realize service discovery and load balancing, such as Consul.

project: Represents a project composed of multiple services. By default, the name of the working directory is used as the project name of the project.
service: A service can contain one or more containers, in which parameters such as network mode port mirroring data volume can be defined.
container: It can be implemented directly by running an existing image, or it can be implemented by building an image through a dockerfile.

        The default project configuration file of Docker-Compose is docker-compose.yml , and the configuration file can be customized through the environment variable COMPOSE_FILE or the -f parameter, which defines multiple dependent services and the containers that each service runs.

        Using a Dockerfile template file allows users to easily define a separate application container. In work, we often encounter situations that require multiple containers to cooperate with each other to complete a certain task. For example, to implement a web project, in addition to the web service container itself, it is often necessary to add a back-end database service container, and even a load balancing container.

        Compose allows users to define a group of associated application containers as a project through a single docker-compose.yml template file (YAML format).

        The Docker-Compose project is written in Python and calls the API provided by the Docker service to manage the container. Therefore, as long as the operating platform supports the Docker API, Compose can be used for orchestration management on it.

Two, Docker-compose installation

Note: Docker Compose is an independent product of Docker, so you need to install Docker Compose separately after installing Docker.

Get the installation source

#获取在线源
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#安装,给予执行权限
chmod +x /usr/local/bin/docker-compose
#查看版本
docker-compose --version

Installation package download

#下载好安装包,直接上传到系统,给执行权限,放在指定目录
chmod +x docker-compose
mv docker-compose /usr/local/bin/

3. YAML file format and writing precautions

1 Introduction

        YAML is a markup language, which can display data serialization format very intuitively, with high readability. Similar to the json data description language, the syntax is much simpler than json. YAML data structures are represented by indentation, consecutive items are represented by minus signs, key-value pairs are separated by colons, arrays are enclosed by brackets [ ], and hashes are enclosed by curly braces { }.

Note the following when using YAML

  • Case Sensitive;
  • Hierarchical relationships are represented by indentation, tabs are not supported for indentation, only spaces can be used for indentation ;
  • The number of indented spaces is not important, as long as the same level is left-aligned , usually 2 spaces are indented at the beginning;
  • Comment with # sign;
  • Symbol characters are indented with 1 space , such as colon ":", comma ",", horizontal bar "-";
  • If it contains special characters enclosed in single quotation marks ' ', it will be processed as a normal string; if quoted in double quotation marks " ", the special characters will be used as the meaning they want to express.

2. How to use

(1) Data structure

animals: pets        #object mapping: a dictionary of key-value pairs

(2) sequence array

pets:         #object mapping: 

  - cat                       - sequence value

  - dog

  - pig

pets: ["cat", "dog", "pig"]        #Another expression format

example

#Sequence mapping refers to an object, and the values ​​in the sequence belong to the object, such as vegetables include cabbage, carrots, potatoes, etc.

vegetables:         

  - potato

  - carrot

  - cabbage

#The mapping of mapping means that there are multiple sub-objects for an object, each with their values, such as the system state including cpu, memory, network, disk, etc.

vmstat: 

  cpu: 2

  memory: 4G

  disk: 100G

  network: 

    ipv4: 192.168.1.1 

    netmask: 255.255.255.0

    gateway: 192.168.1.254

    dns: 8.8.8.8

are represented by a left-aligned hierarchical relationship

(3) Boolean value

debug: true        #true true, false false, cannot be quoted in quotation marks

debug: false

(4) Text block

value: |        # | means to keep the newline and carriage return of the output text, if not added, even if the text is newline, it will be displayed on the same line

  hello          #In this case, hello is output, without |, it is helloworld!

  world!                                       world!

| - # do not keep newlines at the end of text blocks                 | + # will keep newlines at the end of text blocks

value: > hello        #Separate the string after > and the string after it with a space, that is hello world!

world!

(5) Anchors and references

type: &a string        #& is the anchor point, followed by the anchor point name, &a represents the string

type: *a                    #*a represents the reference anchor, the output here is type: string

types:                      #Here is the output type: 

  - int                                                   - int

  - *a                                                   - string 

  - double                                            - double

(6) Combining yaml files

pets:       

  - cat                      

  - dog

  - pig

---         #Multiple yml files can be written into one yml file, using --- as a separator

vegetables:         

  - potato

  - carrot

  - cabbage

4. Common commands of Docker Compose

Order describe
docker-compose build rebuild service
ps list containers
up Create and start containers
exec Execute the command inside the container
scale Specify the number of service containers to start
top show container processes
logs View container output
down Delete containers, networks, data volumes and images
stop/start/restart stop/start/restart service
-f,--file Use a specific compose template file, the default is docker-compose.yml
-p,--project-name Specify the project name, the directory name is used by default
-d running in the background

Five, Docker Compose configuration common fields

field meaning
build Build the image using Dockerfile. Specify the Dockerfile file name. To specify the Dockerfile file, you need to use the dockerfile tag in the child tag of the build tag to specify
dockerfile Build image context path (specify Dockerfile file)
context It can be the path of the dockerfile, or the url address pointing to the git repository
image Specifies the mirror.
command Execute the command to override the command executed by default after the container starts (similar to docker run)
container_name Specify the container name, because the container name is unique, if you specify a custom name, you cannot scale the specified number of containers (the same image specifies the number of multiple containers)
deploy Specify the configuration related to deploying and running services, which can only be used in Swarm mode
environment Add environment variables
networks Join the network, refer to the entries under the top-level networks
networks_mode Set the network mode of the container, such as host, bridge
ports Expose the container port, the same as -p, but the port cannot be less than 60
volumes Mount a host directory or command volume into the container, the command volume must define the volume name in the top-level volumes
volumes_from Mount volume from another service or container, optional parameters :ro and :rw. Only supported by version '2'
hostname container hostname
sysctls Set kernel parameters inside the container
links Connect to another container, - service name [: service alias] (similar to container interconnection)
privileged

It is used to give the container root permission. Note that it is not safe. There are only two values: true or false

restart        

Set restart policy, never, always, no-failure, unless-stopped

        never - the default policy, do not restart the container when the container exits

        on-failure—— The container will only be restarted when the container exits abnormally (exit status is not 0)

        on-failure:3——在容器非正常退出时重启容器,最多重启3次

        always——在容器退出时总是重启容器

        unless-stopped——在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器

depends_on

此标签用于解决容器的依赖、启动先后问题

六、Docker-compose搭建LNMP实战

整体文件结构

1.准备依赖文件,配置nginx

#创建nginx工作目录,上传nginx-1.12.0的软件包
mkdir /opt/compose_lnmp/nginx/
[root@localhost1 ~]#ls /opt/compose_lnmp/nginx/
Dockerfile  nginx-1.12.0.tar.gz  nginx.conf
#编写nginx的Dockerfile
vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image <chips>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make && useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make -j2 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
EXPOSE 443
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
#修改nginx配置文件(只做了基础配置,后续可以修改)
#修改45行,添加php项
 location / {
            root   html;
            index  index.html index.php;
        }
#66~71行取消注释,修改
location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   172.18.0.30:9000;
 68             fastcgi_index  index.php;
 69             fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_sc    ript_name;
 70             include        fastcgi_params;
 71         }

2.准备依赖文件,配置mysql

#创建mysql工作目录,上传mysql-boost-5.7.20的软件包
mkdir /opt/compose_lnmp/mysql/
[root@localhost1 mysql]#ls
Dockerfile  my.cnf  mysql-boost-5.7.20.tar.gz
#编写Dockerfile
vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <chips>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j2 && make install
ADD my.cnf /etc/
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin  mysql && chown mysql:mysql /etc/my.cnf && chown -R mysql:mysql /usr/local/mysql/ && /usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data && cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
#修改my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash

[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

3.准备依赖文件,配置php

#创建php工作目录,上传php-7.1.10的软件包
mkdir /opt/compose_lnmp/php/
[root@localhost1 php]#ls
Dockerfile  php-7.1.10.tar.bz2  php-fpm.conf  php.ini  www.conf
#编写Dockerfile
FROM centos:7
MAINTAINER this is php image <chips>
RUN yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel && useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make -j2 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini     /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
#修改php.ini文件
#1170行
mysqli.default_socket = /usr/local/mysql/mysql.sock
#939行
date.timezone = Asia/Shanghai
#修改php-fpm.conf文件
#19行取消注释
pid = run/php-fpm.pid
#修改www.conf文件
#23,24行修改
user = nginx
group = nginx
#36行修改为监听php地址的9000端口
listen = 172.18.0.30:9000
#62行允许的客户端,添加监听地址为nginx的地址
listen.allowed_clients = 127.0.0.1,172.18.0.10

4.编写docker-compose.yml文件

#docker-compose.yml放在服务同级目录
[root@localhost1 compose_lnmp]#ls
docker-compose.yml  mysql  nginx  php  wwwroot
#编写
vim docker-compose.yml
#定义docker-compose版本,可以是2 或 3
version: '3'

#使用services定义服务
services:
  #第一个service
  nginx:
    #设置容器名
    container_name: nginx
    #使用dockerfile来构建镜像
    build:
      #指定Dockerfile文件所在位置
      context: ./nginx
      #指定文件名
      dockerfile: Dockerfile
    #映射端口
    ports:
    - 80:80
    - 443:443
    #设置数据卷挂载
    volumes:
    - ./wwwroot:/usr/local/nginx/html
    #加入网络
    networks:
      lnmp:
        ipv4_address: 172.18.0.10

  mysql:
    container_name: mysql
    build:
      context: ./mysql
      dockerfile: Dockerfile
    ports:
    - 3306:3306
    networks:
      lnmp:
        ipv4_address: 172.18.0.20
    volumes:
    - db-data:/usr/local/mysql
    privileged: true

  php:
    container_name: php
    build:
      context: ./php
      dockerfile: Dockerfile
    ports:
    - 9000:9000
    networks:
      lnmp:
        ipv4_address: 172.18.0.30
    volumes:
    - db-data:/usr/local/mysql
    - ./wwwroot:/usr/local/nginx/html
    #指定依赖,php需要最后启动
    depends_on:
    - nginx
    - mysql

#设置网络为自定义网络
networks:
  lnmp:
    driver: bridge
    ipam:
      config:
      - subnet: 172.18.0.0/16

volumes:
  db-data:

5.准备测试项目WordPress

6.docker-compose构建项目(等待约30min)

查看状态(全up则成功)

7.授权数据库,访问192.168.116.10/wordpress/index.php/

Guess you like

Origin blog.csdn.net/weixin_58544496/article/details/128037254