This is the first of many posts centered around docker and its marvelous uses in technology. As many of you know by now (i.e. 2018) the age of containers is already upon us taking over the other two historical periods (i.e Baremetal & VM) in OS deployments. The reason for this post is to present to the world a “hello world” equivalent to containerized applications and services. The primary focus will be on deploying a blog since that is the simplest and most useful platform for expression online. Furthermore, the readers of this blog would also get a practical benefit not just from learning how to deploy a blog but also about how docker containers interact with each other via ports. Let’s get started then.
WordPress blog on docker
The blog you see before you has been setup via docker-compose using WordPress, MySQL, Nginx and Let’s Encrypt. As you can imagine for most people this effort isn’t trival or well known. Well, I’m here to tell you that it’s as trivial as checking your email. The reason for this is because docker manages your configurations similarly to how gmail manages your emails. If you want to dive straight into the practical recipe check out https://github.com/ivandir/Ivandir.com_WordPress.
Deploy WordPress with docker-compose
For those of you that want to know all the details I’ll start breaking them down here line by line. In the end if you are still confused please leave a comment, otherwise if you are hungry for more knowledge head on over to https://docs.docker.com/compose/compose-file/.
Defining a docker-compose file for WordPress
// Docker compose used in www.ivandir.com
version: '3' // This is just a docker-compose version
services: // This is high level definition of your services group
db: // First service is named=db, any other name would suffice
// Consequently db also becomes a hostname accessible
// within this local docker network used by these two containers (db & wordpress)
image: mysql:5.7 // Image is just an operating system docker image with mysql v5.7 installed
volumes: // Volume is just a virtual filesystem assigned to the db service
- mysqldata:/var/lib/mysql // This is nothing more than a mapping filesystem_name:path
restart: always // Restart the service if it dies/fails for whatever reason
expose: // Expose this service's port locally
environment: // Define these environment variables. Required by mysql:5.7 image
MYSQL_ROOT_PASSWORD: wordpress // Change the values below for minimal security
wordpress: // See db service definition for clarity
depends_on: // This is simply a service dependency declaration.
- db // wordpress service won't start until db service completes its initialization
image: wordpress:latest // See db image definition. (Note: This image has wordpress and nginx in it)
links: // This is simply a link dependency declaration
- db // wordpress service links directly via port 3306 to db. Port# is implicitly declared in code
ports: // Similar to expose above however this exposes the ports globally
- "80:80" // The reason for port 80 being exposed externally exists so that nginx can serve HTML pages
restart: always // See db definition above
environment: // These are environment variables required by wordpress
WORDPRESS_DB_PASSWORD: wordpress // Must match the wordpress MYSQL_PASSWORD environment variable
volumes: // Globally define all volume names to be used by services. In this case it's just one.
driver: local // Use local communication driver within the same docker installation for these containers
Deploying WordPress using the docker-compose file
The deployment process provided that you have docker installed and docker-compose becomes very simple. After you’ve defined a docker-compose file, go to that directory and execute the following command.
docker-compose up -d
Let’s define its arguments and explain what this command means. All the above command does is call the docker-compose executable giving it the order to create and start the containers.
Furthermore because you don’t want to deal with managing the containers, the following command puts them in a detached mode (i.e run containers in the background).
That’s all there is to it. Remember all you need to do is:
- Install docker
- Install docker-compose
- Define a docker-compose file
- Run docker-compose up -d command
On a more personal level this blog is also an avenue of expression, learning and self promotion. I’ll be as direct as a line between two points when expressing opinions and when detailing technological solutions as I see them. Don’t expect perfection, after all this is a blog and my time is limited but do expect practicality and diversity.