PrestaShop Docker Setup: MySQL & PhpMyAdmin Guide

by Alex Johnson 50 views

In this comprehensive guide, we will walk you through the process of creating a fully functional Docker-based development environment for PrestaShop, complete with a MySQL database and phpMyAdmin for efficient database management. This setup is designed to streamline your development workflow, ensuring consistency and ease of use across different environments. By the end of this article, you'll have a robust, containerized environment ready for PrestaShop development.

Understanding the Requirements

The primary goal is to establish a Docker environment utilizing Docker Compose to orchestrate multiple containers seamlessly. Our setup will include three core components:

  1. PrestaShop 1.7.8 Container: This container will host the PrestaShop application, running from pre-existing project files. This approach allows for a quick start, bypassing the need for a fresh installation each time the environment is set up.
  2. MySQL Container: A dedicated MySQL container will serve as the database backend for PrestaShop. The container will be configured to automatically import a provided database dump (dump.sql) upon its initial startup, ensuring that the database is populated with all necessary data.
  3. phpMyAdmin Container: To facilitate database management, a phpMyAdmin container will be included. This will provide a web-based interface to interact with the MySQL database directly, without requiring additional configuration.

When the command docker compose up -d is executed, the environment should initialize and launch without any manual intervention. PrestaShop should boot directly using the committed project files from the repository. The MySQL database must be pre-populated with the complete store structure and data, including an admin user account, Polish language configuration, and all records necessary for PrestaShop to function correctly from the outset. phpMyAdmin should offer seamless access to the MySQL database, and both PrestaShop and MySQL data must be persisted using Docker volumes to prevent data loss across container restarts.

Step-by-Step Implementation

1. Preparing the docker-compose.yml File

The cornerstone of our Docker environment is the docker-compose.yml file. This file defines the services, networks, and volumes that make up our application stack. Let’s break down the key components of this file:

version: "3.8"
services:
  prestashop:
    image: prestashop/prestashop:1.7.8
    ports:
      - "8000:80"
    environment:
      - DB_SERVER=mysql
      - DB_NAME=prestashop
      - DB_USER=prestashop
      - DB_PASS=prestashop
      - PS_INSTALL_AUTO=1
    volumes:
      - prestashop_data:/var/www/html
    depends_on:
      - mysql
    restart: always

  mysql:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=prestashop
      - MYSQL_USER=prestashop
      - MYSQL_PASSWORD=prestashop
    volumes:
      - mysql_data:/var/lib/mysql
      - ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
    restart: always

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - "8080:80"
    environment:
      - PMA_HOST=mysql
      - PMA_USER=root
      - PMA_PASSWORD=root
    depends_on:
      - mysql
    restart: always

volumes:
  prestashop_data:
  mysql_data:
  • PrestaShop Service: This section defines the PrestaShop container. It uses the official PrestaShop 1.7.8 image, maps port 8000 on the host to port 80 on the container, and sets several environment variables. DB_SERVER, DB_NAME, DB_USER, and DB_PASS configure the database connection. PS_INSTALL_AUTO=1 is crucial as it instructs PrestaShop to skip the installation process and boot using existing project files. The prestashop_data volume ensures that PrestaShop files are persisted across container restarts. The depends_on directive ensures that the MySQL container is running before PrestaShop starts. The restart: always policy ensures that the container restarts automatically if it crashes.
  • MySQL Service: This section configures the MySQL container. It uses the MySQL 5.7 image and sets environment variables for the root password, database name, user, and password. The mysql_data volume persists the MySQL data, and the ./dump.sql:/docker-entrypoint-initdb.d/dump.sql line is key—it tells Docker to import the dump.sql file into the database upon the container's first startup. This allows us to pre-populate the database with the necessary data. The restart: always policy ensures that the container restarts automatically if it crashes.
  • phpMyAdmin Service: This section sets up the phpMyAdmin container. It uses the phpmyadmin/phpmyadmin image, maps port 8080 on the host to port 80 on the container, and sets environment variables to connect to the MySQL database. The depends_on directive ensures that the MySQL container is running before phpMyAdmin starts. The restart: always policy ensures that the container restarts automatically if it crashes.
  • Volumes: The volumes section defines the named volumes prestashop_data and mysql_data. These volumes are used to persist data across container restarts, ensuring that your PrestaShop files and MySQL data are not lost.

2. Configuring MySQL to Import dump.sql

The automatic import of the dump.sql file is handled by Docker’s built-in initialization scripts. By mounting the dump.sql file to the /docker-entrypoint-initdb.d/ directory in the MySQL container, Docker automatically executes the SQL script during the first container startup. This process ensures that the database is pre-populated with your store’s data, eliminating the need for manual import steps.

3. Adding Volumes for Data Persistence

Data persistence is critical in a development environment. Docker volumes allow us to persist data even when containers are stopped or removed. In our docker-compose.yml file, we've defined two volumes:

  • prestashop_data: This volume is mapped to the /var/www/html directory in the PrestaShop container, ensuring that your PrestaShop files are persisted.
  • mysql_data: This volume is mapped to the /var/lib/mysql directory in the MySQL container, ensuring that your database data is persisted.

By using named volumes, Docker manages the storage location, and you don't need to worry about the underlying file system paths. This simplifies the management of your data and ensures consistency across different environments.

4. Configuring PrestaShop to Skip Installation

To ensure that PrestaShop boots directly from the existing project files, we set the PS_INSTALL_AUTO environment variable to 1 in the PrestaShop service definition. This tells PrestaShop to skip the installation process and use the existing configuration and data. Additionally, make sure your committed project files include the app/config/parameters.php file with the correct database connection details. This file should reflect the database settings defined in the docker-compose.yml file, such as the database host, name, user, and password.

5. Verifying the Setup

Once you have the docker-compose.yml file, the dump.sql file, and the PrestaShop project files in place, you can start the environment by running the following command in the directory containing the docker-compose.yml file:

docker compose up -d

This command builds and starts the containers in detached mode, meaning they run in the background. After a few minutes, the environment should be up and running. You can verify the setup by following these steps:

  • Access PrestaShop: Open your web browser and navigate to http://localhost:8000. You should see your PrestaShop store loaded correctly.

  • Access phpMyAdmin: Open another browser tab and navigate to http://localhost:8080. You should see the phpMyAdmin login page. Use the root credentials defined in the docker-compose.yml file (root as the username and password) to log in. Once logged in, you should see the PrestaShop database and its tables.

  • Verify Data Persistence: To ensure that data persistence is working correctly, try adding a product or making other changes in PrestaShop. Then, stop and restart the containers using the following commands:

    docker compose down
    docker compose up -d
    

    After the containers restart, verify that your changes are still present in PrestaShop.

6. Addressing UTF-8 and Polish Characters

Ensuring correct display of UTF-8 characters, especially Polish characters, requires careful configuration of the database and PrestaShop settings. The dump.sql file should be encoded in UTF-8, and the database collation should be set to a UTF-8 variant (e.g., utf8mb4_unicode_ci). Additionally, PrestaShop’s language settings should be configured to use Polish, and the default locale should be set to a UTF-8-compatible locale (e.g., pl_PL.UTF-8).

7. Final Quality Assurance

Before considering the setup complete, perform a final quality assurance check. Restart the containers multiple times to ensure that data persists and everything loads properly. Test various functionalities of PrestaShop, such as adding products, placing orders, and managing customers. Verify that phpMyAdmin provides consistent access to the database and that all data is displayed correctly.

Deliverables

To complete this task, the following deliverables are required:

  • A fully functional docker-compose.yml file that defines the entire environment.
  • An example .env file (if environment variables are used) that provides default values for sensitive configuration settings.
  • A dump.sql file containing the complete store data, including the admin user account and Polish language configuration.
  • A README file with clear instructions for running the environment, including any necessary prerequisites and troubleshooting tips.

Example .env File

If you choose to use environment variables for sensitive configuration settings, you can create a .env file in the same directory as the docker-compose.yml file. Here’s an example of what the .env file might look like:

MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=prestashop
MYSQL_USER=prestashop
MYSQL_PASSWORD=prestashop

In your docker-compose.yml file, you can reference these environment variables using the ${VARIABLE_NAME} syntax. For example:

mysql:
  image: mysql:5.7
  environment:
    - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    - MYSQL_DATABASE=${MYSQL_DATABASE}
    - MYSQL_USER=${MYSQL_USER}
    - MYSQL_PASSWORD=${MYSQL_PASSWORD}

README Instructions

A clear and concise README file is essential for anyone who will be using the environment. The README should include the following information:

  • Prerequisites: List any software or tools that need to be installed before running the environment (e.g., Docker, Docker Compose).
  • Installation Instructions: Provide step-by-step instructions for setting up the environment, including how to clone the repository, create the .env file (if applicable), and run the docker compose up -d command.
  • Accessing the Applications: Explain how to access PrestaShop and phpMyAdmin in the browser, including the URLs and any default credentials.
  • Troubleshooting Tips: Include common issues that users might encounter and how to resolve them (e.g., port conflicts, database connection errors).
  • Data Persistence: Explain how data persistence works using Docker volumes and how to back up and restore data.

Conclusion

Setting up a Docker environment for PrestaShop, MySQL, and phpMyAdmin streamlines the development process and ensures consistency across different environments. By following the steps outlined in this guide, you can create a robust, containerized environment ready for PrestaShop development. Remember to verify each component and perform thorough testing to ensure everything functions correctly. This setup not only simplifies the development workflow but also provides a scalable and maintainable solution for your PrestaShop projects.

For further learning and advanced configurations with Docker, you may find valuable resources on the official Docker Documentation website.