Back

WordPress & SQL in Docker

How to create a Docker container for WordPress with SQL

6

1. πŸ‘¨β€πŸ« Introduction

In this tutorial, we will create a Docker container for WordPress with SQL.
To do this, we need to have Docker and Docker Compose installed.

2. πŸ“¦ Docker and Docker Compose

To install Docker and Docker Compose on Windows and Mac, use their official installer:

To ensure that Docker and Docker Compose are installed, run the following command in your terminal:

docker --version
docker-compose --version

3. πŸ“ Create the folder where WordPress will be located

  1. Create a folder called wordpress in your project’s root folder.

  2. Inside the wordpress folder, create a file called docker-compose.yml with the following content:

version: '3'

services:
  database:
    image: mysql # last mysql version. To specify ones: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: wppassword
      MYSQL_DATABASE: wpdb
      MYSQL_USER: wpuser
      MYSQL_PASSWORD: wppassword
    volumes:
      - mysql:/var/lib/mysql

  wordpress:
    depends_on:
      - database
    image: wordpress:latest
    restart: always
    ports:
      - '80:80'
    environment:
      WORDPRESS_DB_HOST: database:3306
      WORDPRESS_DB_USER: wpuser
      WORDPRESS_DB_PASSWORD: wppassword
      WORDPRESS_DB_NAME: wpdb
    volumes:
      - ./html:/var/www/html

volumes:
  mysql: {}

4. 🏁 Launch / Stop / Delete the container

docker-compose up -d

The -d option is for running the container in the background.

docker-compose stop
docker-compose down