如何使用Docker将CodeIgniter项目容器化?

5

我对Docker非常新手,目前一直通过本地主机(XAMPP)在开发我的CodeIgniter项目。现在我想通过GCP在Docker上托管我的项目。

请问有没有人能够提供指导,告诉我如何编写docker-compose.yml文件来将项目容器化,并包含redis、php、mysql和nginx容器?另外,为了使它正常工作,我的CI项目需要如何组织结构?


你可以尝试使用 https://hub.docker.com/r/bitnami/codeigniter - Tek Nath Acharya
谢谢你提醒我,@TekNath。我认为手动学习这个也很有意思。 - rs2020
请访问 https://www.ibexoft.com/setup-codeigniter-docker-container-for-development/ 了解说明。 - Jawaid
3个回答

16

我每天都使用 Docker 来配合 Codeigniter 4 进行开发。这里是我的文件结构,虽然我并没有在此结构中使用 Redis 或 Nginx,而是使用了 Apache。

文件夹结构:

.database
.docker
  |php
     |sites-available
       |site.conf
     |Dockerfile
  |custom.ini
  |docker-compose.yml
.git
app
  |app
  |public
  |tests
  |vendor
  |writable
  |.env
  |composer.json
  |composer.lock
  |spark
.gitignore

关于配置文件,这是docker-compose.yml

version: '3'
services:
    web:
        container_name: ci4-web
        build:
            context: ./php
        ports:
            - 80:80
        volumes:
            - ../app:/var/www/html/app/
            - ./custom.ini:/usr/local/etc/php/conf.d/custom.ini
        links:
            - mysql
        depends_on:
          - mysql
    mysql:
        container_name: db-ci4
        image: mysql:latest
        volumes:
            - ./db:/var/lib/mysql
        command: --default-authentication-plugin=mysql_native_password
        ports:
            - 3306:3306
        environment:
            MYSQL_ROOT_PASSWORD: root
Dockerfile文件。
FROM php:7.2-apache
RUN apt-get update && \
    apt-get install -y
RUN apt-get install -y curl
RUN apt-get install -y build-essential libssl-dev zlib1g-dev libpng-dev libjpeg-dev libfreetype6-dev
RUN apt-get install -y libicu-dev
COPY sites-available/elioter.conf /etc/apache2/sites-enabled/elioter.conf
RUN apt-get update
RUN docker-php-ext-install intl
RUN docker-php-ext-configure intl
RUN docker-php-ext-install mysqli pdo pdo_mysql zip mbstring
RUN a2enmod rewrite
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install gd
RUN service apache2 restart

我的site.conf

<VirtualHost *:80>
    DocumentRoot "/var/www/html/app/public/"
    ServerName ci4.local
    <Directory "/var/www/html/app/public/">
        AllowOverride all
    </Directory>
</VirtualHost>

在我关于CodeIgniter 4的Youtube系列中,我创建了一个反映这种结构的Github存储库:


2
哇,好的,谢谢你的帮助。非常感激。 - rs2020
custom.ini包含什么内容? - D4ITON

1

当我学习Docker并想要留下更新时,我遇到了这个问题。我正在使用bitnami/codeigniter3。对我来说很容易,因为我不必将我的应用程序迁移到Codeigniter 4。


这个容器也适用于CodeIgniter 3。 - marcogmonteiro

0

最近我遇到了同样的问题,但是我想出了解决方法并使用了以下代码。此外,我已经将MySQL设置在不同的端口上,因为我正在运行另一个应用程序在3306端口上。

version: "1.0"
services:
  webserver:
    image: thecodingmachine/php:7.4-v3-apache-node12
    container_name: test-webserver
    working_dir: /var/www/html
    environment:
      PHP_EXTENSIONS: apcu pdo_mysql opcache redis zip gd yaml exif xdebug
      PHP_EXTENSION_GD: 1
      PHP_EXTENSION_MYSQLI: 1
      APACHE_DOCUMENT_ROOT: app
      APACHE_RUN_GROUP: www-data # use www-data user in container.. which is also used on most webservers for deployer
      APACHE_RUN_USER: www-data
      PHP_EXTENSION_XDEBUG: 1
    depends_on:
      - mysql
    volumes:
      - ./:/var/www/html
      - ~/.ssh:/root/.ssh
    stdin_open: true
    tty: true
    ports:
      - "9090:80"
  mysql:
    image: mysql:5.7
    container_name: test-mysql
    restart: unless-stopped
    ports:
      - "3307:3307"
    expose:
      - 3307
    environment:
      MYSQL_ROOT_PASSWORD: MYSQL_ALLOW_EMPTY_PASSWORD
      MYSQL_DATABASE: test-db
      MYSQL_USER: testuser
      MYSQL_PASSWORD: password
      MYSQL_TCP_PORT: 3307
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: unless-stopped
    container_name: test-phpadmin
    environment:
      PMA_HOST: mysql
      PMA_USER: testuser
      PMA_PASSWORD: password
      PMA_PORT: 3307
      UPLOAD_LIMIT: 300M
    ports:
      - 9091:80

另外,已添加到我的 Github repo。Git 代码包含了一些针对 CI4 的注释代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接