Deploy a Dockerized CakePHP 2.x App with Docker Cloud
**IMPORTANT!**Cluster Management in Docker Cloud has been discontinued on May 25. Therefore, instructions below won’t work anymore. I’ll be creating a new article on how to deploy your app in Digital Ocean using Docker Swarm. So stay tuned!
In this blog post, I’m going to explain on how I deploy my Dockerized CakePHP 2.x application to my Digital Ocean droplet with Docker Cloud.
We’re going to deploy this application that I built from my blog post: Setup CakePHP 2.x Application with Docker from Scratch
The final source code will be available in Github on branch:
Before we start, make sure you have these prerequisites ready.
- Docker Engine
- Docker Cloud CLI. There are several ways to install it.https://docs.docker.com/docker-cloud/getting-started/deploy-app/2_set_up/
- A Docker account. https://cloud.docker.com/
- An account in Digital Ocean. If you do not have an account yet, you may register from here: https://m.do.co/c/df63f05532f9You’ll get 10$ of credit for free.
To start, let’s create a directory for this project and clone the dockerized CakePHP 2.x application.
Link your Docker Cloud and Digital Ocean accounts
You have to login to Docker Cloud account and grant access to your infrastructure. In our case, we’re going to grant access to our cloud service provider, Digital Ocean.
There is a detailed tutorial on how to link your accounts here: https://docs.docker.com/docker-cloud/infrastructure/link-do/
Create and deploy your First Node
After linking your accounts, we are going to launch our first node. When launching a node, you’ll actually be creating a node cluster. It’s best explained in the Docker’s official docs(https://docs.docker.com/docker-cloud/).
We may create a node cluster through the Docker Cloud site, or with
docker-cloud CLI. We’re going to be using the docker cloud CLI.
Open your terminal and log in to Docker. Fill the username and password with your Docker account credentials.
To be able to create a node cluster, we need the following details:
- region (data center)
To check for the list of providers, run the following command:
We will be using
digitalocean as our provider.
Next, we’re going to check for the list of data centers regions available in Digital Ocean:
You may choose the region nearest to you. I’m going to select
sgp1 (Singapore 1).
We now have the provider and region. Let’s check for available nodetypes.
I’m going to use nodetype
512mb, since it only costs $0.007/hour at the time of writing.
Now we have all the information that we need. Let’s proceed to creating and deploying our node cluster by providing the following data: name, provider, region, nodetype. You may name it however you want, but let’s name it cakephp-node-cluster.
After running the command above, it will output a uuid of your newly created node cluster.
You can verify that the node has been created successfully by running this command.
You can inspect the node with the UUID like this:
We now have a node deployed. Let’s proceed on creating the stack file for our services and deploy them to the node.
Docker Cloud Stack File
The Docker Cloud stack file is a YAML file which is very identical to a Docker Compose file. It helps you group your services and deploy them together. The application that we’re going to deploy has 2 services. The CakePHP application, and MySQL database.
But before we create the stack file, if you have read my blog post about the application that we’re going to deploy (Setup CakePHP 2.x Application with Docker from Scratch), we haven’t yet pushed the image to Docker Hub. We need to push the image because docker cloud stack file doesn’t support build. It needs to pull the image from Docker Hub.
Let’s rebuild it first just to make sure and add a tag to it:
onoya/dockerized-cakephp-app. Make sure to change
onoya to your docker username. And you can name it however you want, but let’s name it
dockerized-cakephp-app this time.
When the build is completed, let’s push it.
It may take several minutes to finish.
Now let’s create the
docker-cloud.yml file inside the root of our application directory with the content below.
Looks very identical to
docker-compose.yml file, isn’t it?
Deploy the Stack
We can now deploy our stack by running the command below.
It may take several minutes to finish deploying.
When deployment is done, let’s get the endpoint URI of our application. Start by listing the containers.
We’re going to inspect
It will output a json object. Take a look at the
Try to open a browser and access the link in endpoint_uri. You should see your application running in that URI.
And there you have your dockerized CakePHP application running on a cloud server. Note that we exposed port 8000 in the stack file and that’s why we need to append the port. You may change it to port 80 to access it without appending the port. Although I would suggest on adding a Load Balancing service.
Now, let’s say you have made some changes to your stack, you could easily redeploy with this command
You could also stop them by running
or even totally destroy or terminate your stack services
And if you want to terminate your node cluster, you could run the command below.
And that’s about it.
Please note that I’m also still in the process of learning, so I can’t guarantee you that everything that I’ve wrote here is the right way.
Please share if you’ve liked it and feel free to leave a comment below if you have any question or if you’ve found something that needs to be corrected.
Happy learning to all!