Wordpress and Woocommerce on Docker

Home

Table of Contents

Installing WordPress and WooCommerce in Docker containers can be a straightforward process if you use Docker Compose. Here's a step-by-step guide on how to do this:

1 Step 1: Install Docker and Docker Compose

Make sure Docker and Docker Compose are installed on your system. You can install Docker by following the official instructions for your platform:

  • Docker Installation
  • Docker Compose Installation

2 Step 2: Create a Project Directory

Create a new directory for your WordPress and WooCommerce project. This directory will hold your docker-compose.yml file and any other necessary configurations.

mkdir wordpresswoocommerce
cd wordpresswoocommerce

3 Step 3: Create a docker-compose.yml File

Create a docker-compose.yml file in the project directory. This file will define the services for WordPress, the MySQL database, and phpMyAdmin (optional).

Here is an example docker-compose.yml file:

version: '3.8'

services:
  wordpress:
    image: wordpress:latest
    container_name: wordpress
    ports:
      - "8000:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress_user
      WORDPRESS_DB_PASSWORD: wordpress_password
      WORDPRESS_DB_NAME: wordpress_db
    volumes:
      - ./wordpress:/var/www/html
    depends_on:
      - db

  db:
    image: mysql:5.7
    container_name: wordpress_db
    environment:
      MYSQL_DATABASE: wordpress_db
      MYSQL_USER: wordpress_user
      MYSQL_PASSWORD: wordpress_password
      MYSQL_ROOT_PASSWORD: root_password
    volumes:
      - ./db_data:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: root_password
    ports:
      - "8080:80"
    depends_on:
      - db

Explanation: WordPress Container: The wordpress service pulls the latest WordPress image and sets up the application. It listens on port 8000 and connects to the db container for MySQL. MySQL Container: The db service uses MySQL version 5.7 and sets up the database with the provided credentials. phpMyAdmin (Optional): This service allows you to manage your MySQL database via a web UI and is accessible on port 8080.

4 Step 4: Run Docker Compose

After saving the docker-compose.yml file, start the containers by running:

docker-compose up -d

This will pull the necessary images, create the containers, and start the services.

5 Step 5: Install WordPress and WooCommerce

Open your browser and navigate to http://localhost:8000. You should see the WordPress installation screen. Complete the WordPress setup by following the instructions (set the site title, admin username, password, etc.). Once WordPress is installed, log in to the dashboard. Navigate to Plugins → Add New and search for WooCommerce. Install and activate WooCommerce.

6 Step 6: Access phpMyAdmin (Optional)

You can access phpMyAdmin at http://localhost:8080 using the root credentials provided in the docker-compose.yml file. This will allow you to manage the database directly if needed.

7 Step 7: Storing Data Persistently

The volumes defined in the docker-compose.yml ensure that your WordPress files and MySQL data persist even if the containers are stopped or removed:

WordPress files are stored in the ./wordpress directory. MySQL data is stored in the ./dbdata directory.

8 Step 8: Stop the Containers

To stop the containers, use:

docker-compose down

This will stop and remove the containers but keep the volumes intact for persistent data.

9 Optional Customizations

SSL: If you want to secure your WordPress site, you can use a reverse proxy like Nginx with Let's Encrypt. Scaling: You can scale your WordPress installation for production by adding caching layers (like Redis or Varnish) or configuring an external database. By using Docker, you create a flexible, reproducible environment for WordPress and WooCommerce without manually managing server configurations.

10 worklog

docker pull wordpress

$ docker run –name some-wordpress –network some-network -d wordpress

11 https://hub.docker.com/_/wordpress docker-compose.yml

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress:/var/www/html

  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql

volumes:
  wordpress:
  db:

Author: Sebastian Emilio Narvaez

Created: 2024-09-08 dom 08:38

Validate