WordPress
Article

Deploying WordPress with Docker

By Aleksander Koko

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!).

Creating a DigitalOcean Droplet

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.

The Docker Image on DigitalOcean

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.

ssh root@[ip_of_the_machine]

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:

docker-compose --version

If you get the version number, you’ve done everything correctly.

Next, create a folder for the project. Inside that, create a new docker-compose.yml file.

⧸⧸mkdir wpdocker
cd wpdocker
touch docker-compose.yml
nano docker-compose.yml

Inside your 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.

Execute:

docker-compose up

Docker DigitalOcean

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 -d.

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.

BitBucket Profile

BitBucket SSH

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 git@bitbucket.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.

ssh root@[droplet_ip]

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:

cat ~/.ssh/id_rsa.pub

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 git@bitbucket.org:[repository].git wp-content

You’ve just cloned the repository and given the new folder the name ‘wp-content‘.

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).

Conclusion

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:

We really want to hear from you. And, the same question as last time, would you consider using Docker in production?

  • Mert Kahyaoğlu

    How can i upload my sql to Docker?

    • Aleksander Koko

      It’s better if you use the export functionality of WordPress. Is this a WordPress specific project or in general ?

      • Mert Kahyaoğlu

        I didn’t know the export functionality, thanks. But yeah in general how is that possible?

    • Aleksander Koko

      Also you can use the docker exec command to execute a command inside the container. So it goes like this docker exec [container_id or name] [command that you want to execute
      ]

  • http://mert-kahyaoglu.com Mert Kahyaoğlu

    One last question, where can I access .htaccess file? Thank you in advance

    • Aleksander Koko

      Hi there. You can add an .htaccess file in your project folder. As this folder will be pushed on DigitalOcean, also this file would be deployed there. For this case on volumes (docker-compose.yml) change it to:

      volumes:
      – wordpress/:/var/www/html/

      I didn’t included all the wp files to keep things more simple. Also when working you are dealing more with wp-content folder.

      Hope helped you on this.

      • http://mert-kahyaoglu.com Mert Kahyaoğlu

        Thank you very much. Helped me a lot!

  • childoftv

    your links to articles 2 and 3 seem to be switched in the top paragraph

  • Brandon Tamm

    @aleksanderkoko:disqus – While this is the traditional way to deploy changes between dev and live via git, it is not actually the way docker is intended to be used. Docker-machine should be used to deploy image changes between dev and prod. I would be interested in an updated article using docker deployments as they were intended :)

    Thanks for taking the time anyway.

    • Menzo Wijmenga

      Exactly my thoughts. Although this method works, it’s not how it “should” be done. I’m working on some articles diving a bit more in depth about the methodologies and best practices of docker. There will also be a article available that explains how to deploy wordpress websites in a automated and scalable way.

      I feel like there’s not enough high quality information available on this subject that cater to people just starting with docker.

      Research and writing takes a lot of time though.. If you’re interested I could send you my articles once they’re finished.

      • Brandon Tamm

        I think thousands of people would be interested.

        The valuable concept for people to grasp is that a drastic shift has been made… As best-practice dev-ops, we now deploy:
        – architecture (service images and containers via Docker)
        – code (apps, templates, modules, etc via git & composet)
        – content (db import/export, media tar/untar)

        Whereas before we may have used git (or hopefully not ftp) for all of this, it wasn’t the appropriate tool to be using. We now have tools dedicated to these very different deployments.

        • Aleksander Koko

          Yep you are right on the deployment part. This is a way to do deployment, not the best one tough but Its one way. The article is 1 year old and many members of the docker community were grasping the new way of working with containers.

          • Brandon Tamm

            Actually – thousands is probably an understatement….Your analytics for this article will give you a true picture :)

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Instant Website Review

Use Woorank to analyze and optimize your website to improve your website to improve your ranking!

Run a review to see how your site can improve across 70+ metrics!

Get the latest in WordPress, once a week, for free.