Host your Wordpress with docker container
Tags: docker, wordpress, linux
Published: 2022-10-15 09:20:17

Develop and running your Wordpress site with Docker container given us a large opportunity for developing and delivery our change to Wordpress site quick and efficiently. This article will guide you through simple processes to easily run your Wordpress within Docker container.

## Prerequisite
-	Installed Docker container.
-	Installed Docker Composer.
-	Linux base operation system.
-	Basic understand of Wordpress system and Docker.
Let’s get started!

## Project structure
First open your developing directory on your Wordpress project. This could be your wp-content folder. Your directory structure will some thing look like this:

![Project strucutre](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovwe6fskgjr5n8an011h.png)
Next, create a `docker-compose.yml` file on your project folder with the following content:
```yaml
version: '3'
services:
  db:
    image: mysql:8
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:php8.0-apache
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    working_dir: /var/www/html
    volumes:
      - ./themes:/var/www/html/wp-content/themes
      - uploads:/var/www/html/wp-content/uploads
volumes:
  db_data: {}
  uploads: {}
```
The above file content will create 2 containers within docker composer. One is the SQL database for your Wordpress container and one is your Wordpress container. We will run the Wordpress with port 8000 and mount 2 volumes: one for our database and one for our uploads folder so that the data persistence even when we restart or delete our container.

> One note here is that `./themes:/var/www/html/wp-content/themes` row tell that we will copy all of our developing themes to our Wordpress container so that every change to the theme we work will be delivered to the container.

## Build and run your project
After setting up all thing, run sudo docker-compose up -d on your project folder. This command will execute your docker-compose.yml file in detached mode, pulling all needed images and run it. Continue to run command sudo docker ps to check if your database and wordpress images are up and running, the result will something like this:
```
CONTAINER ID   IMAGE                     COMMAND                  CREATED        STATUS        PORTS                                   NAMES
10ace87d1ccd   wordpress:php8.0-apache   "docker-entrypoint.s…"   22 hours ago   Up 22 hours   0.0.0.0:8000->80/tcp, :::8000->80/tcp   june_dang_blog-wordpress-1
45dcbc0d302d   mysql:8                   "docker-entrypoint.s…"   22 hours ago   Up 22 hours   3306/tcp, 33060/tcp                     june_dang_blog-db-1
```

## Finished Wordpress set up
Open your browser with ip address `http://localhost:8000` and you can see welcome page of Wordpress. If you first start the Wordpress app, it will prompt you to select language and create an admin account. 

![Successfully access Wordpress page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q443nh7283q3h0uz2z6w.png)
Following Wordpress started page and you all have done. Now you can develop and delivery your Wordpress application with Docker container.