How to use docker to run wordpress and create a wordpress website

To use Docker to run WordPress, the easiest and most reliable method is using Docker Compose. This allows you to run both WordPress and its required MySQL database together with minimal configuration.


✅ Step-by-Step: Run WordPress with Docker

📦 Prerequisites


🔧 1. Create a docker-compose.yml File

Create a folder (e.g., wordpress-docker) and inside it, create a docker-compose.yml file:

version: '3.8'

services:
  wordpress:
    image: wordpress:latest
    ports:
      - "8000:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - wordpress_data:/var/www/html
    depends_on:
      - db

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
      MYSQL_ROOT_PASSWORD: rootpassword
    volumes:
      - db_data:/var/lib/mysql

volumes:
  wordpress_data:
  db_data:

▶️ 2. Start the Containers

From the directory where your docker-compose.yml is located, run:

docker-compose up -d

This will:

  • Pull the WordPress and MySQL images
  • Start the containers
  • Map port 8000 to WordPress (so you can access it in your browser)

🌐 3. Access WordPress

Visit http://localhost:8000 in your browser. You’ll see the WordPress setup screen.


🛑 4. Stopping and Removing

To stop:

docker-compose down

To stop and remove volumes (data will be lost):

docker-compose down -v

Optional: Customizations

  • Change port from 8000:80 to another if port 8000 is in use.
  • Use .env file to separate environment variables.
  • Add phpMyAdmin for DB management (let me know if you want that too).

Would you like a version with SSL, phpMyAdmin, or persistent data outside of Docker volumes?

No images available.