Codeigniter 4 Docker Development with Docker Compose is a great way to streamline your development process.
Docker Compose is a tool for defining and running multi-container Docker applications.
Using Docker Compose, you can manage multiple services and containers with ease, making it perfect for Codeigniter 4 projects.
By leveraging Docker Compose, you can quickly spin up a development environment, complete with a database and web server, with just a few commands.
This setup allows you to focus on coding, without worrying about the underlying infrastructure.
Docker Compose simplifies the process of setting up and managing your Codeigniter 4 project's dependencies, making it easier to collaborate with others and deploy your application.
Prerequisites
To get started with CodeIgniter 4 in Docker, you need to have a basic understanding of Docker and its components, including Docker Compose, which is used to define and run multi-container Docker applications.
Docker Compose is a tool for defining and running multi-container Docker applications.
You should have Docker installed on your system, as well as Docker Compose.
Docker provides a free trial and a free Community Edition (CE) that you can use for development and testing.
A basic understanding of CodeIgniter 4 is also required, including its directory structure and configuration files.
The CodeIgniter 4 framework uses a modular approach, with each module representing a feature or functionality.
You should also have a code editor or IDE installed on your system, such as Visual Studio Code or PHPStorm.
Setting Up
To set up a CodeIgniter 4|3 app with Docker, you need a working app. If you don't have one, download it from the CodeIgniter docs or set it up using your command line.
First, run your app and make sure the basic CodeIgniter app is working. This ensures that your setup is correct and you can proceed with the Docker configuration.
A Docker file will configure Apache for you. In this file, the following Apache command is important: RUN a2enmod rewrite to enable the Apache module mod_rewrite used for URL rewriting.RUN service apache2 restart to restart the Apache web server and apply the changes.COPY 000-default.conf /etc/apache2/sites-available/000-default.conf to copy the Apache configuration file.
You need to create a 000-default.conf file in your working directory and add your custom configuration settings for Apache. This file will let Apache run your CodeIgniter app on port 80.
The /var/www/html/public directory is key in your CodeIgniter app. This directory allows directory indexing, symbolic links, and .htaccess files to override Apache configuration.
Database
To get CodeIgniter up and running with a database, you'll need to use a MySQL container on Docker. This means updating the docker-compose.yaml file to include a MySQL container with phpMyAdmin for easy access.
You'll need to install mysqli in the Dockerfile to enable CodeIgniter to connect to MySQL. This involves adding a line to the Dockerfile to install mysqli.
For CodeIgniter to access MySQL, update the app\Config\Database.php file with the necessary variables, including hostname set to "db", the name of the Docker Compose service running MySQL server.
Update the app\Controllers\Home.php file with the MySQL database connection details, then rerun your Docker Compose to enable the connection.
You should now be able to access your CodeIgniter app at http://localhost:8080/ and confirm if it can connect to MySQL.
To access the MySQL database with phpMyAdmin, open http://localhost:8001/ and log in with the username "root" and password based on the MYSQL_ROOT_PASSWORD environment variable.
File and Project
To get started with CodeIgniter 4 and Docker, you'll need to create a Docker Compose file in your working directory. This file will package your whole application together.
Create a new file named docker-compose.yaml and run your container using the following command: `docker-compose up`. This will start Apache on port 80 and expose CodeIgniter on port 8080.
Open http://localhost:8080/ in your browser and your Dockerized CodeIgniter app should be ready.
Running with MySQL
You'll need a MySQL container for CodeIgniter to use. Update the docker-compose.yaml file to add a MySQL container.
We've added phpMyAdmin so you can access the database using the GUI. To connect CodeIgniter to MySQL, install mysqli in the Dockerfile.
In the app\Config\Database.php file, update the variables with the hostname set to "db". This is the name of the Docker Compose service running the MySQL server.
Update the app\Controllers\Home.php file with the MySQL database connection. Rerun your Docker Compose to make the changes take effect.
You can now access your CodeIgniter app at http://localhost:8080/ and confirm if it can connect to MySQL.
Creating a File
To create a Dockerfile, you need to create a file in your project working directory. The Dockerfile will contain all the instructions needed for Docker to get ready for CodeIgniter.
The base image for your Dockerfile is the official PHP 8.2 image, which includes an Apache server to run PHP CodeIgniter code. This is specified using the FROM php:8.2-apache command.
You can upgrade the installed packages to their latest versions using the RUN apt-get update && apt-get upgrade -y command.
To get Docker ready to install CodeIgniter required PHP libraries, you need to install the following packages: libicu-dev, libpng-dev, and libzip-dev. This is done using the RUN apt-get install -y command.
Here are the steps to prepare your Dockerfile:
- FROM php:8.2-apache
- RUN apt-get update && apt-get upgrade -y
- RUN apt-get install -y libicu-dev libpng-dev libzip-dev
Alternatively, you can create a Docker Compose file to package your whole application together. This file is named docker-compose.yaml and is created in your working directory.
Running
Running CodeIgniter on Docker is a straightforward process. You'll need to create a MySQL container for your database.
To get started, update your docker-compose.yaml file to include a MySQL container. This will allow CodeIgniter to connect to your database. You can then use phpMyAdmin to access and manage your database.
A MySQL container is essential for creating CRUD APIs in CodeIgniter. This container will store and manage your application's data. By adding phpMyAdmin, you can easily access and visualize your database using a GUI interface.
Sources
- https://thriveread.com/dockerize-codeigniter-with-docker-compose/
- https://codeigniter4.github.io/userguide/installation/running.html
- https://stackoverflow.com/questions/64371440/how-to-containerise-codeigniter-project-using-docker
- https://forum.codeigniter.com/post-367840.html
- https://forum.codeigniter.com/showthread.php
Featured Images: pexels.com