How to: Deploy CodeIgniter4 on Docker Emman, November 6, 2023November 6, 2023 Prerequisites You need to have Docker and Docker Compose installed on your system. You can follow the official documentation for Docker and Docker Compose to install them. You need to have a CodeIgniter4 project that you want to deploy. You can use the CodeIgniter4 User Guide to learn how to create one, or use an existing project from GitHub, such as this one. Steps Create a docker folder in the root of your project, and inside it create three subfolders: apache, mysql, and php. In the docker folder, create a docker-compose.yml file with the following content: 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 This file defines two services: web and mysql. The web service uses a custom Dockerfile to build a PHP image with Apache and CodeIgniter4 dependencies. The mysql service uses the official MySQL image and sets the root password to root. Both services use volumes to persist data and expose ports for communication. In the docker/php folder, create a Dockerfile with the following content: FROM php:7.4-apache RUN apt-get update && \ apt-get install -y curl && \ apt-get install -y build-essential libssl-dev zlib1g-dev libpng-dev libjpeg-dev libfreetype6-dev && \ apt-get install -y libicu-dev COPY sites-available/site.conf /etc/apache2/sites-enabled/site.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 This file instructs Docker to use the php:7.4-apache image as the base image and install the necessary packages and extensions for CodeIgniter4. It also copies a custom Apache configuration file and enables the rewrite module. In the docker/php/sites-available folder, create a site.conf file with the following content: <VirtualHost *:80> DocumentRoot "/var/www/html/app/public/" ServerName ci4.local <Directory "/var/www/html/app/public/"> AllowOverride all </Directory> </VirtualHost> This file defines the virtual host for the CodeIgniter4 application and sets the document root to the public folder. In the docker/php folder, create a custom.ini file with the following content: date.timezone = UTC This file sets the timezone for PHP to UTC. In the docker/mysql folder, create a db.sql file with the SQL commands to create and populate your database. For example, you can use the following commands to create a ci4 database and a users table: CREATE DATABASE ci4; USE ci4; CREATE TABLE users ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, PRIMARY KEY (id) ); INSERT INTO users (name, email, password) VALUES ('Alice', 'alice@example.com', 'password'); INSERT INTO users (name, email, password) VALUES ('Bob', 'bob@example.com', 'password'); In the app folder, create a .env file with the following content: # CI_ENVIRONMENT = production app.baseURL = 'http://localhost:80/' database.default.hostname = db-ci4 database.default.database = ci4 database.default.username = root database.default.password = root database.default.DBDriver = MySQLi This file sets the environment variables for CodeIgniter4, such as the base URL, the database connection details, and the database driver. Note that the hostname for the database is the same as the container name defined in the docker-compose.yml file. From the docker folder, run the following command to build and run the containers: docker-compose up -d This command will create and start the web and mysql containers in the background. You can use the following command to check the status of the containers: docker-compose ps You should see something like this: Name Command State Ports ----------------------------------------------------------------------- ci4-web docker-php-entrypoint apac ... Up 0.0.0.0:80->80/tcp db-ci4 docker-entrypoint.sh --def ... Up 0.0.0.0:3306->3306/tcp To access your CodeIgniter4 application, open your browser and go to http://localhost:80/. You should see the welcome page of CodeIgniter4. To access your MySQL database, you can use any MySQL client and connect to localhost:3306 with the username root and the password root. You should see the ci4 database and the users table that you created. Conclusion You have successfully deployed your CodeIgniter4 project on Docker using Apache and MySQL. You can now use Docker to manage your development and production environments. You can also customize your Docker configuration to suit your needs, such as adding more services, changing the ports, or using different images. I hope you found this tutorial helpful and informative. If you have any questions or feedback, please leave comments below. 😊 Share this:FacebookX Related Discover more from Code Concepts Snippets Subscribe to get the latest posts sent to your email. Type your email… Subscribe CodeIgniter CodeIgniter4 Docker PHP