How to configure LNMP development environment with Docker on Mac

藏色散人
Release: 2023-03-11 08:18:02
Original
3291 people have browsed it

How to configure LNMP development environment with Docker under Mac: 1. Install Docker; 2. Configure the installation environment; 3. Install Mysql5.7; 4. Install php7.4.5; 5. Install nginx1.16.1; 6. Configure docker-compose.

How to configure LNMP development environment with Docker on Mac

The operating environment of this article: macOS10.15 system, php7.4.5 version, MacBook Air 2019 computer

Docker configuration LNMP under Mac Development environment

Foreword:
1. The standard usage of Docker is that each docker container only provides one service.
So it should be a separate container for mysql, a separate container for php-fpm, and a separate container for nginx.

2. The design concept of Docker is not to run background services in the container. The container itself is an independent main process on the host, which can also be indirectly understood as the application process running services in the container. The life cycle of a container revolves around this main process, so the correct way to use a container is to run the services inside it in the foreground.

1. Install Docker

Download and install

Download address https://download.docker.com/mac/stable/Docker.dmg

Configuration Image acceleration

Preferences >> Docker Engine

{
  "registry-mirrors": [
    "https://registry.docker-cn.com",
    "http://hub-mirror.c.163.com",
    "https://docker.mirrors.ustc.edu.cn"
  ]
}
Copy after login

View configuration status
docker info

2. Configure the installation environment

Install centos (can be skipped)

View the image version https://hub.docker.com/_/centos?tab=tags
Pull the image

docker pull centos:centos7.8.2003
Copy after login

View Mirror

docker images
Copy after login

Create container

docker run -v /data:/docker_data -p 80:80 -itd --privileged=true centos:v0.0.1 /usr/sbin/init
// -v 挂载路径 本地/data挂载到容器的/docker_data路径
// -p 端口映射
// -i 允许你对容器内的标准输入 (STDIN) 进行交互
// -t 在新容器内指定一个伪终端或终端
// -d 在后台运行
// --privileged=true 以特权模式运行容器(可以运行后台服务)
Copy after login

View container

docker ps // -l 查看历史容器
Copy after login

Enter container

docker exec -it 46e9810a35e6 bash
Copy after login

Update image (submit container copy)

docker commit -m "test update" 21e09cfcc692 centos:test
Copy after login

Delete Mirror

docker rmi centos:test
Copy after login

Problem 1: unable to remove repository reference "centos:test" (must force) - container 46e9810a35e6 is using its referenced image 5b5eb956a405
Solution: View and delete historical images

docker ps -l
docker rm 46e9810a35e6
Copy after login

Install Mysql5.7

Pull the image

docker pull mysql:5.7
Copy after login

Create container

docker run -p 3306:3306 --name mysql_test -v ~/Docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d --privileged=true mysql:5.7
Copy after login

Command instructions

-p 3306:3306:将容器的3306端口映射到主机的3306端口
-v PWD/mysql/data:/var/lib/mysql:将主机当前目录下的mysql/data文件夹挂载到容器的/var/lib/mysql 下,在mysql容器中产生的数据就会保存在本机mysql/data目录下(路径会自动创建)
-e MYSQL_ROOT_PASSWORD=passwd:初始化root用户的密码
-d 后台运行容器
--name 给容器指定别名
--privileged=true centos7 可能会碰到权限问题,需要加参数
Copy after login

Enter the container

docker exec -it mysql_test /bin/bash
Copy after login

How to add sudo to docker

1.创建docker组:sudo groupadd docker
2.将当前用户加入docker组:sudo gpasswd -a ${USER} docker
3.重启服务:sudo service docker restart
4.刷新docker成员:newgrp - docker
Copy after login

Under Mac

#查看用户组:
dscl . list /groups
#添加用户组:
sudo dscl . -create /Groups/docker
添加user到group:
sudo dscl . -append /Groups/docker GroupMembership username
Copy after login

Install php7.4.5

Pull the image

docker pull php:7.4.5-fpm
Copy after login

Create Dockerfile

vim Dockerfile

FROM  php:7.4.5-fpm
 RUN apt-get update && apt-get install -y \
 libfreetype6-dev \
 libjpeg62-turbo-dev \
 libpng12*-dev \
 && docker-php-ext-enable opcache \
 && docker-php-ext-install pdo_mysql \
 && docker-php-ext-install gd \
Copy after login

Construct image

docker build -t="php:7.4.5v2" .
Copy after login

Start

docker run -d -p 9000:9000 -v /var/www/html/:/var/www/html/ --name php-with-mysql --link mysql_test:mysql  --volumes-from mysql_test --privileged=true php-fpm5.6/v2
Copy after login

Command description

-v 将本地磁盘上的php代码挂载到docker 环境中,对应docker的目录是 /var/www/html/ 
--name 新建的容器名称 php-with-mysql
--link 链接的容器,链接的容器名称:在该容器中的别名,运行这个容器是,docker中会自动添加一个host识别被链接的容器ip
--privileged=true 权限问题
Copy after login

Remarks

nproc内核参数,是系统上的最大进程数。
$(nproc)是获取安装系统的该内核参数。常用的还有获取文件路径的命令$(pwd)
Copy after login

Extension related

# 查看已开启扩展
php -m
# 查看现有可以启动的扩展
ls /usr/local/lib/php/extensions/no-debug-non-zts-20190902/
# 启动扩展
docker-php-ext-enable opcache
# 安装并启动扩展
Copy after login

Reference Docker php Detailed explanation of installation extension steps

Install nginx1.16.1

Pull image

docker pull nginx:1.16.1
Copy after login

Create container

docker run -d --link php-with-mysql:phpfpm --volumes-from php-with-mysql -p 80:80 -v /var/www/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf --name nginx-php --privileged=true  nginx
Copy after login

Command description

--link php-with-mysql:phpfpm 将php容器链接到nginx容器里来,phpfpm是nginx容器里的别名。
--volumes-from php-with-mysql 将php-with-mysql 容器挂载的文件也同样挂载到nginx容器中
-v /var/www/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf  将nginx 的配置文件替换,挂载本地编写的配置文件
Copy after login

Default configuration

vim default.conf

server {
        listen       80;
        server_name  localhost;

        location / {
            root   /var/www/html;
            index  index.html index.htm index.php; # 增加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   phpfpm:9000;    # 修改为phpfpm容器
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; # 修改为$document_root
            include        fastcgi_params;
        }
    }
Copy after login

Configuration docker-compose

File structure tree

.
├── docker-compose.yml
├── mysql
│   └── Dockerfile
├── nginx
│   ├── Dockerfile
│   └── conf
│       └── default.conf
├── phpfpm
│   └── Dockerfile
└── res
    └── index.php
Copy after login

YAML configuration

vim docker-compose.yml 

nginx:
  build: ./nginx
  ports:
    - "80:80"
  links:
    - "phpfpm"
  volumes:
    - /Users/majun/docker/res:/var/www/html
    - /Users/majun/docker/nginx/conf:/etc/nginx/conf.d
phpfpm:
  build: ./phpfpm
  ports:
    - "9000:9000"
  volumes:
    - /Users/majun/docker/res:/var/www/html
  links:
    - "mysql"
mysql:
  build: ./mysql
  ports:
    - "3306:3306"
  volumes:
    - /Users/majun/docker/mysql/data:/var/lib/mysql
  environment:
    MYSQL_ROOT_PASSWORD : root
Copy after login

Mysql Dockerfile

FROM mysql:5.7
Copy after login

Nginx Dockerfile

FROM nginx:1.16.1
Copy after login

PHPFPM Dockerfile (the image built above is used directly here)

FROM php:7.4.5v2
Copy after login

PHP connection Mysql test

vim index.php

//PDO中的预处理1:sql语句中是: (别名的方式)的
header("Content-type:text/html;charset=utf-8");
//实例化PDO
try{
    $pdo = new PDO(
        "mysql:host=mysql;dbname=test",
        "root",
        "root"
    );
}catch(PDOException $pe){
    die("PDO实例失败!原因:".$pe->getMessage());
}
//定义sql语句
$sql = "select * from test";
//预处理sql
$stmt = $pdo->prepare($sql);
//执行
$stmt->execute();
// 获取多条数据
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($res);
Copy after login

Remarkshost needs to write the mysql container name

Run

docker-compose up -d
Copy after login

Others

Install redis

Pull the image

docker pull redis
Copy after login

Create container

docker run -itd -p 6379:6379 redis
Copy after login

Enter container debugging

# redis-cli
127.0.0.1:6379> set name 1
OK
127.0.0.1:6379> get name
"1"
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to configure LNMP development environment with Docker on Mac. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template