How to Install WordPress in Docker

How to Install WordPress in Docker

Using the official WordPress Docker image is a straightforward process. Here's a step-by-step guide on how to use it:

  1. Install Docker on your machine if you haven't already. You can download Docker from the official Docker website.

  2. Open a terminal window and create a new directory for your WordPress installation. For example, you can use the following command:

     bashCopy codemkdir ~/wordpress-docker
    
  3. Navigate to the directory that you just created using the following command:

     bashCopy codecd ~/wordpress-docker
    
  4. Create a new file called docker-compose.yml using the following command:

     bashCopy codetouch docker-compose.yml
    
  5. Open the docker-compose.yml file in a text editor and paste the following contents:

     yamlCopy codeversion: '3.7'
    
     services:
       db:
         image: mysql:5.7
         volumes:
           - db_data:/var/lib/mysql
         restart: always
         environment:
           MYSQL_ROOT_PASSWORD: somewordpress
           MYSQL_DATABASE: wordpress
           MYSQL_USER: wordpress
           MYSQL_PASSWORD: wordpress
    
       wordpress:
         depends_on:
           - db
         image: wordpress:latest
         volumes:
           - wordpress_data:/var/www/html
         ports:
           - "8000:80"
         restart: always
         environment:
           WORDPRESS_DB_HOST: db:3306
           WORDPRESS_DB_USER: wordpress
           WORDPRESS_DB_PASSWORD: wordpress
           WORDPRESS_DB_NAME: wordpress
    
     volumes:
       db_data:
       wordpress_data:
    

    This YAML file defines two services, one for the MySQL database and another for WordPress.

  6. Save and close the docker-compose.yml file.

  7. In the terminal, navigate to the ~/wordpress-docker directory and run the following command to start the Docker containers:

     Copy codedocker-compose up -d
    

    This command will start the containers in the background (-d flag).

  8. After a few seconds, the WordPress site should be up and running. Open a web browser and go to http://localhost:8000 to access your new WordPress site.

That's it! You have now successfully set up WordPress using the official Docker image.