Containerization in DevOps: The Role of Docker | by Anuradha Iyer | Sep, 2024

  1. Create the Base Directory:
mkdir nginx-php-mysql-app

2. Create the HTML Subdirectory:

mkdir nginx-php-mysql-app/html

3. Create the Files:

  • Create nginx.conf in the base directory:
touch nginx-php-mysql-app/nginx.conf
  • Create docker-compose.yml in the base directory:
touch nginx-php-mysql-app/docker-compose.yml
  • Create connection.php, main.html, and index.php in the html directory:
touch nginx-php-mysql-app/html/connection.php
touch nginx-php-mysql-app/html/main.html
touch nginx-php-mysql-app/html/index.php

4. Add Content to Each File:

server {
listen 80;

root /usr/share/nginx/html; # Ensure this path matches where your files are

index index.php index.html index.htm main.html;

location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include fastcgi_params;
fastcgi_pass php:9000; # Assuming PHP service is named 'php' in docker-compose
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

connection.php:

// Database configuration
$host = 'db'; // Use the service name defined in docker-compose.yml
$user = 'test_user'; // MySQL user
$password = 'user_password'; // MySQL password
$database = 'test_db'; // Database name

// Create connection
$conn = new mysqli($host, $user, $password, $database);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

main.html:




Welcome to Nginx!

This is a sample web page served by Nginx.

For more information, visit the Nginx website.


index.php:

// Include the connection file
include 'connection.php';

// Perform a simple query
$sql = "SELECT DATABASE() AS current_db";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// Output the current database name
while ($row = $result->fetch_assoc()) {
echo "Current database: " . $row['current_db'];
}
} else {
echo "No results found.";
}

// Close the connection
$conn->close();
?>

docker-compose.yml:

Contents of the Docker Compose File

Here’s an example of a docker-compose.yml file for a simple web application that includes a web server, app server and a database:

version: '3.8'

services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- db

php:
image: php:7.4-fpm
volumes:
- ./html:/usr/share/nginx/html
depends_on:
- db
environment:
PHP_INI_SCAN_DIR: "/usr/local/etc/php/conf.d"
command: >
sh -c "docker-php-ext-install mysqli && php-fpm"

db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: test_db
MYSQL_USER: test_user
MYSQL_PASSWORD: user_password
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql

volumes:
db_data:

  1. Version
version: '3.8'

This specifies the version of the Docker Compose file format to use. Version 3.8 is compatible with Docker 1.27.0 and above.

2. Services This section defines the individual services that make up the application.

a. Web Service (Nginx)

web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- db
  • image: Specifies the Docker image to use, in this case, the latest version of Nginx.
  • ports: Maps port 80 inside the container to port 80 on the host machine. This allows you to access the web server via http://
  • volumes:
  • ./html:/usr/share/nginx/html: Maps the html directory from the host to the container, allowing you to serve static files (like HTML).
  • ./nginx.conf:/etc/nginx/conf.d/default.conf: Maps a custom Nginx configuration file to the container.
  • depends_on: Specifies that this service depends on the php and db services. Docker will ensure that these services are started before this one.

b. PHP Service

php:
image: php:7.4-fpm
volumes:
- ./html:/usr/share/nginx/html
depends_on:
- db
environment:
PHP_INI_SCAN_DIR: "/usr/local/etc/php/conf.d"
command: >
sh -c "docker-php-ext-install mysqli && php-fpm"
  • image: Uses the PHP FPM (FastCGI Process Manager) image, version 7.4.
  • volumes: Maps the same html directory from the host to the container for serving PHP files.
  • depends_on: Indicates that the PHP service depends on the db service to ensure the database is available before the PHP service starts.
  • environment: Sets the PHP_INI_SCAN_DIR environment variable to point to the directory for custom PHP configuration files.
  • command: This runs a shell command to install the MySQLi extension and then start the PHP-FPM process.

c. Database Service (MySQL)

db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: test_db
MYSQL_USER: test_user
MYSQL_PASSWORD: user_password
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
  • image: Uses the MySQL 5.7 image.
  • environment: Sets up environment variables for the MySQL root password, initial database, user, and password.
  • MYSQL_ROOT_PASSWORD: Password for the root user.
  • MYSQL_DATABASE: A database that will be created on startup.
  • MYSQL_USER: A new user with access to the created database.
  • MYSQL_PASSWORD: Password for the newly created user.
  • ports: Maps port 3306 of the MySQL server to port 3306 on the host, allowing external connections to the database.
  • volumes: Persists MySQL data using a named volume (db_data), ensuring that the data remains intact even if the container is removed.

3. Volumes

volumes:
db_data:
  • This section defines the named volume db_data, which is used to persist MySQL data. Named volumes are managed by Docker and allow data to persist between container restarts and removals.

Set Permissions:

Change permissions for the base directory:

  • Command: chmod 755 nginx-php-mysql-demo

Change permissions for the html directory:

  • Command: chmod 755 nginx-php-mysql-demo/html

Change permissions for the files in the html directory:

  • Command: chmod 644 nginx-php-mysql-demo/html/*

Change permissions for nginx.conf:

  • Command: chmod 644 nginx-php-mysql-demo/nginx.conf

Change permissions for docker-compose.yml:

  • Command: chmod 644 nginx-php-mysql-demo/docker-compose.yml

Here are the commands you can use to manage your docker-compose.yml setup for running the NGINX, PHP, and MySQL services:

1. Navigate to Your Project Directory:

cd /path/to/nginx-php-mysql-demo

2. Start Services in the Background:

This will pull the images, create the containers, and start the services in the background.

docker-compose up -d

3. Check Running Containers:

To verify that all your services (NGINX, PHP, MySQL) are running.

docker-compose ps

4. Stop the Services:

Gracefully stop the containers without removing them.

docker-compose stop

5. Shut Down the Services and Remove Containers:

This will stop the services and remove the containers, networks, and volumes created by docker-compose.

docker-compose down

6. Rebuild the Containers (Optional):

If you’ve made changes to the Dockerfile or docker-compose.yml and want to rebuild the containers.

docker-compose up -d --build

7. Check Logs:

To monitor the logs from the containers in real-time.

docker-compose logs -f

8. View Specific Service Logs:

If you want to see logs for a specific service, for example, web (NGINX):

docker-compose logs -f web

9. Access the Running Containers:

To access the shell of the running php or nginx containers.

docker exec -it  bash
docker exec -it  bash

10. Validate the Docker Compose Configuration:

Before bringing up the services, validate the docker-compose.yml file.

docker-compose config

These commands will help you manage the lifecycle of your NGINX, PHP, and MySQL services using docker-compose.

You can access the nginx web server by navigating to http:// in your browser.