このブログ投稿では、CodeIgniter 3 アプリケーションを Dockerize する方法について説明します。このガイドを終えると、Apache、PHP、MySQL で実行されるコンテナ化されたアプリケーションがすべて Docker Compose で管理されるようになります。このアプローチにより、開発環境が合理化され、複数のシステム間で一貫したセットアップが確保されます。
前提条件
詳細を説明する前に、次のツールがインストールされていることを確認してください:
- Docker: アプリケーションとその依存関係をコンテナ化します。
- Docker Compose: マルチコンテナ Docker アプリケーションを管理します。
- CodeIgniter 3: 既存の CodeIgniter 3 プロジェクト。
ステップ 1: Dockerfile のセットアップ:
Dockerfile は、アプリケーションが実行される環境を定義します。設定方法は次のとおりです:
# Use an official PHP image with Apache FROM php:8.2-apache # Enable Apache mod_rewrite for CodeIgniter RUN a2enmod rewrite # Set the working directory in the container WORKDIR /var/www/html # Copy project files into the container COPY . /var/www/html # Install necessary PHP extensions RUN docker-php-ext-install mysqli # Set proper permissions for Apache to access files RUN chown -R www-data:www-data /var/www/html && chmod -R 755 /var/www/html # Expose port 80 EXPOSE 80
ステップ 2: Docker Compose のセットアップ
次に、Web アプリケーションとデータベースの両方に対して複数のコンテナを構成して実行する docker-compose.yml ファイルを定義しましょう。
version: '3.8' services: app: build: context: . dockerfile: Dockerfile container_name: ci3-docker # Set the container name here ports: - "8080:80" # Map port 80 of the container to port 8080 on the host volumes: - .:/var/www/html # Mount current directory to /var/www/html inside the container depends_on: - db # Ensure the database is up before starting the application db: image: mysql:8.0 # Uses the official MySQL image container_name: mysql restart: always environment: MYSQL_ROOT_PASSWORD: root # Root password for MySQL MYSQL_DATABASE: ci3docker # Initial database to create ports: - "3306:3306" # Expose port 3306 for database connections volumes: - db_data:/var/lib/mysql # Persist MySQL data volumes: db_data: name: ci3-docker # Name the volume for MySQL data persistence
ステップ 3: コンテナを構築して実行する
Dockerfile ファイルと docker-compose.yml ファイルの準備ができたら、コンテナを構築して実行します。プロジェクトのルートでターミナルを開き、次のコマンドを実行します:
Docker イメージをビルドします:
docker-compose build
コンテナを開始します:
docker-compose up
これにより、CodeIgniter アプリケーションと MySQL データベースの両方が起動します。アプリコンテナは http://localhost:8080 でアクセスでき、MySQL データベースはポート 3306 で実行されます。
ステップ 4: CodeIgniter データベース構成を更新する
ここで、CodeIgniter がコンテナ内の MySQL データベースに接続できることを確認しましょう。 application/config/database.php を開き、データベース接続設定を更新します。
$db['default'] = array( 'dsn' => '', 'hostname' => 'db', // Service name from Docker Compose 'username' => 'root', 'password' => 'root', // Password set in docker-compose.yml 'database' => 'ci3docker', // Database name set in docker-compose.yml 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
ステップ 5: アプリケーションにアクセスする
コンテナが起動したら、Web ブラウザで http://localhost:8080 にアクセスします。すべてが正しく設定されていれば、CodeIgniter 3 アプリケーションは Docker コンテナ内でスムーズに実行されるはずです。
ステップ 6: Docker コンテナの管理
コンテナを停止するには、次のコマンドを実行します:
docker-compose down
結論
このガイドでは、CodeIgniter 3 アプリケーションの Docker 化に成功し、ポータブルで管理が容易になりました。 Docker Compose を使用すると、マルチコンテナ アプリケーションを簡単に定義して実行できるため、開発環境や運用環境に最適です。
Docker を使用すると、すべての開発者にとって一貫した環境が確保され、依存関係を気にせずにアプリケーションをさまざまなシステムに簡単にデプロイできます。アプリケーションを拡張したり、クラウド環境で実行したりする場合、Docker を使用すると管理が驚くほど簡単になります。
以上がDockerize CodeIgniter ステップバイステップ ガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

tomakephpapplicationsfaster、followthesesteps:1)useopcodecachinglikeopcacheTostoredscriptbytecode.2)最小化abasequeriesecachingingindexing.3)leveragephp7機能forbettercodeefficiency.4)

依存性注入(DI)は、明示的に推移的な依存関係によりPHPコードのテスト可能性を大幅に改善します。 1)DI分離クラスと特定の実装により、テストとメンテナンスが柔軟になります。 2)3つのタイプのうち、コンストラクターは、状態を一貫性に保つために明示的な式依存性を注入します。 3)DIコンテナを使用して複雑な依存関係を管理し、コードの品質と開発効率を向上させます。

DatabaseQueryoptimizationInpholvesseveralstrategESTOEnhancePerformance.1)selectonlynlynlyndorycolumnStoredatedataTransfer.2)useindexingtospeedupdataretrieval.3)revenmecrycachingtostoreres sultsoffrequent queries.4)

phpisusededemingemailsduetoitsbuilt-inmail()functionandsupportiveLibrarieslikephpmailerandswiftmailer.1)usethemail()functionforbasicemails、butithaslimitations.2)emploadforadvancedfeatureSlikelikelivableabableabuses.3)雇用

PHPパフォーマンスボトルネックは、次の手順で解決できます。1)パフォーマンス分析にXdebugまたはBlackfireを使用して問題を見つける。 2)データベースクエリを最適化し、APCUなどのキャッシュを使用します。 3)array_filterなどの効率的な関数を使用して、配列操作を最適化します。 4)bytecodeキャッシュ用のopcacheを構成します。 5)HTTP要求の削減や写真の最適化など、フロントエンドを最適化します。 6)パフォーマンスを継続的に監視および最適化します。これらの方法により、PHPアプリケーションのパフォーマンスを大幅に改善できます。

依存関係(di)inphpisadesignpatternativats anducesclassodulencies、拡張測定性、テスト可能性、および維持可能性。

cachingemprovesppperformancebystring of computationsorquickretrieval、還元装置の削減は、reducingerloadendenhancersponseTimes.efcectivestrategiesInclude:1)opcodecaching、compiledphpscriptsinmemorytoskipcompilation;


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。
