Table of Contents
1. Install Docker
Download and install
Configuration Image acceleration
2. Configure the installation environment
Install centos (can be skipped)
Install Mysql5.7
Install php7.4.5
Install nginx1.16.1
Configuration docker-compose
Others
Install redis
Home Backend Development PHP Problem How to configure LNMP development environment with Docker on Mac

How to configure LNMP development environment with Docker on Mac

Jul 05, 2021 am 10:13 AM
docker lnmp macos php

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

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: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

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.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

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 and the Web: Exploring its Long-Term Impact PHP and the Web: Exploring its Long-Term Impact Apr 16, 2025 am 12:17 AM

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.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

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)

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

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.

How to view logs from docker How to view logs from docker Apr 15, 2025 pm 12:24 PM

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

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

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]

See all articles