Web Development & WordPress

Install and use WordPress using Docker

Step 1:

Install Docker on your Laptop. You can test docker is installed or not by running this following command in your command line and it will show you the Docker version

docker -v 

Step 2: Inside your Applications folder create a folder named “dockerwordpress”

mkdir dockerwordpress

Inside dockerwordpress folder create a file named .env. Inside this .env paste this script

CONTAINER_NAME=myapp
DATABASE_NAME=wordpress
DATABASE_USER=user
DATABASE_PASSWORD=password
DATABASE_ROOT_PASSWORD=root_password

Similarly Inside the same dockerwordpress folder create a file named docker-compose.yml. Inside this docker-compose.yml paste this script

services:
  nginx:
      container_name: ${CONTAINER_NAME}-nginx
      image: nginx:1.15.12-alpine
      restart: unless-stopped
      env_file: .env
      ports:
          - "8080:80"
      volumes:
          - ./nginx:/etc/nginx/conf.d:rw 
          - wordpress:/var/www/html 
      networks:
          - internal
  database:
      container_name: ${CONTAINER_NAME}-database
      image: mysql:8.0
      restart: unless-stopped
      env_file: .env
      environment:
          MYSQL_DATABASE: ${DATABASE_NAME}
          MYSQL_PASSWORD: ${DATABASE_PASSWORD}
          MYSQL_ROOT_PASSWORD: ${DATABASE_ROOT_PASSWORD}
          MYSQL_USER: ${DATABASE_USER}
      ports:
          - "3307:3306"  
      volumes:
          - dbdata:/var/lib/mysql
      networks:
          - internal  

  wordpress:
      depends_on:
          - database
      container_name: ${CONTAINER_NAME}-wordpress 
      image: wordpress:6.5.2-fpm-alpine
      restart: unless-stopped
      env_file: .env
      environment:
          WORDPRESS_DB_HOST: database:3306
          WORDPRESS_DB_NAME: '${DATABASE_NAME}'
          WORDPRESS_DB_USER: '${DATABASE_USER}'
          WORDPRESS_DB_PASSWORD: '${DATABASE_PASSWORD}'
      volumes:
          - wordpress:/var/www/html
      networks:
          - internal



  phpmyadmin:
      container_name: ${CONTAINER_NAME}-phpmyadmin   
      image: phpmyadmin/phpmyadmin
      env_file: .env
      environment:
          PMA_HOST: database
          PMA_PORT: 3306
          MYSQL_ROOT_PASSWORD: "${DATABASE_ROOT_PASSWORD}"
      ports:
          - "8081:80"
      networks:
          - internal
volumes:
  dbdata:
  wordpress:
            
networks:
  internal:
      driver: bridge
          

Create a folder named nginx in the same dockerwordpress folder, and inside that nginx folder create a file named default.conf and paste this script

server {
    listen 80 default_server;
    
    root /var/www/html;

    location /{
        index index.php index.html;
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
  }
}

At the end, inside the terminal (screenshot attached), run this script

docker compose up --build

Leave a comment