Chapter 5. Docker Plugins

During DockerCon Europe 2014, there was a round table discussion which took place on the state of the Docker ecosystem, the following problem and possible solution was identified:

The problem which Docker currently faces is that by moving to become a platform it is being seen to threaten its own ecosystem. The proposed solution is that Docker ships its own additions to Docker as late-bound, composable, optional extensions and enables other vendors to do likewise. Docker calls this "batteries included but removable".

During DockerCon 2015 in Seattle, Docker announced the availability of plugins in the experimental branch, the announcement came in the form of a blog post which can be found at https://blog.docker.com/2015/06/extending-docker-with-plugins/.

As you can see from the post, Docker provided a solution where third parties can swap out core functionality. Now a user could run the docker volume and docker network commands along with a driver option to have Docker call external components which add functionality outside of the core Docker Engine while maintaining a high level of compatibility.

In this chapter, we are going to look at two different Docker plugins, the first is a volume plugin called REX-Ray and the second is a network plugin called Weave.

REX-Ray volume plugin

So far, we have been usingthe local storage which is available on our hosts, as mentioned in Chapter 4, Docker Swarm that isn't very useful when you potentially have move the storage between multiple hosts either because you are hosting a cluster or because of problems with the host machine itself.

In this example, we are going to be launching a Docker instance in Amazon Web Services, install a volume plugin called REX-Ray, written by EMC, and then launch our WordPress example but this time we will attach AmazonElastic Block Storage volumes to our containers. Once configured, we will move our containers to a second host machine to demonstrate that the data has persisted.

REX-Ray supports several storage types on both public clouds and EMC's own range, as follows:

The driver is in active development and more types of supported storage are frequently being added, also work is on-going to move the driver over to Dockers new plugin system.

Before we look at installing REX-Ray we need a Docker host in Amazon Web Services, to launch one, use the following command. You can refer to the Amazon Web Services Driver section of Chapter 2, Launching Applications Using Docker. for details on how to generate your access and secret key and find your VPC ID. Remember to replace the access-key, secret-key and vpc-id with your own:

docker-machine create 
    --driver amazonec2 
    --amazonec2-access-key AKIAJ3GYNKVTEWNMFDHQ 
    --amazonec2-secret-key l2WikM2NIz2GA+1Q2PGKVUCfTNBPBT1Nzgf+jDJC 
    --amazonec2-vpc-id vpc-35c91750 
    awstest

Now that you have your instance launched, you can see it in the AWS Console:

REX-Ray volume plugin

We need to install the REX-Ray plugin. As REX-Ray supports Docker's new plugin format this means we need to run the docker plugin command. To start with, we need to configure our local Docker client to connect to our AWS host by running:

eval $(docker-machine env awstest)

Now that we are connected to install the plugin, we simply need to run the following command, the EBS_ACCESSKEY and EBS_SECRETKEY variables are the same we used for Docker Machine, replace them with your own:

docker plugin install rexray/ebs 
EBS_ACCESSKEY=AKIAJ3GYNKVTEWNMFDHQ 
EBS_SECRETKEY=l2WikM2NIz2GA+1Q2PGKVUCfTNBPBT1Nzgf+jDJC

Before the plugin is installed, you will be asked to confirm that you are OK to grant permissions for the plugin to access various parts of your Docker installation, answer yes (y) to this when prompted and the plugin will be downloaded and installed.

Now that the plugin is installed, we need to create two volumes, one which will hold our WordPress data and the second one will back store our MySQL databases. To create the volumes run the following:

docker volume create --driver rexray/ebs --name dbdata
docker volume create --driver rexray/ebs --name wpdata

You can see the preceding commands being run in the following terminal:

REX-Ray volume plugin

You should also be able to see your two volumes by clicking on Volumes in the left-hand side menu of the EC2 section of the AWS Console:

REX-Ray volume plugin

Now we have our two volumes, we need to launch WordPress, to do this we will use the Docker Compose file which can be found in the repo at /bootcamp/chapter05/wordpress-rexray/.

As you can see from the docker-compose.yml file, we are building a WordPress image with wp-cli installed:

version: "3"

services:
mysql:
     image: mysql
     volumes:
       - dbdata:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: wordpress
       MYSQL_DATABASE: wordpress
wordpress:
depends_on:
       - mysql
     build: ./
     volumes:
       - wpdata:/var/www/html
     ports:
       - "80:80"
     restart: always
     environment:
       WORDPRESS_DB_PASSWORD: wordpress

volumes:
dbdata:
      external:
        name: dbdata
wpdata:
      external:
        name: wpdata

As you can also see from the end of the file, we are telling Docker Compose to use the two external volumes we have already created with the docker volume create command.

To build our WordPress image and launch the containers run the following command:

docker-compose up -d

You can check your containers up by running:

docker-compose ps

Now that the two containers we make our WordPress application are up and running you can quickly install WordPress by running the following command (update the variables as needed):

$awshost = "$(docker-machine ip awstest)"
docker-compose exec wordpress wp core install --url=http://$(awshost)/ --title=Testing --admin_user=admin --admin_password=adminpassword [email protected]

Once installed, you should see a message which says Success: WordPress installed successfully. This means that you can open your installation in a browser by running:

open http://$(docker-machine ip awstest)

This should present you with the now familiar WordPress site:

REX-Ray volume plugin

Now let's make a change to our WordPress installation so we can be sure that when we move our application between hosts everything works as expected. We are going to be replacing the image of the plant with fireworks. To do this we need to customize our theme, to get to the theme edit page run the following:

open "http://$(docker-machine ip awstest)/wp-admin/customize.php?return=%2Fwp-admin%2Fthemes.php"

You will be prompted to login using the admin username and password which if you followed the installation will be admin / adminpassword or if you entered your own then use them.

Once you have the page open click on Header Media in the left-hand menu. Scroll down to where it says Add new image in the left-hand menu and follow the on-screen prompts to upload, crop and set the new header image, you can find an image called fireworks.jpg in the repo or use your own image. Once you have finished click on Save & Publish.

Going back to your sites home page should then show your new header image:

REX-Ray volume plugin

Before we remove our Docker host we need to make a note of it's IP address, to do this run the following command:

echo $(docker-machine ip awstest)

And write down the IP address as we are going to need it, in my case the IP address was 54.173.130.142.

Now let's remove our host using the following command:

docker-machine rm awstest

Once the host has been removed our two volumes are shown as available within the AWS console:

REX-Ray volume plugin

That is our WordPress and database data, to access it on a new Docker host we need to first launch one. To do this run the following command again remembering to replace the credentials and vpc id with your own:

docker-machine create 
    --driver amazonec2 
    --amazonec2-access-key AKIAJ3GYNKVTEWNMFDHQ 
    --amazonec2-secret-key l2WikM2NIz2GA+1Q2PGKVUCfTNBPBT1Nzgf+jDJC 
    --amazonec2-vpc-id vpc-35c91750 
    awstest2

Once the new Docker host is up and running the following command to switch our local client over and install REX-Ray:

eval $(docker-machine env awstest2)
docker plugin install rexray/ebs 
  EBS_ACCESSKEY=AKIAJ3GYNKVTEWNMFDHQ 
  EBS_SECRETKEY=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

Once REX-Ray is installed, we need to make it aware of our two existing volumes, to do this simply run the following command:

docker volume create --driver rexray/ebs --name dbdata
docker volume create --driver rexray/ebs --name wpdata

Do not worry, it will not overwrite our existing volumes, it will just make Docker aware that they are there as REX-Ray uses the name you assign to volume rather than a unique ID if it comes across a volume with the name you have told it to use it will assume that is the volume you meant to use, so be careful when naming your volumes as they will be attached to the running container.

You may notice that the commands execute a lot quicker this time, this is because the volumes are already there and do not need re-creating.Running:

docker volume ls

should show our two volumes are there as before.

Now we need to launch WordPress, to do that just run:

docker-compose up -d

If you were to try and access your WordPress site now, you would see a very broken looking site with content, but no styling or images.

This is because the database is still referencing the IP address of the Docker host we terminated, to the database. Run the following the command making sure to replace the IP address in the command to that of your previous Docker host (remember mine was 54.173.130.142):

docker-compose exec wordpress wp search-replace 54.173.130.142 $(docker-machine ip awstest2)

You should see a list of every table within the database along with confirmation of how many instances of the IP address it has replaced with that of the new Docker host.

Going to your new WordPress installation by running:

open http://$(docker-machine ip awstest2)

Should show your cover image is intact and the WordPress installation is exactly how you left it, apart from the change in IP address.

When you have finished test you can remove your installation by running the following commands:

docker-compose stop
docker-compose rm
docker volume rmdbdata
docker volume rmwpdata
docker-machine rm awstest2

Note

You may notice that when you run the docker volume rm commands you are not prompted to confirm your actions, so be careful.

Checking your AWS console should confirm that your Docker host has been terminated and your two volumes have been removed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.154.2