Home>Article>Backend Development> How to configure LNMP development environment with Docker on Mac
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.
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.
Download address https://download.docker.com/mac/stable/Docker.dmg
Preferences >> Docker Engine
{ "registry-mirrors": [ "https://registry.docker-cn.com", "http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn" ] }
View configuration statusdocker info
View the image version https://hub.docker.com/_/centos?tab=tags
Pull the image
docker pull centos:centos7.8.2003
View Mirror
docker images
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 以特权模式运行容器(可以运行后台服务)
View container
docker ps // -l 查看历史容器
Enter container
docker exec -it 46e9810a35e6 bash
Update image (submit container copy)
docker commit -m "test update" 21e09cfcc692 centos:test
Delete Mirror
docker rmi centos:test
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
Pull the image
docker pull mysql:5.7
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
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 可能会碰到权限问题,需要加参数
Enter the container
docker exec -it mysql_test /bin/bash
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
Under Mac
#查看用户组: dscl . list /groups #添加用户组: sudo dscl . -create /Groups/docker 添加user到group: sudo dscl . -append /Groups/docker GroupMembership username
Pull the image
docker pull php:7.4.5-fpm
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 \
Construct image
docker build -t="php:7.4.5v2" .
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
Command description
-v 将本地磁盘上的php代码挂载到docker 环境中,对应docker的目录是 /var/www/html/ --name 新建的容器名称 php-with-mysql --link 链接的容器,链接的容器名称:在该容器中的别名,运行这个容器是,docker中会自动添加一个host识别被链接的容器ip --privileged=true 权限问题
Remarks
nproc内核参数,是系统上的最大进程数。 $(nproc)是获取安装系统的该内核参数。常用的还有获取文件路径的命令$(pwd)
Extension related
# 查看已开启扩展 php -m # 查看现有可以启动的扩展 ls /usr/local/lib/php/extensions/no-debug-non-zts-20190902/ # 启动扩展 docker-php-ext-enable opcache # 安装并启动扩展
Reference Docker php Detailed explanation of installation extension steps
Pull image
docker pull nginx:1.16.1
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
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 的配置文件替换,挂载本地编写的配置文件
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; } }
File structure tree
. ├── docker-compose.yml ├── mysql │ └── Dockerfile ├── nginx │ ├── Dockerfile │ └── conf │ └── default.conf ├── phpfpm │ └── Dockerfile └── res └── index.php
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
Mysql Dockerfile
FROM mysql:5.7
Nginx Dockerfile
FROM nginx:1.16.1
PHPFPM Dockerfile (the image built above is used directly here)
FROM php:7.4.5v2
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);
Remarkshost needs to write the mysql container name
Run
docker-compose up -d
Pull the image
docker pull redis
Create container
docker run -itd -p 6379:6379 redis
Enter container debugging
# redis-cli 127.0.0.1:6379> set name 1 OK 127.0.0.1:6379> get name "1"
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!