Install Prestashop & MySQL by using Docker containers

Install Prestashop & MySQL by using Docker containers

This quick post will show you how you can install and set up Prestashop by using only Docker containers.

ยท

4 min read

This quick post will show you how you can install and set up Prestashop by using only Docker containers.

Prestashop is one of the most popular eCommerce platforms used by many merchants to sell their products online. From the technical side, it has the cleanest database structure amongst other online shopping platforms. However, installation of Prestashop requires multiple dependencies such as MySQL database or XAMPP to launch it in the server.

DevOps is painful if we don't use modern tools to automate such tasks. So, rather than manual configuration, we can use Docker to run apps in containers, making it easy for deployment.

Before dive in, make sure you have a basic understanding of Docker and compose files since these details will be not covered in this post.

Alright! We'll have 3 containers in total:

  • Prestashop
  • MySQL
  • Phpmyadmin

Setting up Database

Let's start by creating our mysql service that will represent our database server:

version: '3.9'

services:
    mysql:
        image: mysql:8
        container_name: prestashop-db
        command: --default-authentication-plugin=mysql_native_password
        environment:
            MYSQL_DATABASE: prestashop
            MYSQL_ROOT_PASSWORD: prestashop
        ports:
            - 3306:3306
        networks:
            - prestashop
  • image: We are building MySQL by pulling from its official image that was published in Dockerhub.

  • container_name: It just gives a custom name to our container so we named it prestashop-db. Naming containers is good practice that allows to easily find the required containers in case of internal changes.

  • environment: There are two environment variables we need to set for the MySQL server:

    • MYSQL_DATABASE allows defining the name of the database.

    • MYSQL_ROOT_PASSWORD allows setting a password for the root user.

At this point, there is no need to add a secure password since we're going to use it locally. For production use cases please take look at the documentation of the MySQL image to set proper environment variables.

ports: We're mapping the default port which is 3306 for the MySQL server

networks: While setting up Prestashop, it will ask to connect our database server. So by using networks we allow containers to communicate with each other.

Setting up Prestashop

Next, let's createprestashop service:

prestashop:
    image: prestashop/prestashop:1.7
    container_name: prestashop
    environment:
        DB_SERVER: mysql
    ports:
        - 8080:80
    networks:
        - prestashop

DB_SERVER allows setting our database server for Prestashop. The name of the database service mysql will represent our MySQL server.

You can choose other versions of Prestashop as well but for now, we're using the latest one.

Setting up PhpMyAdmin (optional)

Lastly, we will configure PHPMyAdmin to interact with the database. This is an optional container but I highly recommend to include in the project to handle data easily.

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    environment:
        PMA_HOST: mysql
    ports:
        - 1235:80
    networks:
        - prestashop
    depends_on:
        - mysql

PMA_HOST environment variable allows setting external MySQL server that we're running undermysql service. You'll be able to login into phpmyadmin by providing the same password for the database service. Note that, these passwords are applied to root users so in case you switch to production be aware of security vulnerabilities.

All containers communicate through the same network, so we should add network configuration as well. After all, compose file should look like below:

version: '3.9'

services:
    mysql:
        image: mysql:8
        container_name: prestashop-db
        command: --default-authentication-plugin=mysql_native_password
        environment:
            MYSQL_DATABASE: prestashop
            MYSQL_ROOT_PASSWORD: prestashop
        ports:
            - 3306:3306
        networks:
            - prestashop

    prestashop:
        image: prestashop/prestashop:1.7
        container_name: prestashop
        environment:
            DB_SERVER: mysql
        ports:
            - 8080:80
        networks:
            - prestashop

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    environment:
        PMA_HOST: mysql
    ports:
        - 1235:80
    networks:
        - prestashop
    depends_on:
        - mysql

networks:
    prestashop:

Great! Now it's ready for launch:

docker-compose up -d

Take some coffee it can take a while โ˜•

After successful installation navigate to localhost and you will see Prestashop's installation assistant. I suggest taking a look video explanation of this post below, where you'll be guided through the installation process. There're some key points that you have to know for a successful database connection:

Support ๐ŸŒ

If you feel like you unlocked new skills, please share them with your friends and subscribe to the youtube channel to not miss any valuable information.

ย