Use docker-compose to build lnmpr environment

This article is participating in the "Golden Stone Project. Share 60,000 cash prize"

The environment of this article is docker20.10, PHP8.1 (including extensions) + Nginx1.22 + MySQL8.0 + Mongo6.0 + Redis6.0 + Swoole2.0 If you
don’t understand, you can comment or contact me Email: [email protected]
Copyright belongs to All of Owen Zhang. For commercial reprint, please contact OwenZhang for authorization, for non-commercial reprint, please indicate the source.

Use docker-compose to build lnmpr environment

introduce

docker-compose builds PHP8.1 (including extensions) + Nginx1.22 + MySQL8.0 + Mongo6.0 + Redis6.0 + Swoole2.0

Docker is an open source application container engine, based on the  Go language  and open source in compliance with the Apache2.0 protocol.

Docker allows developers to package their applications and dependencies into a lightweight, portable container, which can then be distributed to any popular Linux machine, and can also be virtualized.

The container is completely using the sandbox mechanism, and there will be no interface between them (similar to iPhone apps), and more importantly, the performance overhead of the container is extremely low.

Gitee address: gitee.com/owenzhang24…

grateful

Added some functions and plug-ins on the basis of kingsfeng GitHub address: github.com/kingsfeng/d…

Software Architecture

docker-compose builds the LNMP environment mapping file directory, clones to the specified composer_lnmp74 directory, and can be installed with one click

Introduction to Compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use YML files to configure all the services your application needs. Then, with a single command, all services can be created and started from the YML file configuration.

If you don't know YML file configuration yet, you can read the  YAML Getting Started tutorial first .

The three steps used by Compose:

  • Use a Dockerfile to define your application's environment.
  • Use docker-compose.yml to define the services that make up your application so they can run together in isolation.
  • Finally, execute the docker-compose up command to get the entire application up and running.

Installation Tutorial

  1. git clone https://gitee.com/owenzhang24/docker_compose_lnmp.git lnmp
  2. cd lnmp
  3. docker-compose build
  4. docker-compose up -d

Specific file content docker-compose.yml

version: "3.8"
# 定义四个服务nginx,php,mysql,redis
# php-fpm和php-cli可以选着1个安装,看项目,2个一起安装也可以

services:

  redis:
    image: redis:6.0
    container_name: lnmp_redis
    build: ./redis
    volumes:
      - ./redis/redis.conf/:/etc/redis.conf:ro
      - ./redis/data:/usr/local/redis/data
      - ./redis/redis.log:/usr/local/redis/redis.log
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "6379:6379"
    command: [ "redis-server", "/etc/redis.conf" ]
    networks:
      - backend

  mysql:
    image: mysql:8.0
    container_name: lnmp_mysql
    volumes:
      - ./mysql/conf.d:/etc/mysql/my.cnf:ro
      - ./mysql/data:/var/lib/mysql
      - ./mysql/log:/data/mysql/logs
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - TZ=Asia/Shanghai
#    platform: linux/x86_64 #针对mac M1芯片
    ports:
      - "3306:3306"
    networks:
          - backend

  mongo:
    image: mongo:6.0
    container_name: lnmp_mongo
    volumes:
      - ./mongo/data:/data/db
      - ./mongo/log:/var/log/mongodb
    environment:
      - MONGO_INITDB_ROOT_USERNAME=owenweb
      - MONGO_INITDB_ROOT_PASSWORD=owenweb
      - TZ=Asia/Shanghai
    ports:
      - "27017:27017"
    networks:
          - backend

  php:
    image: php:8.1-fpm
    container_name: lnmp_php
    build: ./php/extension
    #防止启动php失败
    stdin_open: true
    #防止启动php失败
    tty: true
    depends_on:
      - "mysql"
      - "redis"
      - "mongo"
    volumes:
      - ./nginx/www:/var/www/html
      - ./php/log:/var/log/php
      - ./php/etc/php.ini:/usr/local/etc/php/php.ini
#      - ./php/etc/php-fpm.conf:/usr/local/etc/php-fpm.conf    # 映射配置文件
#      - ./php/etc/php-fpm.d:/usr/local/etc/php-fpm.d
    links:
      - mysql:mysql
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "9000:9000"
    networks:
          - frontend
          - backend

  phpcli:
    image: php:8.1-cli
    container_name: lnmp_phpcli
    build: ./php/extension-phpcli
    #防止启动php失败
    stdin_open: true
    #防止启动php失败
    tty: true
    depends_on:
      - "mysql"
      - "redis"
      - "mongo"
    volumes:
      - ./nginx/www:/var/www/html
      - ./php/log:/var/log/php
      - ./php/etc/php.ini:/usr/local/etc/php/php.ini
    #      - ./php/etc/php-fpm.conf:/usr/local/etc/php-fpm.conf    # 映射配置文件
    #      - ./php/etc/php-fpm.d:/usr/local/etc/php-fpm.d
    links:
      - mysql:mysql
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "8241:8241"
    networks:
      - frontend
      - backend

  nginx:
    image: nginx:1.22
    container_name: lnmp_nginx
    # 依赖关系 先跑php nginx必须依赖php的,所以要用depends_on
    depends_on:
      - "php"
    volumes:
      - ./nginx/ssl/:/etc/nginx/ssl/
      - ./nginx/conf:/etc/nginx/nginx/  # 主配置文件
      - ./nginx/www:/usr/share/nginx/html  # 项目目录
      - ./nginx/log:/var/log/nginx    # 日志
    links:
      - php:php
      - phpcli:phpcli
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "80:80"
      - "443:443"
    networks:
          - frontend

  node:
    image: node:18.0
    container_name: lnmp_node
    volumes:
      - ./node:/node
    environment:
      - TZ=Asia/Shanghai
    ports:
       - "3000:3000"
    command: [ "tail", "-f", "/dev/null" ]
    networks:
      - backend


networks:
  frontend:
  backend:
复制代码

If the installation fails or the configuration file modification causes the build to fail, you can perform the following steps and then rebuild

  1. docker-compose stop

Click y to confirm and delete all containers (execute carefully if there are other containers in the environment)

  1. docker-compose rm

Delete all images (execute carefully if there are other images in the environment)

  1. docker rmi $(docker images -q)

Instructions for use

  1. /docker_compose_lnmp/php/extension/dockerfile 是PHP8.1的常用扩展,包括mysqli、gd、mcrypt、zip、redis、memcache、mongodb、swoole等等

  2. 在/docker_compose_lnmp/ 目录下执行安装命令

Buy me a cup of coffee :)

If you think it is helpful to you, please give me a reward, thank you!

WeChat appreciation code link, click to jump:

www.owenzhang.com/wechat_rewa…

This article is participating in the "Golden Stone Project. Share 60,000 cash prize"

Guess you like

Origin juejin.im/post/7166914465579925512