Skip to content

Instantly share code, notes, and snippets.

@sourabhdeshmukh
Last active September 15, 2020 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sourabhdeshmukh/04f2ddc8276420eacb459a9c64602707 to your computer and use it in GitHub Desktop.
Save sourabhdeshmukh/04f2ddc8276420eacb459a9c64602707 to your computer and use it in GitHub Desktop.
Deploying Wordpress application using docker containers

Deploy Wordpress App Using Containers

Prerequisite:

  • Install docker on you host machine. You can follow this link.

Summary:

  • First we need to create an environment for working of your application for that we need web server and the application dependecies to fullfill the requirements. So in this case we are deployin wordpress application and I am using an nginx server and php dependencies.

Details:

  • In Docker file I will be using alpine as a base image. So why alpine image? You can read it here.

  • As we discussed in a summary we will be installing dependencies using apk package manager which is default in alpine image.

    apk add nginx
    apk add php-fpm 
    apk add php-json 
    apk add php-mysqli 
    
  • We will be exposing the application port to access our application.

    EXPOSE 80
    
  • To make nginx server to work properly we need to create /run/nginx. In this directory our PID file will get stored. We required to create this directory because by default this directory will not be present so we need to create this directory.

    mkdir /run/nginx
    
  • In the next step we are going to update the default configuration file of an nginx server to allow php. So after modification our default.conf file will look like.

    #$ cat /etc/nginx/conf.d/default.conf
    
    server {
      listen 80 default_server;
      listen [::]:80 default_server;
    
      root   /var/www/localhost/htdocs;
      index  index.php index.html index.htm;
      location / {
        try_files $uri $uri/ /index.php?$query_string;
      }
    
      location = /404.html {
        internal;
      }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass   0.0.0.0:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
    }
    
  • In next step we will clone our application.

    git clone https://github.com/WordPress/WordPress
    cd Wordpress
    mkdir config
    
  • We will copy the above default.conf nginx configuration file into config folder created in above step.

  • We need to define working directory as

    WORKDIR /var/www/localhost/htdocs
    

Dockerfile So our dockerfile will look like below.

FROM alpine
EXPOSE 80
RUN apk update && \
  apk add nginx && \
  apk add php-fpm && \
  apk add php-json && \
  apk add php-mysqli && \
  mkdir /run/nginx;
ADD config/default.conf /etc/nginx/conf.d/default.conf
CMD ["/bin/sh", "-c", "/usr/sbin/php-fpm7; exec nginx -g 'daemon off;';"]
WORKDIR /var/www/localhost/htdocs
  • Switch to clonned directory and create docker file inside it. Because we are copying some file the dependecy files from there. And we are going to build docker image.

    docker build -t wpapp .
    
  • We will run the container from same directory using below command. Here we have mounted local directory to container as a volume, else you can copy the contents from directory to a docker container with the help of Dockerfile.

    docker run -it -d -v $PWD:/var/www/localhost/htdocs -p 80:80 --name wpappbox wpapp
    

Note: This will only host our application so to drive our application properly we need to attach it with the datastore. So it this can we will be using mysql docker container.

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=<Password> -e MYSQL_DATABASE=wpappdb --name wpappdb mysql --default-authentication-plugin=mysql_native_password
  • We need to point this container to our application. Go to application directory and create wp-config.php. You can copy the sample wp-config-sample.php to wp-config.php and modify the following values.
    • DB Name
    • DB User
    • DB Pass
    • DB Host (In this case our mysql docker container IP).
  • Now restart the docker container using docker restart .
  • Go to the browser window and access your application using you host ip.

Congratulations...

You have successfully deployed your wordpress application using docker containers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment