在這篇部落格文章中,我們將演練如何 Dockerize CodeIgniter 3 應用程式。在本指南結束時,您將擁有一個使用 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
現在讓我們定義一個 docker-compose.yml 文件,它將為您的 Web 應用程式和資料庫配置和運行多個容器。
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中文網其他相關文章!

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TOOPTIMIZEPHPAPPLICITIONSFORPERSTORANCE,USECACHING,數據庫imization,opcodecaching和SererverConfiguration.1)InlumentCachingWithApcutCutoredSatfetchTimes.2)優化的atabasesbasesebasesebasesbasesbasesbaysbysbyIndexing,BeallancingAndWriteExing

依賴性注射inphpisadesignpatternthatenhancesFlexibility,可檢驗性和ManiaginabilybyByByByByByExternalDependencEctenceScoupling.itallowsforloosecoupling,EasiererTestingThroughMocking,andModularDesign,andModularDesign,butquirscarecarefulscarefullsstructoringDovairing voavoidOverOver-Inje

PHP性能優化可以通過以下步驟實現:1)在腳本頂部使用require_once或include_once減少文件加載次數;2)使用預處理語句和批處理減少數據庫查詢次數;3)配置OPcache進行opcode緩存;4)啟用並配置PHP-FPM優化進程管理;5)使用CDN分發靜態資源;6)使用Xdebug或Blackfire進行代碼性能分析;7)選擇高效的數據結構如數組;8)編寫模塊化代碼以優化執行。

opcodecachingsimplovesphperforvesphpermance bycachingCompiledCode,reducingServerLoadAndResponSetimes.1)itstorescompiledphpcodeinmemory,bypassingparsingparsingparsingandcompiling.2)useopcachebachebachebachebachebachebachebysettingparametersinphametersinphp.ini,likeememeryconmorysmorysmeryplement.33)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。