Contents

Dockerizing And Deploying Multiple WordPress Applications On Ubuntu

01 Jan 2018 0 212  0

Introduction

WordPress is an extremely useful and popular web application which is both deployed and used by many.

Through years of constant development, people are now able to create a nearly endless amount of different websites or web-applications based on WordPress and the plug-ins / extensions it provides.

In this tutorial, we are going to be using the Docker Linux Container Engine to teach you how to dockerise WordPress applications on Ubuntu cloud servers as well as showing you which way is the most simple and secure when deploying several WordPress sites on one host.

Docker In Brief

Docker provides you with some high level tools which are built on top of Linux Kernel and features the goal of helping developers as well as system administrators port applications, using all of their dependencies conjointly and allowing them to run across systems and machines headache free.

Docker allows this by making safe, LXC (Linux Containers) based environments for applications called ‘containers’ which are made from images. Those bases for containers could be either built by running commands manually and logging in like with a virtual-machine, or by simply automating the procedure using ‘Dockerfiles’.

WordPress In Brief

WordPress was first made as an easy way to install and use  aself-publication platform such as a blogging engine. It has became very popular which has led to the development of a lot of 3rd party plugins, which turned the tool into a full CMS or Content Management System. Based on WordPress, many types of websites and web applications can be made with simplicity and deployed easily.

WordPress is an open-source platform which was developed by using the PHP programming language, this in turn helped it on its way to success. Right now, PHP is one of the most common web-site and web-application creation languages, and it is the language used by a lot of companies including Facebook.

WordPress sites depend on MySQL relational databases in order to keep their data, there are several ways you could use this to power a WordPress site given the multiple options that exist to run PHP and MySQL together.

In our tutorial, we are going to go with a tried-and-tested method to make WordPress installed Docker images, that should enable you to run yet another WordPress site on any VPS with a single command using Docker.

Installing Docker on Ubuntu (latest)

Firstly, update your VPS with the following commands.

sudo apt-get    update
sudo apt-get -y upgrade

Ensure that ‘aufs’ support is available.

sudo apt-get install linux-image-extra-`uname -r`

Then add the ‘Docker repository’ key to ‘apt-key’ for package verification.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9

Next, append ‘Docker’s repository’ to sources.

sudo sh -c "echo deb http://get.docker.io/ubuntu docker main\

> /etc/apt/sources.list.d/docker.list"

Now, update the repository.

sudo apt-get update

Finally, download and install Docker.

sudo apt-get install lxc-docker git

By default, Ubuntu’s firewall, UFW: Uncomplicated Firewall, rejects all forwarding traffic automatically, and this is required by docker.

Enable the forwarding with UFW; edit the UFW config by using nano text editor.

sudo nano /etc/default/ufw

Scroll down and look for the line starting with ‘DEFAULTFORWARDPOLICY’.

Change:

DEFAULT_FORWARD_POLICY=”DROP”

To:

DEFAULT_FORWARD_POLICY=”ACCEPT”

Then press CTRL+X and approve by using Y to save and exit.

Now reload the UFW using the following.

sudo ufw reload

Allowing remote connections

If you are planning to use docker daemon remotely, then you’ll have to allow the default Docker port 4243.

sudo ufw allow 4243/tcp

Working with Docker

Before starting to use docker, let’s go over the commands available.

Command Line Interface Usage and the Daemon

Once the installation is done, the docker daemon will be running in the background and ready to accept commands that were sent by the docker client. For some situations, where it may be needed to manually run Docker, use the following command.

Running the docker daemon.

sudo docker -d &

Client Commands

You can retrieve a full list of the commands available by calling the client.

Docker

Below is a list of the commands available as of version 0.8.0.

Commands:

    attach    Attach to a running container

    build     Build a container from a Dockerfile

    commit    Create a new image from a container's changes

    cp        Copy files/folders from the containers filesystem to the host path

    diff      Inspect changes on a container's filesystem

    events    Get real time events from the server

    export    Stream the contents of a container as a tar archive

    history   Show the history of an image

    images    List images

    import    Create a new filesystem image from the contents of a tarball

    info      Display system-wide information

    insert    Insert a file in an image

    inspect   Return low-level information on a container

    kill      Kill a running container

    load      Load an image from a tar archive

    login     Register or Login to the docker registry server

    logs      Fetch the logs of a container

    port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT

    ps        List containers

    pull      Pull an image or a repository from the docker registry server

    push      Push an image or a repository to the docker registry server

    restart   Restart a running container

    rm        Remove one or more containers

    rmi       Remove one or more images

    run       Run a command in a new container

    save      Save an image to a tar archive

    search    Search for an image in the docker index

    start     Start a stopped container

    stop      Stop a running container

    tag       Tag an image into a repository

    top       Lookup the running processes of a container

    version   Show the docker version information

    wait      Block until a container stops, then print its exit code

Working with Dockerfiles

What exactly are Dockerfiles?

Dockerfiles are scripts that have commands declared successively to be executed in a certain order given by Docker to automatically make a new image.

Those files have to start with the definition of a base image using the ‘FORM’ instruction. Then, the build procedure should begin with every action forming the final image with commits.

Dockerfiles may be used with the build command.

# Build an image using the Dockerfile at current location

# Tag the final image with [name] (e.g. *wordpress_img*)

# Example: sudo docker build -t [name] .

sudo docker build -t wordpress_img .

Dockerfile Commands Overview

Dockerfiles work by retrieving the instructions below:

  • ADD: Copy a file from the host into the container
  • CMD: Set default commands to be executed, or passed to the ENTRYPOINT
  • ENTRYPOINT: Set the default entrypoint application inside the container
  • ENV: Set environment variable (e.g. key = value)
  • EXPOSE: Expose a port to outside
  • FROM: Set the base image to use
  • MAINTAINER: Set the author / owner data of the Dockerfile
  • RUN: Run a command and commit the ending result (container) image
  • USER: Set the user to run the containers from the image
  • VOLUME: Mount a directory from the host to the container
  • WORKDIR: Set the directory for the directives of CMD to be executed

Creating WordPress Containers

Pulling The Image

Pulling The Image

In this tutorial, we are going to be using an out-of-the-box WordPress image named ‘tutum/wordpress’. That wordpress image was made using  Tutum’s WordPress Image: To be able to make containers with this image; we will first have to pull/download it first.

Let’s pull the image.

docker pull tutum/wordpress

That command should download the underlying base images including every modified layer.

After the image is ready, you can issue a single command to create dockerised WordPress instances.

Creating A Publicly Accessible WordPress Container

Execute the next command to make a container which is reachable from the outside on a port you specify. For example: 80.

# Usage: docker run -p [Port Number]:80 tutum/wordpress

# Example:

docker run -p 80:80 tutum/wordpress

This command will make a WordPress instance which accepts connections from the outside on the default HTTP port 80.

Creating A Locally Accessible WordPress Container

Often you may wish to have containers reachable only locally. This can be very useful when deciding to set up a load-balancer, or another reverse-proxy, to provide connections across many WordPress instances.

Execute the following command to make a locally accessible container. # Allocate a port dynamically.

# Usage: docker run -p 127.0.0.1::80 tutum/wordpress

# Example:

docker run -p 127.0.0.1::80 tutum/wordpress

After you have used the command above, Docker should create a container before giving you its ID and dynamically allocating a port. You may figure out what port the container is using by entering the ‘port’ command.

# Usage: docker port [container ID] [private port number]

# Example:

docker port 9af15d73fdf8a997 80



# 127.0.0.1:49156

In this situation, the output classifies that the container is accessible only from the localhost on port 49156. You can use the address, given in full, to redirect connections from a reverse-proxy.

If you would like to specify a port, put it in-between the IP address and the private port used by the web server inside (e.g 80).

# Usage: docker run -p 127.0.0.1:[local port]:80 tutum/wordpress

# Example:

docker run -p 127.0.0.1:8081:80 tutum/wordpress

Now, you should have a WordPress instance which is locally accessible at port 8081.

To be able to run the container in the background, you will have to add the ‘–d’ flag and after, the run command shown below.

docker run –d ..

Otherwise, you will be connected to the container where you should see the outpout from all of the applications currently running.

If you would like to leave the container, use the escape sequence ‘CTRL+P’ instantly, followed by ‘CTRL+Q’.

With the docker ps command, you can get the list of running containers to locate your newly instituted one’s ID.

With the ‘–name’ [name] argument, you may tag a container using a name which is going to free you from dealing with complex container IDs:

docker run -d -name new_container_1 ..

Limiting The Memory Usage For Containers

To be able to limit the amount of memory a docker container process can use, simply use the ‘–m’ [memory amount] flag with the limit.

To use a container with memory limited to 256 MBs.

# Example: docker run -name [name] -m [Memory (int)][memory unit (b, k, m or g)] -d (to run not to attach) -p (to set access and expose ports) [image ID]

docker run -m 64m -d -p 8082:80 tutum/wordpress

To verify the memory limit, you may inspect the container.

# Example: docker inspect [container ID] | grep Memory

docker inspect 9a7562a361122706 | grep Memory

Conclusion

You should now have a Docker container running with a WordPress image.

0 Comments
Add a comment

Leave a Reply