Read More
Read More
Menu Close

    Running PrestaShop in docker from scratch (Dockerfile method)

    In our last blog, we talked about Running PrestaShop in Docker Container using official PrestaShop image from docker hub. Here we will explain how you can create your own PrestaShop docker image from scratch.

    There are a number of benefits in custom PrestaShop image, for instance:

    • There is no need for a separate database container.
    • There is no need for a docker network.
    • The PrestaShop installation is done by the image author, so he has access to modify/customise anything.

    We will use Dockerfile to manage almost all steps automatically so let’s start by preparing folder for Dockerfile:

    Step 1 : Prepare folder for docker image

    First of all, create an empty folder “prestashop” and create a Dockerfile inside this folder. Then download PrestaShop from official website and extract all files inside prestashop folder.

    The folder structure should look like this:

    Searching for an experienced
    Prestashop Company ?
    Read More
    image-27

    Step 2 : Prepare Dockerfile

    Edit and put this content in Dockerfile:

    FROM php:7.4-apache
    WORKDIR /var/www/html
    # update packages
    RUN apt-get update
    # install zip and pdo extensions
    RUN apt-get install -y \
            libzip-dev \
            zip \
        && docker-php-ext-configure zip \
        && docker-php-ext-install zip \
        && docker-php-ext-install pdo pdo_mysql
    
    # install gd image library
    RUN apt-get install -y \
    		libfreetype6-dev \
    		libjpeg62-turbo-dev \
    		libpng-dev \
    	&& docker-php-ext-configure gd --with-freetype --with-jpeg \
      && docker-php-ext-install -j "$(nproc)" gd
    
    # install intl extension
    RUN apt-get install -y zlib1g-dev libicu-dev g++ \
    && docker-php-ext-configure intl \
    && docker-php-ext-install intl
    
    # enable mod_rewrite
    RUN a2enmod rewrite
    
    # install opcache for php acceleration
    RUN docker-php-ext-install opcache \
        && docker-php-ext-enable opcache \
        && rm -rf /var/lib/apt/lists/* \
        && apt-get autoremove -y
    
    # install database
    RUN apt-get update
    RUN apt-get install -y mariadb-server
    
    # create user-psuser password-admin and assign privileges
    RUN service mariadb start && mysql -uroot mysql -e "CREATE USER 'psuser'@localhost IDENTIFIED BY 'admin';GRANT ALL PRIVILEGES ON *.* TO 'psuser'@localhost IDENTIFIED BY 'admin';FLUSH PRIVILEGES;"
    
    # copy prestashop folder content
    COPY prestashop/ /var/www/html
    # Change owner and permissions
    # RUN chown -R www-data:www-data /var/www/html/var/logs
    RUN chmod -R 0777 /var/www/html/var/logs \
    /var/www/html/var/cache \
    /var/www/html/config \
    /var/www/html/img \
    /var/www/html/mails \
    /var/www/html/modules \
    /var/www/html/translations \
    /var/www/html/upload \
    /var/www/html/download \
    /var/www/html/app/config \
    /var/www/html/app/Resources/translations \
    /var/www/html/img
    
    RUN touch /usr/local/etc/php/php.ini && echo "short_open_tag=FALSE" >> /usr/local/etc/php/php.ini
    
    EXPOSE 80

    Let’s understand each step in this Dockerfile:

    • FROM php:7.4-apache – Using php:7.4-apache.
    • WORKDIR /var/www/html – Set document root.
    • RUN apt-get update – Update packages list.
    • All commands starting with “RUN apt-get install” install the necessary extensions for PrestaShop.
    • RUN a2enmod rewrite – Enable mod_rewrite.
    • RUN docker-php-ext-install opcache… – install opcache for php acceleration (optional).
    • RUN service mariadb start && … – Start mysql server and create user with password.
    • COPY prestashop/ /var/www/html – Copy PrestaShop files to html directory of image.
    • RUN chmod -R 0777 /var… – Assign read/write permissions to necessary folders in prestashop
    • RUN touch /usr/local/etc/php/php.ini && … – Create php.ini and set short_open_tag=FALSE (Optional)
    • EXPOSE 80 – Docker container port configuration for TCP connections.

    Step 3 : Open terminal and build docker image

    Open terminal in the folder where Dockerfile is located and run this command:

    docker build -t myps:latest .

    The “myps” is custom image name, you can use any name for your docker image. After running this command, the commands from Dockerfile will execute and create docker image. It will take sometime to perform all commands so do not interrupt or leave the terminal.

    Step 4 : PrestaShop installation

    Now that our image is created, we will install the PrestaShop and commit to docker image so that our image is updated with an installed PrestaShop. Hence, run a container on the created image with this command:

    docker run --name mypscontainer -d -p 8910:80 myps:latest && docker exec -ti mypscontainer service mariadb start

    This command runs a container and starts mysql server inside the container:

    • mypscontainer is custom container name, you can use any name here.
    • myps is our image name created in previous steps.
    • 8910 is port number where we want to run our container.

    After container starts, go to http://localhost:8910, the PrestaShop installation page will open. Complete the installation and use these details for database connection:

    • Host : 127.0.0.1
    • Username : psuser
    • Password : admin

    The above user was created while image creation from the Dockerfile.

    After the installation is complete, open terminal and run these two commands to remove “install” folder and rename “admin” folder (this step is required for PrestaShop):

    docker exec -ti mypscontainer rm -rf /var/www/html/install
    docker exec -ti mypscontainer mv /var/www/html/admin /var/www/html/adminps

    The mypscontainer is the container name, so use your container name or id in its place.

    Step 5 : Commit this installed PrestaShop container to image

    Now, our PrestaShop is installed in the container so we will commit this to our image so that whenever we start container, we will get this installed PrestaShop. Run this command in terminal to commit the container:

    docker commit mypscontainer myps:latest

    The mypscontainer is container name, myps is image name and latest is the image tag. You can use your corresponding values here.

    Now always use this command to run container and start database server together:

    docker run --name mypscontainer -d -p 8910:80 myps:latest && docker exec -ti mypscontainer service mariadb start

    That’s all about running PrestaShop in docker from scratch. If any issue or doubt in the above process, please feel free to let us know in the comment section.

    I would be happy to help.

    Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected].

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top