Docker is a powerful tool that allows developers to easily create and manage isolated environments for their applications. In this guide, we will walk through the process of setting up a Docker environment for PHP development that includes MySQL, Composer, and Nginx.
Step 1: Install Docker and Docker Compose
The first step is to install Docker and Docker Compose on your machine. You can download the Docker Desktop for Mac or Windows, or for Linux you can follow the instructions for your specific distribution. Here are the instruction for install it on linux ubuntu
To install Docker:
- Update the system:
sudo apt update
- Install dependencies:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
- Add Docker GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Add Docker repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- Update package index:
sudo apt update
- Install Docker:
sudo apt install docker-ce
To install Docker Compose:
- Download the binary:
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- Apply executable permissions:
sudo chmod +x /usr/local/bin/docker-compose
- Verify the installation:
Step 2: Create a new directory for your project
Create a new directory for your project and navigate into it. This will be the root of your project and where all of your code and configuration files will be stored.
Step 3: Create a Dockerfile
Create a new file called “Dockerfile” in your project’s root directory. This file will contain the instructions for building your Docker image.
Here is an example of a basic Dockerfile for PHP development:
FROM php:7.4-fpm RUN apt-get update && apt-get install -y \ libpq-dev \ libzip-dev \ zip \ unzip RUN docker-php-ext-install pdo_mysql \ && docker-php-ext-configure zip --with-libzip \ && docker-php-ext-install zip COPY . /var/www/html WORKDIR /var/www/html CMD ["php-fpm"]
This Dockerfile starts from the official PHP 7.4 FPM image, installs the necessary dependencies for using PDO with MySQL and the Zip extension, copies the contents of the current directory to the webroot, and sets the working directory to the webroot.
FROM php:7.4-fpmspecifies the base image to be used for building the Docker image. In this case, it’s using the PHP 7.4 FPM (FastCGI Process Manager) image from the official PHP Docker repository.
RUN apt-get update && apt-get install -yupdates the package list and installs the dependencies listed after it:
RUN docker-php-ext-install pdo_mysqlinstalls the PDO (PHP Data Objects) extension for MySQL.
docker-php-ext-configure zip --with-libzipconfigures the zip extension to use libzip.
docker-php-ext-install zipinstalls the zip extension.
COPY . /var/www/htmlcopies all the files in the current directory to the /var/www/html directory in the Docker image.
WORKDIR /var/www/htmlsets the working directory to /var/www/html, where the application files will be located.
CMD ["php-fpm"]specifies the command to run when the Docker image is started. In this case, it’s running the php-fpm service.
Step 4: Create a docker-compose.yml file
Create a new file called “docker-compose.yml” in your project’s root directory. This file will define the services that make up your application and how they are connected.
Here is an example of a basic docker-compose.yml file for PHP development:
version: '3' services: web: build: . ports: - "9000:9000" volumes: - .:/var/www/html db: image: mysql:5.7 ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: mydb MYSQL_USER: myuser MYSQL_PASSWORD: mypassword nginx: image: nginx:latest ports: - "80:80" volumes: - .:/var/www/html - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
The file docker-compose.yml is used to define and run multi-container Docker applications with Docker Compose. The content in the file describes the services needed to run the application and how they should be configured.
version: '3'specifies the version of the Docker Compose file format used. In this case, it’s using version 3.
services:section defines the services needed for the application:
web:section describes the service that runs the web application.
build: .specifies that the Docker image should be built using the Dockerfile in the current directory.
ports:maps the host’s port 9000 to the container’s port 9000, allowing external access to the web service.
volumes:mounts the current directory to the /var/www/html directory in the container, allowing changes to the code to take effect immediately.
db:section describes the service that runs the database.
image: mysql:5.7specifies the MySQL 5.7 image to be used for the database service.
ports:maps the host’s port 3306 to the container’s port 3306, allowing external access to the database.
environment:sets the environment variables for the database service, including the root password, database name, user, and user password.
nginx:section describes the service that runs the web server.
image: nginx:latestspecifies the latest version of the Nginx image to be used for the web server service.
ports:maps the host’s port 80 to the container’s port 80, allowing external access to the web server.
volumes:mounts the current directory to the /var/www/html directory in the container and the nginx/default.conf file to the /etc/nginx/conf.d/default.conf file in the container, allowing changes to the configuration to take effect immediately.
This file defines three services: web, db, and nginx. The web service uses the Dockerfile in the current directory, maps port 9000 to the host, and mounts the current directory as a volume. The db service uses the official MySQL 5.7 image, maps port 3306 to the host, and sets the necessary environment variables for the root password, database name, user, and password. The nginx service uses the official Nginx image, maps port 80 to the host, and mounts the current