


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.
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" ] }
View configuration statusdocker 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
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
Install Mysql5.7
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
Install php7.4.5
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
Install nginx1.16.1
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; } }
Configuration docker-compose
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
Others
Install redis
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]
