(第 3 部分)从头开始为 PHP 框架创建 Docker 环境

(第 3 部分)从头开始为 PHP 框架创建 Docker 环境

我正在此环境下运行此 docker composer 文件:

version: "3.1"
services:
  mysql:
    container_name: mysql
    image: 'mysql:8.0'
    working_dir: /application
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - '.:/application'
    environment:
      - MYSQL_ROOT_PASSWORD=1qazxsw22
      - MYSQL_DATABASE=minos
      - MYSQL_PASSWORD=1qazxsw22
    ports:
      - "3308:3306"
  webserver:
    container_name: webserver
    image: 'nginx:alpine'
    working_dir: /application
    volumes:
      - '.:/application'
      - './conf-example/nginx/nginx.conf:/etc/nginx/conf.d/default.conf'
    ports:
      - '8008:80'
    depends_on:
      - mysql
    links:
      - mysql:minos
  php-fpm:
    build: conf-example/php-fpm
    working_dir: /application
    volumes:
      - '.:/application'
      - './conf-example/php-fpm/php-ini-overrides.ini:/etc/php/8.1/fpm/conf.d/99-overrides.ini'

Dockerfile 安装如下所示:

FROM phpdockerio/php:8.1-fpm
LABEL maintainer="ChaosEmperorDragonEnvoyoftheEnd"
LABEL version="1.0.0"

WORKDIR "/application"

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV DATE_TIMEZONE UTC
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update; \
    apt-get -y --no-install-recommends install \
        build-essential \
        curl \
        git \
        mysql-client \
        nano \
        unzip \
        wget \
        zip \
        apache2 \
        libapache2-mod-php \
        libmcrypt-dev \
        cron \
        locales \
        php8.1 \
        php8.1-fpm \
        php8.1-cli \
        php8.1-gd \
        php8.1-common \
        php8.1-curl \
        php8.1-imap \
        php8.1-intl \
        php8.1-json \
        php8.1-mailparse \
        php8.1-mbstring \
        php8.1-mysql \
        php8.1-pear \
        php8.1-readline \
        php8.1-soap \
        php8.1-xml \
        php8.1-xmlrpc \
        php8.1-zip \
        php8.1-bz2 \
        php8.1-dba \
        php8.1-ldap \
        php8.1-dev \
        php8.1-xdebug \
    apt-get clean; \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

实现一个 web 服务器灯栈,但是当我尝试运行 index.php 从 php 连接到 mysql 时,如下所示:

<?php

try {
    $dbh = new PDO(
        'mysql:mysql:3308;dbname=minos',
        'minos',
        '1qazxsw22',
        array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
    );
} catch (PDOException $e) {
    echo $e->getMessage();
}

我收到此错误:

找不到驱动程序

我需要在代码或安装中做哪些更改才能消除此连接错误?

答案1

尝试这样运行:

RUN apt update \
    && apt -y --no-install-recommends install php-mysql php-curl php-intl php-gd php-common php-redis php-sodium git \
    && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

将 php8.1-lib 更改为 php-lib

相关内容