Regardless of what you might have read or heard about databases on Docker: It’s a thing, and a major one at that. According to Datadog, the second most popular Docker image is Redis, the third most popular image is Elasticsearch, followed by Postgres, and MySQL on places five and six – and finally there’s MongoDB at nineth place.
However, there’s dissent about the usage of databases in containers, with some even warning about it in biblical fashion. Being strong advocates for Docker and containers, here’s our guide to Databases on Docker.
Setup within seconds
Docker offers for a fast setup and portability of applications in containers. This comes in especially handy when you’re working locally and need to deploy elsewhere. With almost all web projects relying on a database in their backend, it’s no surprise that images of database management-systems are so often searched for.
Check out the docker-compose example for Apache CouchDB, a document-oriented NoSQL database management-system that comes with a built-in web administration tool. To install CouchDB with Docker, simply copy the following code and save it into
docker-compose.yml, then run
docker-compose up in your terminal.
version: "3" services: db: image: "couchdb:2.1.1" ports: - 5984:5984 volumes: - /usr/local/var/lib/couchdb
Make sure you’re using the latest version and preferably an official release, as they are updated and maintained regularly. You can head over to Docker Hub to see which images suit you. In this case, we’ve used the official CouchDB image in the latest version 2.1.1. The same result can also be achieved with a one-liner using
docker run -p 5984:5984 -d -v $(pwd):/usr/local/var/lib/couchdb couchdb:2.1.1
Your CouchDB instance should be up and running now. It comes with an attached volume (we’ll get to the importance of that later). If you want to access Futon, the built-in administration tool, open //localhost:3000:5984/_utils – that’s it!
But they warned about it
Perhaps the biggest challenge for those who have worked extensively with database management-systems before is to adapt to the parameters and requirements of the Docker environment and its container-based architecture.
Much of the criticism found online focuses on this new environment not being able to recreate a structure of old. If you do want to adapt and to experience the manifold advantages of Docker containers, please keep the following in mind:
Storing it the right way
“Shared storage sucks”, they say, and they are right. So for your database, you don’t want your data stored that way. But you don’t have to! We use ScaleIO as our storage system, which prevents volumes to be used by multiple containers and is optimized for performance. There are also alternatives like GlusterFS.
Keeping your data persistent
With bloggers and Hackernews contributors moaning over the ideal that containers should be stateless, it’s much easier to accept that databases simply need states and therefore go with it. If you want to keep your data persistent (and you do want that), just combine your database with a volume and you won’t have any trouble. At sloppy.io, our block-storage system provides just that, along with mandatory volumes for databases.
Nevermind masters and slaves (unless you have to)
Accepting the frame conditions of a different environment also applies to this one: While many database systems allow for master-slave or master-master setups, it is better for a database in a container to simply have a single instance combined with a single volume, thus keeping the system consistent and persistent.
For a lot of small to medium size applications, this is more than enough. For large applications that require a cluster, the cluster has to still be configured properly. Scaling a container to multiple instances with shared storage would not help here.
Host your database at sloppy.io
Meanwhile, we frequently get asked about how to implement and manage database systems on sloppy.io, so we added a series of beginner-friendly tutorials and a quick reference guide to our knowledge base. Note that we’ve provided a web-based administration tool for each database. These are required to access your database directly, since we don’t allow direct TCP connections. The tutorials so far include:
Our goal here was to show you how quick and easy setting up a database on sloppy.io can be by just using the sloppy.io admin panel. Now, a lot of you work with the CLI and you can easily adapt to it by using the environment variables presented in the posts and the reference guide.
There are a lot of people out there, though, who don’t use a terminal and it’s important for us to show them that sloppy.io is not just for experienced developers but for anyone who wants to start their web project.
That said, we’d like to know if you find these tutorials helpful and if there are database systems we should add to the list. Just comment below, tweet us, or contact us via our helpdesk.
If you’re not yet using sloppy.io you are welcome to use these examples and make them into yml for docker-compose. Better yet: come on over and use our free 14-day-trial and see for yourself what sloppy.io can do for your project.
Featured Image CC BY-SA 2.0 Dwight Stone on Flickr