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:
-
On Windows:
Visit the official page to download the Docker executable for Windows.
-
On Mac:
Visit the official page to download the Docker executable for Mac.
-
On Linux, you need to install it in 2 steps:
- Install Docker
sudo apt install docker.io
- Install Docker Compose
sudo apt install docker-compose
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
-
Create a folder called
wordpress
in your projectβs root folder. -
Inside the
wordpress
folder, create a file calleddocker-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
- To LAUNCH the container, run the following command in your terminal:
docker-compose up -d
The -d
option is for running the container in the background.
- To STOP the container, run the following command in your terminal:
docker-compose stop
- To DELETE the container, run the following command in your terminal:
docker-compose down