Docker Memcached step by step

Memcached on Docker

Memcached is a distributed, open-source data storage engine. It was designed to store certain types of data in RAM (instead of slower rate traditional disks) for very fast retrievals by applications, cutting the amount of time it takes to process requests by reducing the number of queries performed against heavier datasets or APIs such as traditional databases (e.g. MySQL).

By introducing a smart, well-planned, and optimized caching mechanism, it becomes possible to handle a seemingly larger amount of requests and perform more procedures by applications. This is the most important use case of Memcached, as it is with any other caching application or component.

Heavily relied upon and used in production for web sites and various other applications, Memcached has become one of the go-to tools for increasing performance without -necessarily – needing to utilize further hardware (e.g. more servers or server resources).

It works by storing keys and their matching values (up to 1 MB in size) onto an associative array (i.e. hash table) which can be scaled and distributed across a large number of virtual servers.

Installing Docker on Ubuntu (Latest)

To start using the Docker project on your VPS, you can either use DigitalOcean’s docker image for Ubuntu 13.04 or install it yourself. In this section, we will quickly go over the basic installation instructions for Docker 0.7.1.

Installation Instructions for Ubuntu

Update your droplet:

Make sure aufs support is available:

Add docker repository key to apt-key for package verification:

Add the docker repository to aptitude sources:

Update the repository with the new addition:

Finally, download and install docker:

Ubuntu’s default firewall (UFW: Uncomplicated Firewall) denies all forwarding traffic by default, which is needed by docker.

Enable forwarding with UFW:

Edit UFW configuration using the nano text editor.

Scroll down and find the line beginning with DEFAULTFORWARDPOLICY.



Press CTRL+X and approve with Y to save and close.

Finally, reload the UFW:

Basic Docker Commands

Before we begin working with docker, let’s quickly go over its available commands to refresh our memory from our first Getting Started article.

Running the docker daemon and CLI Usage

Upon installation, the docker daemon should be running in the background, ready to accept commands sent by the docker CLI. For certain situations where it might be necessary to manually run docker, use the following.

Running the docker daemon:

docker CLI Usage:

Note: docker needs sudo privileges in order to work.

Commands List

Here is a summary of currently available (version 0.7.1) docker commands:


Attach to a running container


Build a container from a Dockerfile


Create a new image from a container’s changes


Copy files/folders from the containers filesystem to the host path


Inspect changes on a container’s filesystem


Get real time events from the server


Stream the contents of a container as a tar archive


Show the history of an image


List images


Create a new filesystem image from the contents of a tarball


Display system-wide information


Insert a file in an image


Return low-level information on a container


Kill a running container


Load an image from a tar archive


Register or Login to the docker registry server


Fetch the logs of a container


Lookup the public-facing port which is NAT-ed to PRIVATE_PORT


List containers


Pull an image or a repository from the docker registry server


Push an image or a repository to the docker registry server


Restart a running container


Remove one or more containers


Remove one or more images


Run a command in a new container


Save an image to a tar archive

Search for an image in the docker index


Start a stopped container


Stop a running container


Tag an image into a repository


Lookup the running processes of a container


Show the docker version information

Getting Started with Creating Memcached Images

Building on our knowledge gained from the previous articles in the docker series, let’s dive straight into building a Dockerfile to have docker automatically build Memcached installed images (which will be used to run sandboxed Memcached instances).

Quick Recap: What Are Dockerfiles?

Dockerfiles are scripts containing commands declared successively which are to be executed, in the order given, by docker to automatically create a new docker image. They help greatly with deployments.

These files always begin with the definition of a base image by using the FROM command. From there on, the build process starts and each following action taken forms the final with commits (saving the image state) on the host.


Note: To learn more about Dockerfiles, check out our article: Docker Explained: Using Dockerfiles to Automate Building of Images.

Dockerfile Commands Overview


Copy a file from the host into the container


Set default commands to be executed, or passed to the ENTRYPOINT


Set the default entrypoint application inside the container


Set environment variable (e.g. “key = value”)


Expose a port to outside


Set the base image to use


Set the author / owner data of the Dockerfile


Run a command and commit the ending result (container) image


Set the user to run the containers from the image


Mount a directory from the host to the container


Set the directory for the directives of CMD to be executed

Creating a Dockerfile

Since Dockerfiles constitute of plain-text documents, creating one translates to launching your favourite text editor and writing the commands you want docker to execute in order to build an image. After you start working on the file, continue with adding all the content below (one after the other) before saving the final result.

Note: You can find what the final Dockerfile will look like at the end of this section.

Let’s create an empty Dockerfile using nano text editor:

We need to have all instructions (commands) and directives listed successively. However, everything starts with building on a base image (set with the FROM command).

Let’s define the purpose of our Dockerfile and declare the base image to use:

After this initial block of commands and declarations, we can begin with listing the instructions for Memcached installation.

Set the default port to be exposed to outside the container:

Set the default execution command and entrpoint (i.e. Memcached daemon):

Final Dockerfile

After having everything written inside the Dockerfile, save it and exit by pressing CTRL+X followed by Y.

Using this Dockerfile, we are ready to get started with dockerised Memcached containers!

Creating the Docker Image for Memcached Containers

We can now create our first Memcached image by following the usage instructions explained in the Dockerfile Basics section.

Run the following command to create an image, tagged as “memcached_img”:

Note: Do not forget the trailing . for docker to find the Dockerfile.

Running dockerised Memcached Containers

It is very simple to create any number of perfectly isolated and self-contained memcached instances – now– thanks to the image we have obtained in the previous section. All we have to do is to create a new container with docker run.

Creating a Memcached Installed Container

To create a new container, use the following command, modifying it to suit your requirements following this example:

Now we will have a docker container named “memcachedins”, accessible from port 45001, run using our image tagged “memcachedimg”, which we built previously.

Limiting the Memory for a Memcached Container

In order to limit the amount of memory a docker container process can use, simply set the -m [memory amount] flag with the limit.

To run a container with memory limited to 256 MBs:

To confirm the memory limit, you can inspect the container:

Note: The command above will grab the memory related information from the inspection output. To see all the relevant information regarding your container, opt for sudo docker inspect [container ID].

Testing the Memcached Container

There are various ways to try your newly created Memcached running container(s). We will use a simple Python CLI application for this. However, you can just get to production with your application using caching add-ons, frameworks, or libraries.

Make sure that your host has the necessary libraries for Python / Memcached:

Let’s create a simple Python script called “” using nano:

Copy-and-paste the below (self-explanatory) content inside:

Press CTRL+X and approve with Y to save and close.

Testing a docker memcached instance using the script above from your host:

For the full set of instructions to install and use docker, check out the docker documentation at