A few days back Lucas Carlson, founder of AppFog and CIO of Century Link, wrote a great blog post on the topic at hand: how to build a 2-container application with Docker. This article can be found here.
In this blog post, I’m going to show you how to launch a 2-container application using Docker and Tutum. For this example, we’re going to be using the combination of a WordPress container plus a MySQL container, but this exact same procedure can be used to link just about any combination of 2 or more containers.
Step 1 – Start with a MySQL container
WordPress needs a DB to run, we could have both the DB and the APP within a single Docker container, but that would both defeat the purpose of this tutorial, as well as make it much much harder to scale in the future.
And below the Dockerfile:
FROM ubuntu:quantal MAINTAINER Fernando Mayo # Install packages RUN apt-get update RUN apt-get -y upgrade RUN ! DEBIAN_FRONTEND=noninteractive apt-get -y install supervisor mysql-server pwgen # Add image configuration and scripts ADD https://raw.github.com/tutumcloud/tutum-docker-mysql/master/start.sh /start.sh ADD https://raw.github.com/tutumcloud/tutum-docker-mysql/master/run.sh /run.sh ADD https://raw.github.com/tutumcloud/tutum-docker-mysql/master/supervisord-mysqld.conf /etc/supervisor/conf.d/supervisord-mysqld.conf ADD https://raw.github.com/tutumcloud/tutum-docker-mysql/master/my.cnf /etc/mysql/conf.d/my.cnf ADD https://raw.github.com/tutumcloud/tutum-docker-mysql/master/create_mysql_admin_user.sh /create_mysql_admin_user.sh ADD https://raw.github.com/tutumcloud/tutum-docker-mysql/master/import_sql.sh /import_sql.sh RUN chmod 755 /*.sh EXPOSE 3306 CMD ["/run.sh"]
This MySQL image is available as part of Tutum’s Official Images, meaning we’re just a few clicks away from deploying it.
Launch a new container by clicking on the green + New Container button. A modal will come up, click on the Tutum images tab and select the tutum/mysql image.
VERY IMPORTANT: Since Tutum, like Docker, uses container names to link containers, it is very important to set the correct name here. The WordPress image that we’ll be using expects the MySQL Database container name to be “DB”. So we will set the name accordingly (see image below).
For the 3rd step Environment Variables, we can simply click Finish as there is no need to set up any ENV VARs at this stage.
In just a few seconds you’ll get a new MySQL container up and running. Click on it’s name or the magnifying glass next to it to see the container’s details, and from the new window, click on the Logs tab.
In the logs you’ll find the admin password for the newly created MySQL container. In the screenshot below the password is yBH0rIDejZ3I
We will be needing this password in Step 4.
Step 2 – Setup WordPress Container
This is the trickier, yet not so tricky part. In order for WordPress to use the MySQL container that we just launched, our WordPress container needs to be made ‘aware’ of the existence of the MySQL DB. We achieve this through the use of environment variables in the WordPress wp-config.php file.
// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', getenv('WORDPRESS_DB_NAME')); /** MySQL database username */ define('DB_USER', 'admin'); /** MySQL database password */ define('DB_PASSWORD', getenv('DB_PASSWORD')); /** MySQL hostname */ define('DB_HOST', getenv('DB_PORT_3306_TCP_ADDR').":".getenv('DB_PORT_3306_TCP_PORT'));
For our Docker Image, we’re going to extend from tutum/apache-php since that provides us with a foundation in which to install WordPress. This is the Dockerfile for our wordpress-nosql image:
FROM tutum/apache-php MAINTAINER Borja Burgos <email@example.com> # Install packages RUN apt-get update && apt-get -y upgrade && apt-get -y install mysql-client # Download latest version of WordPress into /app RUN rm -fr /app && git clone https://github.com/WordPress/WordPress.git /app # Add wp-config with info for WordPress to connect to DB ADD https://raw.github.com/tutumcloud/tutum-docker-wordpress-nosql/master/wp-config.php /app/wp-config.php RUN chmod 644 /app/wp-config.php # Add script to create 'wordpress' DB ADD https://raw.github.com/tutumcloud/tutum-docker-wordpress-nosql/master/run.sh run.sh RUN chmod 755 /*.sh #Default DB is named 'wordpress', modify with environment variable to change ENV WORDPRESS_DB_NAME wordpress EXPOSE 80 CMD ["/run.sh"]
Let’s now analyze the run.sh that’s included in the repository and that will be executed to run this image.
#!/bin/bash if [ -f /.mysql_db_created ]; then exec supervisord -n exit 1 fi DB_EXISTS=$(mysql -uadmin -p$DB_PASSWORD -h$DB_PORT_3306_TCP_ADDR -P$DB_PORT_3306_TCP_PORT -e "SHOW DATABASES LIKE '"$WORDPRESS_DB_NAME"';" | grep "$WORDPRESS_DB_NAME" > /dev/null; echo "$?") if [[ DB_EXISTS -eq 1 ]]; then echo "=> Creating database $WORDPRESS_DB_NAME" RET=1 while [[ RET -ne 0 ]]; do sleep 5 mysql -uadmin -p$DB_PASSWORD -h$DB_PORT_3306_TCP_ADDR -P$DB_PORT_3306_TCP_PORT -e "CREATE DATABASE $WORDPRESS_DB_NAME" RET=$? done echo "=> Done!" fi echo "=> Skipped creation of database $WORDPRESS_DB_NAME – it already exists." touch /.mysql_db_created exec supervisord -n
Here’s what’s going on:
- Check if this container has already attempted to create the DB, if so, simply start supervisor and exit.
- Check if the DB that we’re trying to create already exists, if it does not, create it. By default the name of the DB is set to ‘wordpress’. This is done by an environment variable called WORDPRESS_DB_NAME defined in the container’s Dockerfile above. If one wanted to use a different name, it’s as simple as redefining the value for this ENV VAR at runtime. If were using the Docker CLI client this is done with the ‘-e’ flag, for Tutum we will show you how shortly.
- Create a .mysql_db_created file. We check the existence of this file on step 1. This helps us avoid any DB interaction if we were to stop and restart the container in the future.
- Start supervisord
Step 3 – Launch WordPress Container
With our WordPress Image ready for action we can now launch a WordPress container that will connect to the MySQL DB that is already up and running.
Once again, from Tutum’s management console, click on + New Container. This WordPress sans SQL image is not part of Tutum images so we will instead pull the image from Docker’s Public Index by searching for borja and selecting the borja/wordpress-nosql image.
The container name in Step 2 “Configure the container” is not as crucial as it was for the DB, but nonetheless, we recommend using a friendly name, such as “WordPress”.
Step 4 – Link WordPress/MySQL Container
And here’s where the magic happens. First thing you’ll notice is that this image already has a number of ENV VAR defined, one of which is WORDPRESS_DB_NAME with the value wordpress. If you wish to modify this value, simply add a new environment variable with the same name, and a new value. Like shown in the screenshot below, where the name of the default wordpress DB was changed to wordpress2. Now we will also set the DB’s password. Add another environment variable named DB_PASSWORD, the value of which has to be the admin password for the MySQL container we launched earlier, remember from Step 1 that in our case, it was: yBH0rIDejZ3I .
You’ll see icons next to the variable names. The camera icon indicates the wariable was loaded from the image, the human icon that the variable was defined by the user, and the link icon that the variable is linked from another container. Note that the HOME and PATH variables are default in every container but can, for this use-case, be disregarded.
Now we need to add the environment variables of the DB. We do this by simply clicking on Select containers next to Link containers. You’ll see a drop down of all your containers. Please select DB as that’s how we named the MySQL container.
Lastly, click on Finish.
Step 5 – Check it’s working
Once the WordPress container finishes launching click on its name or the magnifying glass to see its details. If it were to fail to start due to problems pulling from the Docker’s registry, you can retry by clicking on Actions > Start.
From the Container Details, click on the Web Endpoints link. If everything goes well, you’ll be presented with the WordPress Installation screen.
Step 6 – Video Demonstration
Step 7 – Conclusion
You should now be able to link any 2 or more containers, very simply, using Docker and Tutum. Leveraging readily available images and Dockerfiles, you can do so without even touching the CLI.
What’s neat is that you can now create multiple instances of the WordPress container that all talk to the same MySQL database. This will be rather useful when we introduce Container Clusters with [auto]scaling capabilities.
Do note that this setup described is far from ideal for a a production environment. The MySQL container’s data is ephemeral and will not persist if the container is terminated. To mitigate this issue we’re also working on a Persistent Storage solution which will be made available in the near future.
To get notified of our next blog post with more Docker tricks and tutorials, and cool and innovative ways to deploy using Tutum’s infrastructure, please sign up at the bottom right corner, or follow us on Twitter at http://twitter.com/tutumcloud.