DEV Community

Cover image for Codeigniter 4 with Docker on AWS Cloud9
fadilxcoder
fadilxcoder

Posted on

Codeigniter 4 with Docker on AWS Cloud9

Hey devs 💻, I've been spending my days during lockdown trying get familiar with Docker & Amazon Web Services (AWS) Cloud9.

As a PHP developer, I once was using Codeigniter 3 for developing applications. Then it came to my attention that Codeigniter 4 (CI4) was released.

So I decided to combine these 3 and by using AWS - Ubuntu Server 18.04 LTS Platform, I will use Docker and have CI4 working on it.

My AWS environment settings are as follows :

  • Environment type : EC2
  • Platform : Ubuntu Server 18.04 LTS

Let the trip begin 😎

In your bash terminal on AWS, just to check the php version, we won't be using this one though.

ubuntu:~/environment $ php -v
Enter fullscreen mode Exit fullscreen mode

Normally you should see php version 7.2,

PHP version
PHP version

I actually spent couple of days trying to built a nice Docker Environment comprising of

  • nginx
  • php-pfm
  • database

It is available on Github

GitHub logo fadilxcoder / docker-env

Docker Environment Configuration

NGINX / APACHE / PHP-FPM / ADMINER

Example :

FROM php:7.2-cli

ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

RUN chmod +x /usr/local/bin/install-php-extensions && \
    install-php-extensions gd xdebug
  • To verify if extension is present, use command : php -m|grep mysql for example
  • Connect to App Terminal : docker exec -it fx_php_fpm ash
  • Verify if container is UP : docker ps

NOTES

DETAILS

  • PHP : 8.1.5
  • NGINX : nginx/1.21.6
  • MySQL version : 10.7.3-MariaDB-1:10.7.3+maria~focal

URL







Clone the repository and move the files to the root of your project.

ubuntu:~/environment $ git clone https://github.com/fadilxcoder/docker-env.git
ubuntu:~/environment $ mv docker-env/{*,.*} ./
ubuntu:~/environment $ rmdir docker-env/
Enter fullscreen mode Exit fullscreen mode

AWS
AWS

Okay, from here i'll explain to you the file structure.

  • In src folder, we will add only the application codes. It can be html, js, css, php files or assets (images / favicon). Overall , our codes goes in this folder.

  • In docker folder, it consist of a docker-compose file where we have the configuration parameters for docker and other Dockerfile each for an image. Depending on your application, you might need to updated these files before building it.

Now we will get CI4 working!

Let the trip continue 👽

You will have to edit the Dockerfile in php-fpm as CI4 uses the intl php extension.

RUN chmod uga+x /usr/local/bin/install-php-extensions && sync && \
    install-php-extensions gd xdebug mysqli intl
Enter fullscreen mode Exit fullscreen mode

Ans also in docker-compose.yml, change port as AWS by default uses port 8080.

    ports:
      - "8080:80"
Enter fullscreen mode Exit fullscreen mode

AWS Environment does not have docker-compose command by default, you will need to install it by running sudo apt install docker-compose.

Once installed, go to docker repository and run :

ubuntu:~/environment/docker $ docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

It might take some time as it will have to download the image if it is not present.

Open up another terminal in your AWS and check the container status whether it is up and running,

ubuntu:~/environment/docker $ docker ps
Enter fullscreen mode Exit fullscreen mode

AWS Docker Container
AWS Docker Container

Now you will need to connect to your container terminal,

ubuntu:~/environment $ docker exec -it fx_php_fpm ash
/var/www/html # composer create-project codeigniter4/appstarter ci4
Enter fullscreen mode Exit fullscreen mode

The installation will start...

CI4 in Docker Container
CI4 in Docker Container

95% complete 😁

In the src folder, uncomment the phpinfo(); save the file and the preview your application.

php version 7.4.5
php version 7.4.5

To preview your CI4 application,

URL : http://xxxxxxxxxxxxxxxxxxxxxxxxx.vfs.cloud9.us-east-2.amazonaws.com/ci4/public/

Codeigniter 4
Codeigniter 4

One problem that you might come across is that you may not be able to edit the code of the codeigniter app.

But no worry 😃

Use the 2 commands below to change files ownership & group.

ubuntu:~/environment/src $ sudo chown -R ubuntu ci4/
ubuntu:~/environment/src $ sudo chgrp -R ubuntu ci4/
Enter fullscreen mode Exit fullscreen mode

File Permission
File permission

Trip's over ✨✨✨ 👊 ❤️ ✌️ ✨✨✨

Now we have a fully functional docker container with Codeigniter on AWS C9 and you can code on it anytime/ anywhere...

We did it folks

Top comments (3)

Collapse
 
efleurine profile image
Emmanuel • Edited

After cloning I would suggest to remove the .git folder because the moving command will move it also
and every other file in the home directory will get check in the repo
So

ubuntu:~/environment $ git clone https://github.com/fadilxcoder/docker-env.git
ubuntu:~/environment $ cd docker-env/ && rm -rf .git && cd ..
ubuntu:~/environment $ mv docker-env/{*,.*} ./
ubuntu:~/environment $ rmdir docker-env/
Enter fullscreen mode Exit fullscreen mode
Collapse
 
efleurine profile image
Emmanuel

Or even better to create an app folder and move inside first

ubuntu:~/environment $ mkdir app && cd app
ubuntu:~/environment $ git clone https://github.com/fadilxcoder/docker-env.git
ubuntu:~/environment $ mv docker-env/{*,.*} ./
ubuntu:~/environment $ rmdir docker-env/
Enter fullscreen mode Exit fullscreen mode
Collapse
 
efleurine profile image
Emmanuel

This port configuration worked for me

 ports:
      - "80:80"
Enter fullscreen mode Exit fullscreen mode