Docker set up for Opencart for local development

Setting up a local PHP development environment with Docker for OpenCart involves creating a Docker Compose configuration file to define the services needed for running OpenCart, such as PHP, MySQL, Redis, Apache, etc. Below is a step-by-step guide to help you set up your development environment:

Install Docker and Docker Compose

Make sure you have Docker and Docker Compose installed on your system. You can download and install them from the official Docker website: https://www.docker.com/get-started

Clone the Opencart Github

Install Git: Before you can clone the OpenCart repository, you need to have Git installed on your system. Git is a version control system that allows you to track changes to files and collaborate with others on software development projects. You can download and install Git from the official website: https://git-scm.com/.

Clone the OpenCart Repository Once Git is installed, open a terminal or command prompt and navigate to the directory where you want to clone the OpenCart repository. Then, run the following command:

git clone https://github.com/opencart/opencart.git

This command will clone the entire OpenCart repository from GitHub to your local machine.

Set Up a Local Development Environment

To set up a local development environment for OpenCart, you’ll need a web server (e.g., Apache or Nginx), PHP, and MySQL. You can either install these components manually or use a pre-configured solution like Docker. If you’re using Docker, you can create a docker-compose.yml. This file defines the services needed for running OpenCart, including PHP, MySQL, and Apache.

When you clone from the Opencart Github, everything is already set up for you. You will see docker-compose.yml, Dockerfile, and tools folder

Opencart Docker

Now, run docker-compose up is the command used to start the Docker containers defined in your docker-compose.yml file. This command starts with the services specified in the file, which typically include web servers, databases, and any other necessary components for your application.

docker-compose up 

The first time will some time to pull all the Docker images like Postgres, Redis, Memcached, MySQL, Opencart, admirer etc.

Docker compose up

Once all docker images are pulled, in the end, you will see the Store link and Admin link below:

Opencart URL Store

You can visit http://localhost and you will see the Opencart Store. For admin login go to http://localhost/admin and use admin as username and admin as password.

If you check the Docker Desktop, then you will see the following container:

Opencart Docker Container

You will see the following Docker Images pulled:

Opencart Docker Images

Now you can work in the upload folder code and see changes as you develop in localhost URL.

Access the Opencart database in Docker

To access the Opencart database visit http://localhost:8080 and enter root as username and password as opencart.

Opencart docker database PHPmyadmin

Once you log in you will see the interface similar to PHPmyadmin, select the opencart database and you can see all the database tables

Docker PHPmyadmin

Dockerfile for Opencart

FROM php:8.2.11-apache

ARG DOWNLOAD_URL
ARG FOLDER


ENV DIR_OPENCART='/var/www/html/'
ENV DIR_STORAGE='/storage/'
ENV DIR_CACHE=${DIR_STORAGE}'cache/'
ENV DIR_DOWNLOAD=${DIR_STORAGE}'download/'
ENV DIR_LOGS=${DIR_STORAGE}'logs/'
ENV DIR_SESSION=${DIR_STORAGE}'session/'
ENV DIR_UPLOAD=${DIR_STORAGE}'upload/'
ENV DIR_IMAGE=${DIR_OPENCART}'image/'


RUN apt-get clean && apt-get update && apt-get install unzip

RUN apt-get install -y \
  libfreetype6-dev \
  libjpeg62-turbo-dev \
  libpng-dev \
  libzip-dev \
  && docker-php-ext-configure gd --with-freetype --with-jpeg\
  && docker-php-ext-install -j$(nproc) gd \
  && docker-php-ext-install zip && && docker-php-ext-enable zip\
  && docker-php-ext-enable mysqli

RUN apt-get install -y vim

RUN mkdir /storage && mkdir /opencart

RUN if [ -z "$DOWNLOAD_URL" ]; then \
  curl -Lo /tmp/opencart.zip $(sh -c 'curl -s https://api.github.com/repos/opencart/opencart/releases/latest | grep "browser_download_url" | cut -d : -f 2,3 | tr -d \"'); \
  else \
  curl -Lo /tmp/opencart.zip ${DOWNLOAD_URL}; \
  fi

RUN unzip /tmp/opencart.zip -d  /tmp/opencart;

RUN mv /tmp/opencart/$(if [ -n "$FOLDER" ]; then echo $FOLDER; else  unzip -l /tmp/opencart.zip | awk '{print $4}' | grep -E 'opencart-[a-z0-9.]+/upload/$'; fi)* ${DIR_OPENCART};

RUN rm -rf /tmp/opencart.zip && rm -rf /tmp/opencart && rm -rf ${DIR_OPENCART}install;

RUN mv ${DIR_OPENCART}system/storage/* /storage
COPY configs ${DIR_OPENCART}
COPY php.ini ${PHP_INI_DIR}

RUN a2enmod rewrite

RUN chown -R www-data:www-data ${DIR_STORAGE}
RUN chmod -R 555 ${DIR_OPENCART}
RUN chmod -R 666 ${DIR_STORAGE}
RUN chmod 555 ${DIR_STORAGE}
RUN chmod -R 555 ${DIR_STORAGE}vendor
RUN chmod 755 ${DIR_LOGS}
RUN chmod -R 644 ${DIR_LOGS}*

RUN chown -R www-data:www-data ${DIR_IMAGE}
RUN chmod -R 744 ${DIR_IMAGE}
RUN chmod -R 755 ${DIR_CACHE}

RUN chmod -R 666 ${DIR_DOWNLOAD}
RUN chmod -R 666 ${DIR_SESSION}
RUN chmod -R 666 ${DIR_UPLOAD}

CMD ["apache2-foreground"]

Docker Compose Configuration File for Opencart

version: '3'
services:
  opencart:
    build: tools
    user: 1000:1000
    ports:
      - "80:80"
    volumes:
      - ./upload:/var/www/html
    depends_on:
      - mysql
    command: >
      bash -c "if [ ! -f /var/www/html/install.lock ]; then
                 wait-for-it mysql:3306 -t 60 &&
                 cp config-dist.php config.php
                 cp admin/config-dist.php admin/config.php
                 php /var/www/html/install/cli_install.php install --username admin --password admin --email email@example.com --http_server http://localhost/ --db_driver mysqli --db_hostname mysql --db_username root --db_password opencart --db_database opencart --db_port 3306 --db_prefix oc_;
                 touch /var/www/html/install.lock;
               fi &&
               apache2-foreground"

  mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=opencart
      - MYSQL_DATABASE=opencart

  adminer:
    image: adminer:latest
    environment:
      ADMINER_DEFAULT_SERVER: mysql
    depends_on:
      - mysql
    ports:
      - "8080:8080"

  redis:
    image: redis:latest

  memcached:
    image: memcached:latest

  postgres:
    image: postgres:latest
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=opencart
      - POSTGRES_DB=opencart

Error: Cannot connect to the Docker daemon at unix:///Users//.docker/run/docker.sock. Is the docker daemon running?

You just need to run the Docker as questioned.

That’s it! You now have a local PHP development environment with Docker for OpenCart. You can develop and test your OpenCart extensions or customizations locally before deploying them to a production environment.

Previous articleGoogle base feed module for Opencart 4
Next articleHow to develop an Opencart 4 custom theme? OpenCart 4 theme development tutorial

LEAVE A REPLY

Please enter your comment!
Please enter your name here