In my previous articles, we’ve covered what Docker is, and prepared our local environment. In the second article we got Docker to work with WordPress. In the third article, we saw how we can better use Docker Compose to build containers more easily and in a much more manageable way. Now, I’m going to show you how to deploy a project (WordPress) from a local machine to a DigitalOcean droplet.
Setting up Your DigitalOcean Droplet
First, create an account on DigitalOcean. DigitalOcean is a popular and affordable VPS provider. It is one of the cheaper VPS providers out there, but that being said, it doesn’t mean that cheap=poor quality. You can run a 512MB instance with 20GB of SSD storage for only $5 per month.
After updating the billing information (if you are new on DigitalOcean), you now need to create a droplet. A droplet is a single VPS instance (after all, cloud technology is all about cool names!).
Next, select a name for your instance. When you’re starting out, 512MB instance for $5/month is pretty good. Later you can upgrade this instance to 1GB, or go all the way up to 64GB instances. The region is where your instance will live. Select your instance somewhere near the region where you and/or your users will be. After all, you want the best speed for your website. In my case I chose Amsterdam, as I live in Europe. For Images you can start with a plain OS or go with an application. There is an application (pre-built box) that comes with Docker installed. Select that image.
Now you have to select an SSH key. SSH is a secure way that we can connect to our droplet and start working via the terminal. To better understand how SSH works and how to configure it for use with DigitalOcean, read this article on their blog. As I have an SSH key already configured I don’t need to add a new one. After that, simply click create droplet and it should only take a couple of seconds until it creates a fully functional Ubuntu 14.04 system with Docker pre-installed.
Installing Docker Compose
The easiest way to setup WordPress on DigitalOcean is to use Docker Compose. After all, who wants to be stuck working with cryptic commands all the time. To set up Docker Compose, we need a connection to our droplet. An SSH connection will let us execute commands on the droplet.
To install Compose, execute these two commands:
curl -L https://github.com/docker/compose/releases/download/1.2.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
Now we’ll check if Compose was installed properly:
If you get the version number, you’ve done everything correctly.
Next, create a folder for the project. Inside that, create a new
⧸⧸mkdir wpdocker cd wpdocker touch docker-compose.yml nano docker-compose.yml
docker-compose.yml file, add this:
web: image: wordpress links: - mysql environment: - WORDPRESS_DB_PASSWORD=password working_dir: /var/www/html volumes: - wordpress/wp-content/:/var/www/html/wp-content ports: - "80:80" mysql: image: mysql:5.7 environment: - MYSQL_ROOT_PASSWORD=password - MYSQL_DATABASE=wordpress
The only difference from our last example is that the IP address isn’t specified. The ports should be configured, even if port 80 on container is port 80 on the droplet, without this it wont work.
Because it’s the first time that this droplet will use Docker, it will pull all the images first. Now check on the browser for the droplet IP and you’ll get an installation WordPress page. Hurray, you have done it!
There are two problems with this example. First, if we close the terminal, the process (on the droplet) will shutdown. The second problem is that we may want to edit our theme files. This is not really a deployment, but rather just an installation on a VPS.
For the first problem, you only need to add
docker-compose up -d
Sync Local Development with DigitalOcean
This is where the fun part starts. We can use git and a repository to make changes to our local WordPress installation and push the changes to the repository. After pushing the changes to the repository we can pull those changes back to the droplet. You may want to choose a private repository. You can choose BitBucket as an alternative, since GitHub only offers public repositories if you’re not paying. In my example, I’m going to use BitBucket.
Next we need an SSH key. If you don’t have an SSH key yet, read this article on GitHub.
Once you have your SSH key, create an account on BitBucket. If you want to use GitHub feel free, it will work the same way. Create a new repository and configure your SSH keys. Your SSH keys are located under the
~/.ssh/ folder. Open the
id_rsa.pub file under the SSH folder and copy the contents of that file.
Next, go to your local project (the project that we created in the previous article if you’re following along). Execute
docker-compose up -d if it isn’t running and start making changes. Install some plugins and a theme. Also, add new posts, pages or whatever else you want.
Now navigate inside the
wp-content folder and execute these commands:
git init git remote add origin firstname.lastname@example.org:[repository].git git add . git commit -m "first init" git push -u origin master
The contents of the
wp-content folder is now in your BitBucket repository. Next we need to pull them on to our droplet. SSH to your droplet, if you’re not already logged in.
Once you’ve connected, navigate inside the project folder then:
docker-compose stop cd wordpress rm -rf wp-content
You’ll see I’ve removed the
wp-content folder on the droplet because we’ll use the content from the repository.
Now we need to generate an SSH key for the droplet, we can do this by running:
ssh-keygen -t rsa -C "your_email_here"
We need to copy the
id_rsa.pub contents again and register that SSH key on BitBucket:
Copy the contents that was echoed on the terminal and add that as a new key to BitBucket, making sure you give it a meaningful name.
git clone git clone email@example.com:[repository].git wp-content
You’ve just cloned the repository and given the new folder the name ‘
After that, navigate back to the root of the project and start the containers again.
cd .. docker-compose up -d
Now you can check your site using your browser. You should be able to login to the Admin panel and you’ll see the new plugins and theme installed. You can also export the local WordPress data and use it on your droplet installation. I’ll let you tackle this step as homework (if you like that kind of homework).
We’re now getting towards the end of this series on Docker. Docker is an exciting new technology, it’s looking like it will change the way we create applications and deploy them.
In this article we’ve seen how to deploy our local WordPress Docker installation to DigitalOcean. We created a repository on BitBucket and used that repository to sync with our DigitalOcean droplet. If you haven’t yet read our previous articles in this series on Docker, you can find them below:
- Introduction to Docker for WordPress Developers
- How to Manually Build Docker Containers for WordPress
- How to Use the Official Docker WordPress Image
We really want to hear from you. And, the same question as last time, would you consider using Docker in production?