Install Opencart and MariaDB using Docker Containers

Install Opencart and MariaDB using Docker Containers
Install Opencart and MariaDB using Docker Containers

In this article, we'll see how to run Opencart in Docker containers alongside MariaDB and Phpmyadmin.

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

We'll have 3 containers in total:

  1. Opencart
  2. MariaDB
  3. Phpmyadmin

Setting up Database

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

version: '3'
services:
  mariadb:
    image: docker.io/bitnami/mariadb:10.3
    environment:
      - MARIADB_USER=admin
      - MARIADB_PASSWORD=passwd
      - MARIADB_ROOT_USER=root
      - MARIADB_ROOT_PASSWORD=supersecretpassword
      - MARIADB_DATABASE=opencart_db
    ports:
      - '3306:3306'
    volumes:
      - 'mariadb_data:/bitnami/mariadb'
    networks:
        - opencart-net

image: We are building MariaDB by pulling from its official image that was published in Bitnami.

environment: There are a few environment variables we need to set for the MariaDB server:

  • MARIADB_USER creating a DB user in the first run of the container. This user will be used to connect DB from Opencart container.
  • MARIADB_PASSWORD allows setting password for the user.
  • MARIADB_ROOT_USER is required and setting root user for DB. By default, the user is root so it's not required to define it in environment variables.
  • MARIADB_ROOT_PASSWORD is also required and setting a password for the root user.
  • MARIADB_DATABASE is required and allows to define the database name.

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

volumes: Preventing data loss by mounting DB data to internal volume.

networks: By using networks we allow containers to communicate with each other such as connecting DB internally from the Opencart container.

Setting up Opencart

Next, let's create opencart service:

 opencart:
    image: docker.io/bitnami/opencart:3
    ports:
      - '80:8080'
    environment:
      - OPENCART_HOST=localhost
      - OPENCART_DATABASE_HOST=mariadb
      - OPENCART_DATABASE_PORT_NUMBER=3306
      - OPENCART_DATABASE_USER=admin
      - OPENCART_DATABASE_PASSWORD=passwd
      - OPENCART_DATABASE_NAME=opencart_db
    volumes:
      - 'opencart_data:/bitnami/opencart'
      - 'opencart_storage_data:/bitnami/opencart_storage/'
    networks:
        - opencart-net
    depends_on:
      - mariadb

You can choose other versions of Opencart as well but for now, we're using the latest one which Β is v3.

  • OPENCART_HOST - defines OpenCart server hostname/address.
  • OPENCART_DATABASE_HOST - Hostname for MariaDB server. Default: mariadb
  • OPENCART_DATABASE_PORT - DB port of MariaDB
  • OPENCART_DATABASE_USER - should be the same as MariaDB user for successful connection.
  • OPENCART_DATABASE_PASSWORD - should be the same as the MariaDB password for successful connection.
  • OPENCART_DATABASE_NAME - database name for connection which is opencart_db as previously defined in mariadb service.

depends_on: Prevents launching before MariaDB is ready for connection.

Setting up PhpMyAdmin (optional)

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

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    ports:
        - '1235:80'
    environment:
        - PMA_HOST=mariadb
        - PMA_ROOT_PASSWORD=opencart
    links:
        - mariadb:mariadb
    networks:
        - opencart-net
    depends_on:
        - mariadb

PMA_HOST environment variable allows setting external MariaDB server that we're running under mariadb service. You'll be able to login in phpmyadmin by providing the same password for the database service. 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 the below:

version: '3'
services:
  mariadb:
    image: docker.io/bitnami/mariadb:10.3
    environment:
      - MARIADB_USER=admin
      - MARIADB_PASSWORD=passwd
      - MARIADB_ROOT_USER=root
      - MARIADB_ROOT_PASSWORD=supersecretpassword
      - MARIADB_DATABASE=opencart_db
    ports:
      - '3306:3306'
    volumes:
      - 'mariadb_data:/bitnami/mariadb'
    networks:
        - opencart-net

  opencart:
    image: docker.io/bitnami/opencart:3
    ports:
      - '80:8080'
      - '443:8443'
    environment:
      - OPENCART_HOST=localhost
      - OPENCART_DATABASE_HOST=mariadb
      - OPENCART_DATABASE_PORT_NUMBER=3306
      - OPENCART_DATABASE_USER=admin
      - OPENCART_DATABASE_PASSWORD=passwd
      - OPENCART_DATABASE_NAME=opencart_db
    volumes:
      - 'opencart_data:/bitnami/opencart'
      - 'opencart_storage_data:/bitnami/opencart_storage/'
    networks:
        - opencart-net
    depends_on:
      - mariadb

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    ports:
        - '1235:80'
    environment:
        - PMA_HOST=mariadb
        - PMA_ROOT_PASSWORD=opencart
    links:
        - mariadb:mariadb
    networks:
        - opencart-net
    depends_on:
        - mariadb

volumes:
  mariadb_data:
    driver: local
  opencart_data:
    driver: local
  opencart_storage_data:
    driver: local

networks:
    opencart-net:

Now it's ready for launch:

docker-compose up -d

After successful installation navigate to http://localhost and you will see the Opencart storefront.

Support 🌏

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