This time we will talk about Docker Compose. I would like to tell the fact that generally we are not only using Docker. As discussed before, Docker could help us build an application faster. But how about some complicated application which contains Web Server, Application Server and Database? The 3-tier model has its connection. The question is, could we use Docker to make connection between different containers? The answer is definitely yes. We could use something called Docker Compose.
Why Should we use Docker Compose?
Apart from making relationships between different containers, there are some reasons we use Docker Compose. In fact, it is not difficult to move from Docker to Docker Compose. When we use Docker Compose, it just needs a docker-compose.yaml file instead of Dockerfile. The docker-compose.yaml file will contain the information about the containers, networks and volumes. I don’t like paper talk, let’s have a hands-on configuration with Docker Compose to build a popular open source application WordPress.
Again, in this article, we will have some comparison to let you know how Docker is different from the traditional approach.
In console, we choose Cloud Shell and then click the editor mode such that we could easily review our code.
The we have to setup our environment e.g. define project, zone and region:
gcloud config set project <you-project-id>
gcloud config set compute/zone us-central1-a
gcloud config set compute/region us-central1
This time we don’t use Cloud Shell, and directly use Compute Engine instead. So we create an instance first.
gcloud compute instances create wordpress \
–machine-type g1-small \
–zone us-central1-a \
–image-family ubuntu-1804-lts \
–image-project ubuntu-os-cloud \
Go to the Compute Engine page and SSH to the instance. Update the Ubuntu Linux by typing:
sudo apt-get update && sudo apt-get upgrade -y
Traditional Method to Deploy WordPress
- Install Apache
sudo apt install apache2 -y && systemctl status apache2
2. Install MySQL
sudo apt install mariadb-server mariadb-client -y && sudo mysql_secure_installation
When it asks you to enter current password for root, you just need to press your password. Then it asks you change the root password or not, it is up to you but I click “y” and type the password again.
Other options are up to you but if you have no idea, just follow me.
3. Install PHP
sudo apt install php php-mysql -y
4. Create WordPress Database
sudo mysql -u root -p
Then it asks your password to login, just type it. And run the below SQL command to create our database.
CREATE DATABASE wordpress_db; CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'password'; GRANT ALL ON wordpress_db.* TO 'wp_user'@'localhost' IDENTIFIED BY 'password'; FLUSH PRIVILEGES; Exit;
5. Install WordPress CMS
cd /tmp && wget https://wordpress.org/latest.tar.gz
tar -xvf latest.tar.gz
Copy the WordPress folder to
sudo cp -R wordpress /var/www/html/
Run the command below to change ownership of ‘wordpress’ directory.
sudo chown -R www-data:www-data /var/www/html/wordpress/
Change File permissions of the WordPress folder.
sudo chmod -R 755 /var/www/html/wordpress/
Create ‘uploads’ directory.
sudo mkdir /var/www/html/wordpress/wp-content/uploads
Finally, change permissions of ‘uploads’ directory.
sudo chown -R www-data:www-data /var/www/html/wordpress/wp-content/uploads/
Browse our WordPress. You could check the ip in Compute Engine Page.
Then I skip the WordPress installation process here. This is the traditional installation of WordPress, quite a long steps right?
Now we stop the Apache and make sure we cannot access.
sudo systemctl stop apache2.service
Docker Compose Method to Deploy WordPress
Then you will certainly see the power of WordPress. You could simply use 2 line commands to setup all the above things. Yes, 2 line commands, don’t line. If you download my prepared shell script, you just need 1 line command.
cd ~ && git clone https://github.com/manbobo2002/wordpress-docker-compose.git && cd ~/wordpress-docker-compose
After download my script, just kick it start by typing:
Done, that’s all. Browse it.
So, you may ask what my 2 magic command lines are. The first line is to download docker-compose and install it. The second line is run our docker-compose.yaml file by typing:
sudo docker-compose up -d
So, what’s the secret in docker-compose.yaml file?
Docker Compose yaml file
As I mentioned before, the docker-compose.yaml file will contain the information about the containers, networks and volumes.
What we have done is firstly assign a version of this application i.e. 3.3. Secondly, we define what our services are, that means the database and WordPress in our case. Then for each service, we also have to define what docker image we will use, where the data store is, the environment variables and so on. Actually, it is nothing special but just to pre-configure our setting instead of typing command one by one. You could even create one more container with the same or different configuration if you want, it could be done easily in Docker.
Of course, there are couple of docker-compose commands I do not talk, please simply check the below cheat sheet for more details.
Just Delete the VM we have created.