Wordpress in Docker with nginx Webserver

Wordpress in Docker with nginx Webserver

how to host wordpress in docker container

WordPress is a free and open source blogging tool and a content management system (CMS) based on PHP and MySQL, which runs on a web hosting service. Features include a plugin architecture and a template system. WordPress is used by more than 22.0% of the top 10 million websites as of August 2013. WordPress is the most popular blogging system in use on the Web, at more than 60 million websites

WordPress

To create a site on WordPress using Docker, you will need to follow these steps or skip to docker-compose file in the end .

Better skip to docker-compose as it is simple and straight forward .

  1. Make sure you have Docker installed on your system. If not, you can download it from the official website.
    or download the script and run it as below
    curl -fsSL https://get.docker.com -o get-docker.sh and add permission with chmod +x get-docker.sh and run it with sh get-docker.sh

  2. Next, create a new directory on your system and navigate to it in the command line.

  3. Run the following command to pull the latest version of the WordPress image from Docker Hub:

docker pull wordpress

Once the image is downloaded, run the following command to start a new container using the WordPress image:

docker run --name my-wordpress -p 8080:80 -d wordpress

This will start a new container named "my-wordpress" and map port 8080 on your system to port 80 on the container.

  1. You can now access the WordPress installation by navigating to http://localhost:8080 in your web browser.

  2. Once you logged in to your WordPress site, on the left sidebar go to "Post" and then click on "Add new"

  3. Fill in the post title and content and publish it.

  4. Your post is now live on your WordPress site running in a Docker container.

Note: In order to access the WordPress site outside of your local machine, you will need to map port 8080 to a publicly accessible IP address.

Site will work only if there is a Database

Mysql Database

Next you will need MYSql database

docker run --name my-mysql -e MYSQL_ROOT_PASSWORD=mypassword -d mysql

This will start a new container named "my-mysql" and set the root password for the MySQL server to "mypassword".

Now we need to link the WordPress container with the MySQL container, so run the following command:

docker run --name my-wordpress --link my-mysql:mysql -p 8080:80 -d wordpress

This will link the "my-wordpress" container to the "my-mysql" container and give it access to the MySQL server.

  1. To connect the WordPress site to the MySQL container, you will need to configure the WordPress container's environment variables. You will need to set the WORDPRESS_DB_HOST to mysql, WORDPRESS_DB_USER to root and WORDPRESS_DB_PASSWORD to mypassword

  2. You may also need to configure the wp-config.php file to connect to the MySQL server.

  3. Once you've completed these steps, the WordPress site running in the Docker container should be able to connect to the MySQL server running in another container and you can able to create new post and it will store in the MySQL container.

Nginx Proxy

To add an nginx reverse proxy to your WordPress installation running in a Docker container, you will need to follow these additional steps:

create a new directory on your system and navigate to it in the command line. Create a new file named default.conf and add the following configuration:

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://wordpress:80;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

This configuration tells nginx to listen on port 80 and forward requests to the WordPress container running on port 80. Replace example.com with your website domain

Run the following command to start a new container using the nginx image

docker run --name my-nginx -p 80:80 --link my-wordpress:wordpress -v /path/to/default.conf:/etc/nginx/conf.d/default.conf:ro -d nginx

This will start a new container named "my-nginx" and map port 80 on the host to port 80 on the container, link the nginx container with the wordpress container and mount the default.conf file as the nginx configuration file.

  1. Now all the request sent to the example.com will be handled by the nginx reverse proxy and then forwarded to the wordpress container.

  2. You can also configure nginx to handle SSL encryption by adding a SSL certificate and key to the configuration file, and configuring the listen and server_name options accordingly.

With Docker-Compose

Below is the docker-compose file to run everything in single command

copy the content and save it in a file docker-compose.yml

version: '3.1'

services:


  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress:/var/www/html

##database 
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql


  ## nginx webserver
  webserver:
    image: nginx:stable
    restart: always 
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./templates:/etc/nginx/templates
      - ./nginx-ssl:/etc/nginx/ssl/
    environment:
      - NGINX_HOST=localhost

volumes:
  wordpress:
  db:
  nginx-ssl:

To store SSL certs, I created a nginx-ssl directory. Look at that, if you want.

The following is my configuration for nginx, (create templates directory and save a file as default.conf.template) which I changed to accommodate server SSL requests.

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://wordpress:80;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

server {
    listen 443 ssl;
    server_name example.com;
    location / {
        proxy_pass http://wordpress:80;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

         ssl_certificate    /etc/nginx/ssl/yourssl.pem;
        ssl_certificate_key    /etc/nginx/ssl/yoursecret.key;

}

Once you are ready , just run docker-compose up -d

That's all . Open <ipaddress:80> or <ipaddress:443>you will see your wordpress site .

To convert existing site to docker

Use the same docker-compose.yml file and change the volume directory and also you may have to restore database and database values in wp-config.php