How to containerise CodeIgniter project using Docker?
Asked Answered
O

3

6

I'm very new to Docker and I have a CodeIgniter project that I have been working on via my localhost (XAMPP) up until now. I now want to host my project on Docker using GCP.

Would anyone be able to provide guidance as to how I would write a docker-compose.yml to containerise the project with redis, php, mysql and and nginx containers? Also how would I need my CI project structured for it to work?

Ommatophore answered 15/10, 2020 at 12:20 Comment(3)
You can try hub.docker.com/r/bitnami/codeigniterDirectorate
Thanks for the heads up @TekNath, I think it would be pretty neat to know how to do it manually too thoughOmmatophore
See ibexoft.com/setup-codeigniter-docker-container-for-development for the instructions.Symphonia
P
16

I use docker with Codeigniter 4 on daily basis. Here's my structure, although in my structure I'm not using neither redis or nginx. I'm using apache instead.

Folder structure:

.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

As for the config files, here's the 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

The 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

my site.conf

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

On my youtube series about codeigniter 4 I created a github repo that reflects this structure:

Pincer answered 15/10, 2020 at 13:48 Comment(3)
Wow okay, thanks for the help man. Very much appreciated.Ommatophore
custom.ini what containts?Auger
@D4ITON basically it will have everything you want to override on your normal php.ini settings. So it will be anything you want. Just add it there, and rebuild the docker container. Sorry for the late replayPincer
S
1

I came across this question when I was learning docker and wanted to leave an update here. I'm using bitnami/codeigniter3. This was easy for me as I did not have to migrate my application to Codeigniter 4.

Simferopol answered 4/8, 2021 at 13:42 Comment(1)
This container would also work for codeigniter 3.Pincer
S
0

Recently I was having trouble with same thing but somehow I figure it out and I used the below code. Also, I have setup MySQL on a different port as I was running another app on 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

Also, added to my github repo. The git code include has some commented code for CI4.

Stearne answered 21/9, 2022 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.