Docker is a container technology for Linux that allows a developer to package up an application with all of the parts it needs. It makes it easier to create, deploy, port and run applications by using containers. Docker Compose makes dealing with the orchestration processes of Docker containers (such as starting up, shutting down, and setting up intra-container linking and volumes) really easy.
To really take full advantage of Docker’s potential, it’s best if each component of your application runs in its own container. For complex applications with a lot of components, orchestrating all the containers to start up and shut down together can quickly become knotty.
To deal with this, the Docker team decided to come up with a solution based on the Fig source called Docker Compose.
This article provides a real-world example of using Docker Compose to install an application, in this case WordPress with PHPMyAdmin as an extra. WordPress normally runs on a LAMP stack, which means Linux, Apache, MySQL/MariaDB, and PHP. The official WordPress Docker image includes Apache and PHP for us, so the only part we have to worry about is MariaDB.
- Host Machine: Ubuntu 14.04 OMegha Bolt (VM).
- A non-root user with sudo privileges.
- Reasonable knowledge of Linux commands.
The Docker installation package available in the official Ubuntu 14.04 repository may not be the latest version. To get the latest and greatest version, install Docker from the official Docker repository. To do that;
First, add the GPG key for the official Docker repository to the system:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker repository to APT sources:
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Update the package database with the Docker packages from the newly added repo:
$ sudo apt-get update
$ sudo apt-get install –y docker-ce
Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:
$ sudo service docker status
Installing Docker Compose:
Now that you have Docker installed, let’s go ahead and install Docker Compose. First, install python-pip as prerequisite:
$ sudo apt-get install python-pip
Install Docker Compose:
$ sudo pip install docker-compose
Docker Compose makes dealing with the orchestration processes of Docker containers (such as starting up, shutting down, and setting up intra-container linking and volumes) really easy.
Installation of Application:
Here we’ll install a WordPress application with PHPMyAdmin using Docker-compose. WordPress normally runs on a LAMP stack, which means Linux, Apache, MySQL/MariaDB, and PHP. The official WordPress Docker image includes Apache and PHP for us, so we’ll have to run MariaDB container as a source for database purposes.
Create a folder where our data will live and create the docker-compose.yml file to run our app:
$ mkdir ~/wordpress $ cd wordpress
Then create a docker-compose.yml with your any text editor.
$ sudo vi ~/wordpress/docker-compose.yml
And paste in the following:
wordpress: image: wordpress links: - wordpress_db:mysql ports: - 8080:80 wordpress_db: image: mariadb environment: MYSQL_ROOT_PASSWORD: mypassword phpmyadmin: image: nazarpc/phpmyadmin links: - wordpress_db:mysql ports: - 8181:80 environment: MYSQL_USERNAME: root MYSQL_ROOT_PASSWORD: mypassword
Explaining the YML file:
- Docker Compose to start a new container called wordpress and download the wordpressimage from the Docker Hub.
- Define a new container called wordpress_db and told it to use the mariadbimage from the Docker Hub.
- WordPress container to link our wordpress_db container into the wordpress container and call it mysql (inside the wordpress container the hostname mysql will be forwarded to our wordpress_db container).
- Set the MYSQL_ROOT_PASSWORD variable to start the mariadb server. The MariaDB Docker image is configured to check for this environment variable when it starts up and will take care of setting up the DB with a root account with the password defined as MYSQL_ROOT_PASSWORD.
- Set up a port forward so that we can connect to our WordPress install once it actually loads up. The first port number is the port number on the host, and the second port number is the port inside the container. So, this configuration forwards requests on port 8080 of the host to the default web server port 80 inside the container.
- Grabs docker-phpmyadmin by community member nazarpc, links it to our wordpress_db container with the name mysql, exposes its port 80 on port 8181 of the host system, and finally sets a couple of environment variables with our MariaDB username and password.
This image does not automatically grab the MYSQL_ROOT_PASSWORD environment variable from the wordpress_dbcontainer’s environment the way the wordpress image does. We actually have to copy the MYSQL_ROOT_PASSWORD: mypassword line from the wordpress_db container, and set the username to root.
Now start up the application group:
$ sudo docker-compose up -d
Run it with the -d option, which will tell docker-compose to run the containers in the background so that you can keep using your terminal.
One can see the corresponding images being pulled and run as containers. Towards the end of the output lines you can see that the containers are created and have started running;
Creating wordpress_wordpress_db_1 ... Creating wordpress_wordpress_db_1 ... done Creating wordpress_wordpress_1 ... Creating wordpress_phpmyadmin_1 ... done Creating wordpress_wordpress_1 Creating wordpress_phpmyadmin_1 ... done
To verify this, list the running containers using the command
$ sudo docker ps
To verify that the app is up, open up a web browser and browse to the IP of your box on port 8080. Here the host IP address we use for this document is of format xx.xxx.xx.xxx.
Type <host-IP>:8080 into the browser. This should land a WordPress installation page as in the picture.
For the demo purposes, fill the fields as per below and install the application.
Successful installation will land the following page.
To verify the phpMyAdmin, open another tab and type xx.xxx.xx.xxx:8181 into it.
This should open up the phpMyAdmin page.
To login, use the details that were used in the YML file. In this case, the user is root, and the password is ‘mypassword’.
Upon logging in, one will be able to access the databases in the MariaDB server as the picture shows;
Here by using Docker-compose and Docker concepts in general, installations of wordpress and phpMyAdmin have been made much easier than the traditional ways of installing these applications.