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!

docker中rm和rmi的区别:rm命令用于删除一个或者多个容器,而rmi命令用于删除一个或者多个镜像;rm命令的语法为“docker rm [OPTIONS] CONTAINER [CONTAINER...]”,rmi命令的语法为“docker rmi [OPTIONS] IMAGE [IMAGE...]”。

docker官方镜像有:1、nginx,一个高性能的HTTP和反向代理服务;2、alpine,一个面向安全应用的轻量级Linux发行版;3、busybox,一个集成了三百多个常用Linux命令和工具的软件;4、ubuntu;5、PHP等等。

docker对于小型企业、个人、教育和非商业开源项目来说是免费的;2021年8月31日,docker宣布“Docker Desktop”将转变“Docker Personal”,将只免费提供给小型企业、个人、教育和非商业开源项目使用,对于其他用例则需要付费订阅。

docker容器重启后数据会丢失的;但是可以利用volume或者“data container”来实现数据持久化,在容器关闭之后可以利用“-v”或者“–volumes-from”重新使用以前的数据,docker也可挂载宿主机磁盘目录,用来永久存储数据。

AUFS是docker最早支持的存储引擎。AUFS是一种Union File System,是文件级的存储驱动,是Docker早期用的存储驱动,是Docker18.06版本之前,Ubuntu14.04版本前推荐的,支持xfs、ext4文件。

docker能安装oracle。安装方法:1、拉取Oracle官方镜像,可以利用“docker images”查看镜像;2、启动容器后利用“docker exec -it oracle11g bash”进入容器,并且编辑环境变量;3、利用“sqlplus /nolog”进入oracle命令行即可。

解决方法:1、停止docker服务后,利用“rsync -avz /var/lib/docker 大磁盘目录/docker/lib/”将docker迁移到大容量磁盘中;2、编辑“/etc/docker/daemon.json”添加指定参数,将docker的目录迁移绑定;3、重载和重启docker服务即可。

docker中的镜像会自动更新;可以利用Watchtower工具来自动更新镜像,Watchtower是一个可以监控正在运行的容器镜像是否更新的工具,当本地镜像与远程镜像有差异的时候,可以自动使用当前容器的运行参数以新镜像重新创建一个新的容器,并删除旧的容器。


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.