在这篇博文中,我们将介绍如何对 codeigniter 3 应用程序进行 docker 化。在本指南结束时,您将拥有一个使用 apache、php 和 mysql 运行的容器化应用程序,所有这些都通过 docker compose 进行管理。这种方法将简化您的开发环境并确保跨多个系统的设置一致。
先决条件在我们深入了解详细信息之前,请确保您已安装以下工具:
- docker:容器化应用程序及其依赖项。
- docker compose:管理多容器 docker 应用程序。
- codeigniter 3:您现有的 codeigniter 3 项目。
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 步:访问应用程序
容器启动后,请在网络浏览器中访问 http://localhost:8080。如果一切设置正确,您的 codeigniter 3 应用程序应该可以在 docker 容器内顺利运行。
第 6 步:管理 docker 容器要停止容器,请运行:
docker-compose down
结论
在本指南中,我们成功对 codeigniter 3 应用程序进行了 docker 化,使其可移植且易于管理。 docker compose 允许我们轻松定义和运行多容器应用程序,使其非常适合开发和生产环境。
通过使用 docker,您可以确保所有开发人员拥有一致的环境,并轻松地将应用程序部署到各种系统,而无需担心依赖关系。如果您希望扩展您的应用程序或在云环境中运行它,docker 使其管理起来非常简单。
以上就是Dockerize CodeIgniter 分步指南的详细内容,更多请关注知识资源分享宝库其它相关文章!
版权声明
本站内容来源于互联网搬运,
仅限用于小范围内传播学习,请在下载后24小时内删除,
如果有侵权内容、不妥之处,请第一时间联系我们删除。敬请谅解!
E-mail:dpw1001@163.com
发表评论