Loading... ## 背景 本地开发thinkphp可以使用docker-compose来配置一个本地的开发环境,要是使用到的镜像有php,nginx,mysql,phpmyadmin,主要用的是前两个,后面两个可以根据项目来判断是否需要。 文件目录如下 ![](https://minio-api.open.yantao.wiki:16666/blog-image/2024/10/05/6700d3ad77836.png) 1. docker文件夹放dockerfile和其他配置文件 2. mysql是mysql的数据文件夹 3. docker-compose.yml 首先我们创建 `Docker` 文件夹,并在里面创建 `docker-compose.yml` 文件 这里说明下,因为我的 `Docker` 文件夹是和Thinkphp代码是在一个目录下的,所以映射的时候直接是`../:/var/www/html` ```yaml version: '3.9' services: db: image: mysql:5.7 container_name: bbs-mysql platform: linux/amd64 volumes: - ./mysql:/var/lib/mysql - ./init.sql:/docker-entrypoint-initdb.d/init.sql # 初始化脚本 restart: always ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: bbs MYSQL_DATABASE: bbs MYSQL_USER: bbs MYSQL_PASSWORD: bbs MYSQL_OPTIONS: "--sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION" depends_on: - php nginx: build: ./nginx/ container_name: bbs-nginx ports: - "1234:80" volumes: - ../:/var/www/html - ../logs:/var/log/nginx/ links: - php php: build: ./php/ container_name: bbs-php expose: - 9000 volumes: - ../:/var/www/html phpmyadmin: image: phpmyadmin container_name: bbs-phpmyadmin restart: always ports: - "8080:80" depends_on: - db ``` ### 创建Nginx编译 ```yaml FROM nginx:latest COPY ./default.conf /etc/nginx/conf.d/default.conf ``` `default.conf` ```bash server { listen 80 default_server; root /var/www/html/public; index index.html index.php; charset utf-8; location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; } } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log off; error_log /var/log/nginx/error.log error; sendfile off; client_max_body_size 100m; location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REQUEST_SCHEME $scheme; fastcgi_param HTTPS $https if_not_empty; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200; set $real_script_name $fastcgi_script_name; if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") { set $real_script_name $1; set $path_info $2; } fastcgi_param SCRIPT_FILENAME $document_root$real_script_name; fastcgi_param SCRIPT_NAME $real_script_name; fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors off; fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; } location ~ /.ht { deny all; } } ``` ### 创建PHP环境 ```bash FROM php:7.4-fpm RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" RUN docker-php-ext-install mysqli pdo pdo_mysql RUN sed -i 's/;extension=pdo_mysql/extension=pdo_mysql/g' "$PHP_INI_DIR/php.ini" ``` ## 编译 ```bash docker-compose build --no-cache ``` ## 启动 ```bash docker-compose up -d ``` 最后修改:2024 年 10 月 14 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏