Inception
Based on the sources, here is a step-by-step plan to develop the 42 Inception project:
Project Overview and Prerequisites
-
Understand the Project Goal:
- The Inception project requires you to set up a small Docker-based infrastructure. This infrastructure must consist of NGINX, WordPress, and MariaDB services.
- The entire project needs to be done within a Virtual Machine.
- You must use Docker Compose for multi-container deployment and configuration.
- The infrastructure must comply with specific constraints, including custom configurations and security measures like TLS.
-
Prerequisites:
- Ensure Docker and Docker Compose are installed on your host machine.
- Have a basic understanding of Docker, NGINX, WordPress, and MariaDB.
Project Structure and General Guidelines
-
Set Up Directory Structure:
- Create a
srcsfolder at the root of your project directory; all configuration files must be placed inside it. - Place a
Makefileat the root of your directory. - Inside
srcs, include yourdocker-compose.ymlfile and a.envfile. - Create a
requirementsfolder withinsrcs, which will contain separate folders for each service (e.g.,mariadb,nginx,wordpress). - Each service folder (e.g.,
mariadb,nginx) should contain its ownDockerfileand aconfdirectory for service-specific configurations.
- Create a
-
Adhere to Docker Guidelines:
- Each Docker image must have the same name as its corresponding service.
- Each service has to run in a dedicated container.
- Containers must be built from the penultimate stable version of Debian Buster (or Alpine Linux).
- Write your own Dockerfiles for each service; do not pull ready-made Docker images (except the base OS).
- Ensure your containers are configured to restart in case of a crash (e.g.,
restart: alwaysindocker-compose.yml). - Do not use hacky patches like
tail -f,bash,sleep infinity, orwhile trueto keep containers running. - The use of
network: hostor--link/links:is forbidden. Anetworkline must be present in yourdocker-compose.yml. - The
latesttag for images is prohibited. - No passwords must be present in your Dockerfiles; use environment variables, preferably stored in a
.envfile located in thesrcsdirectory.
Mandatory Service Setup
-
Configure NGINX:
- Role: Acts as the reverse proxy and the only entry point into the infrastructure.
- Port & TLS: Listen on port 443 only, using TLSv1.2 or TLSv1.3 protocols.
- Dockerfile:
- Install
nginxandopenssl. - Create
/etc/nginx/ssldirectory. - Copy your custom
default.confto/etc/nginx/conf.d/default.conf. - Copy and execute a script (e.g.,
generate_cert.sh) to generate a self-signed SSL certificate (nginx.keyandnginx.crt) within the container. - Expose port 443.
- Ensure NGINX runs in the foreground (e.g.,
nginx -g "daemon off;").
- Install
nginx.conf: Handles static files and forwards PHP requests to WordPress (e.g.,fastcgi_pass wordpress:9000).
-
Configure WordPress:
- Role: PHP-based CMS served by NGINX and connected to MariaDB.
- Execution: Runs using PHP-FPM on port 9000.
- Installation: Automatically installs WordPress and sets up the database on first launch via a script.
- Users: Create two users in the WordPress database: an administrator and a regular user.
- The administrator's username cannot contain "admin" or "administrator" (e.g., "admin", "administrator", "Administrator", "admin-123" are forbidden). The
init_wp.shscript should check for this.
- The administrator's username cannot contain "admin" or "administrator" (e.g., "admin", "administrator", "Administrator", "admin-123" are forbidden). The
- Dockerfile:
- Install necessary packages:
net-tools,php-fpm,php-mysql,wget,unzip,curl,mariadb-client. - Download and set up WordPress files in
/var/www/html. - Configure PHP-FPM to listen on port 9000.
- Copy and execute an initialization script (e.g.,
init_wp.sh) as the main command.
- Install necessary packages:
init_wp.shscript:- Wait for MariaDB to be ready before proceeding.
- Create
wp-config.phpusing environment variables (e.g.,WORDPRESS_DB_NAME,WORDPRESS_DB_USER,WORDPRESS_DB_PASSWORD,MYSQL_DB_HOST). - Install WordPress core.
- Create the administrator user (e.g.,
WORDPRESS_ADMIN_USER,WORDPRESS_ADMIN_PASSWORD,WORDPRESS_ADMIN_EMAIL) and the regular user (e.g.,WORDPRESS_USER,WORDPRESS_USER_PASSWORD,WORDPRESS_USER_EMAIL).
-
Configure MariaDB:
- Role: Relational database system for storing WordPress data.
- Initialization: Initialized using a script (e.g.,
init_db.sh) to create the database and users specified in the.envfile. - Dockerfile:
- Install
mariadb-server. - Copy your
init_db.shscript to/usr/local/bin/init_db.shand make it executable. - Expose port 3306.
- Set the command to execute
init_db.sh.
- Install
init_db.shscript:- Initialize the MariaDB data directory if it doesn't exist.
- Modify the
bind-addressin MariaDB configuration (/etc/mysql/mariadb.conf.d/50-server.cnf) to0.0.0.0or comment it out to allow external connections. - Start the MariaDB server (e.g.,
mysqld_safe). - Create the WordPress database and users (
MYSQL_DATABASE,MYSQL_USER,MYSQL_PASSWORD,MYSQL_ADMIN_USER,MYSQL_ADMIN_PASSWORD) based on.envvariables, and grant necessary privileges.
-
Manage Volumes:
- Ensure persistent storage for WordPress files and MariaDB databases.
- Define two volumes in your
docker-compose.yml:- For MariaDB: Map to
/home/login/data/mariadbon the host machine (e.g.,/home/mfaoussi/data/mariadb) for database storage. - For WordPress: Map to
/home/login/data/wordpresson the host machine (e.g.,/home/mfaoussi/data/wordpress) for website files.
- For MariaDB: Map to
- These volumes should use
driver: localwithtype: noneando: bind. - The
WP_DATAandDB_DATAdirectories on the host machine should be created (e.g., usingmkdir -pin the Makefile).
-
Set Up Docker Network:
- Define a custom bridge network (e.g.,
inception) in yourdocker-compose.yml. - All containers (NGINX, WordPress, MariaDB) must be connected to this single network to establish communication.
- Define a custom bridge network (e.g.,
Environment Variables and Domain Configuration
-
Create
.envFile:- Inside your
srcsdirectory, create a.envfile to store all sensitive information and configuration variables. - Populate it with variables such as:
WORDPRESS_DB_NAME,WORDPRESS_DB_USER,WORDPRESS_DB_PASSWORD,WORDPRESS_DB_HOST.DOMAIN_NAME(e.g.,mfaoussi.42.fr).WORDPRESS_TITLE,WORDPRESS_ADMIN_USER,WORDPRESS_ADMIN_PASSWORD,WORDPRESS_ADMIN_EMAIL.WORDPRESS_USER,WORDPRESS_USER_PASSWORD,WORDPRESS_USER_EMAIL.MYSQL_DB_HOST,MYSQL_ROOT_PASSWORD,MYSQL_DATABASE,MYSQL_USER,MYSQL_PASSWORD,MYSQL_ADMIN_USER,MYSQL_ADMIN_PASSWORD.
- Inside your
-
Configure Domain Name:
- Alter your
/etc/hostsfile on the host machine to map your custom domain name (e.g.,mfaoussi.42.fr) to127.0.0.1. This domain name should belogin.42.frwhereloginis your actual login.
- Alter your
Makefile and Execution
- Develop the Makefile:
- The
Makefilemust be at the root of your project and set up your entire application by building Docker images usingdocker-compose.yml. - Include targets for common Docker Compose operations:
all: Default target, typically callsup.up: Builds and starts the services in detached mode (docker compose up -d). Should also create the volume data directories (WP_DATA,DB_DATA).down: Stops and removes containers, networks, images, and volumes.stop: Stops running services.start: Starts stopped services.build: Builds the service images.clean: Stops and removes all running containers, images, volumes, and networks, and also removes the WordPress and MariaDB data directories on the host.re: Rebuilds the infrastructure (e.g.,cleanthenup).prune: Cleans up unused Docker objects including volumes (docker system prune -a --volumes -f).
- The
Verification and Interaction
-
Build and Start Services:
- Execute
make upfrom your project root directory.
- Execute
-
Verify Setup:
- Access WordPress by navigating to
https://mfaoussi.42.fr(or your configuredDOMAIN_NAME) in a web browser. - Log in using the administrator credentials specified in your
.envfile.
- Access WordPress by navigating to
-
Interact with Services:
- Access MariaDB: From within its container, you can access the MariaDB prompt using
docker exec -it mariadb mysql -u root -p. - Access WordPress: Primarily through the web interface via NGINX.
- Access MariaDB: From within its container, you can access the MariaDB prompt using
Bonus Part (Optional)
Once the mandatory part is perfectly implemented and fully functional, you can consider the bonus part:
- Set up Redis cache for WordPress.
- Set up an FTP server container pointing to the WordPress volume.
- Create a simple static website (not PHP).
- Set up Adminer.
- Set up another service of your choice and be ready to justify it.
Remember that the bonus part will only be assessed if the mandatory part is integrally done and works without malfunctioning.