Install wordpress localy with docker easily

As a wordpress developer, setting up separate wordpress environment each time I get a new project development is not an easy task. It will time consuming and once setup, it’s resource consumption is also too high.

This can be easily overcome by using docker and docker composer to setup wordpress development environment. Docker allows you to package wordpress and all the dependencies into containers, making it easier to manage and replicating the same environment in colleague’s machines.

Let me walk you through each step one by one.

  1. Create a directory for the new project.
    mkdir wpdocker
  2. Navigate to the newly created project directory.
    cd wpdocker
  3. Create a docker compose file. Name has to be docker-compose.yml.
    Content of the file is at the end of this article.
  4. Run command to start the docker.
    docker-compose up
    Using this command, the terminal session has to continue or else, docker will be stopped. To overcome that, use below command
    docker-compose up -d
    This will dittach the terminal session and docker will run in background
  5. Volumes to sync files with local directory.
    volumes:
    – ./:/var/www/html

    This ensures to link current directory of the local machine with /var/www/html in the docker file system. When wordpress files are downloaded to the above directory, those content can be seen in the local machine as well.

docker-compose.yml

version: "3.1"
services:
  database:
    mem_limit: 2048m
    image: mariadb:10.6.4-focal
    restart: unless-stopped
    ports:
      - 3306:3306
    env_file: .env
    environment:
      MYSQL_ROOT_PASSWORD: '${MYSQL_ROOT_PASSWORD}'
      MYSQL_DATABASE: '${MYSQL_DATABASE}'
      MYSQL_USER: '${MYSQL_USER}'
      MYSQL_PASSWORD: '${MYSQL_PASSWORD}'
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - wordpress-network

  phpmyadmin:
    depends_on:
      - database
    image: phpmyadmin/phpmyadmin
    restart: unless-stopped
    ports:
      - 8081:80
    env_file: .env
    environment:
      PMA_HOST: database
      MYSQL_ROOT_PASSWORD: '${MYSQL_ROOT_PASSWORD}'
    networks:
      - wordpress-network

  wordpress:
    depends_on:
      - database
    image: wordpress:6.2.2-apache
    restart: unless-stopped
    ports:
      - 8080:80
    env_file: .env
    environment:
      WORDPRESS_DB_HOST: database:3306
      WORDPRESS_DB_NAME: '${MYSQL_DATABASE}'
      WORDPRESS_DB_USER: '${MYSQL_USER}'
      WORDPRESS_DB_PASSWORD: '${MYSQL_PASSWORD}'
    volumes:
      - ./:/var/www/html
    networks:
      - wordpress-network

volumes:
  db-data:

networks:
  wordpress-network:
    driver: bridge