Docker Explained: How To Quickly Create Docker Containers Running Memcached

A few days back, the folks at Digital Ocean posted a great article on how to create a Docker container to run Memcached. If you have not read it, I recommend you go do that now, it is available here:

https://www.digitalocean.com/community/articles/docker-explained-how-to-create-docker-containers-running-memcached

In this short post, I’d like to show you how you can achieve the same result in a simple and more streamlined process thanks to Tutum.

Let’s start off with the same Dockerfile given in the Digital Ocean article.

############################################################
# Dockerfile to run Memcached Containers
# Based on Ubuntu Image
############################################################

# Set the base image to use to Ubuntu
FROM ubuntu

# Set the file maintainer (your name - the file's author)
MAINTAINER Maintaner Name

# Update the default application repository sources list
RUN apt-get update

# Install Memcached
RUN apt-get install -y memcached

# Port to expose (default: 11211)
EXPOSE 11211

# Default Memcached run command arguments
# Change this value in Tutum to modify mem size
CMD ["-m", "64"]

# Set the user to run Memcached daemon
USER daemon

# Set the entrypoint to memcached binary
ENTRYPOINT memcached

In the future, you’ll be able to create Docker images from Dockerfiles like the one above right from within Tutum. In the mean time, we have two options to get the image produced from this Dockerfile to Tutum: push it to Docker’s public index, or push it to Tutum’s or another 3rd party private registry.

We will be tackling the first approach in this post and show you how to push it to Docker’s public index.

From within the same folder where your Dockerfile is located, do the following:

sudo docker build -t memcached -rm -no-cache .

This will build a new Docker image, name/tag it “memcached”, the -rm flag remove intermediate containers after a successful build and the -no-cache prevents the use of cache when building the image.

Now, let’s tag it so that we are able to push it to the public index. First make sure you have created an account at http://index.docker.io

# Example: sudo docker tag memcached borja/memcached
sudo docker tag memcached docker_index_username/memcached

# Example: sudo docker push borja/memcached
sudo docker push docker_index_username/memcached

It will ask you to authenticate, and then, it will push your Memcached image to the public index. Alternatively, one could enable the latest feature from the Docker public index “Trusted Builds”, to add the Docker Image to the public index right from within Github – we will be exploring that possibility in another post.

Screen-Shot-2013-12-27-at-4.01.18-PM

With our Memcached image in the public index let’s see how we can run it in Tutum.

After you log in, click on “+ New Container” button on the top right corner. On the modal screen that shows up we want to select the tab that says “Docker index”, and search by the name of our image or our Docker index username.

Screen-Shot-2013-12-27-at-4.04.51-PM

After selecting the [latest] tag, we will be presented with a screen to configure our container. Here we can specify a name for our container, a size, see the entry point which was defined in the Dockerfile/Image, modify the RUN command (change 64 to have Memcached use more memory), and add any additional TCP/UDP ports we want exposed to the world.

Screen-Shot-2013-12-27-at-4.10.47-PM

Click Finish to have a shiny new Memcached container created within seconds. Next click on the Container’s name or the magnifying glass next to it, to see the container’s details.

Screen-Shot-2013-12-27-at-4.19.09-PM

In the screen above you have all the details you need to make use of your Memcached.

We’ve gone ahead and modified the cache.py code that Digital Ocean provided. We now have the ability to pass the hostname as a parameter. Here’s the new code:

# Import python-memcache and sys for arguments
import memcache
import sys

# Get number of arguments
# Expected format: python cache.py [host] [memcached port] [key] [value]
len_argv = len(sys.argv)

# At least the port number and a key must be supplied
if len_argv < 4:
    sys.exit("Not enough arguments.")

# Port is supplied and a key is supplied - let's connect!
addr = sys.argv[1]
port  = sys.argv[2]
cache = memcache.Client(["{0}:{1}".format(addr, port)])

# Get the key
key   = str(sys.argv[3])

# If a value is also supplied, set the key-value pair
if len_argv == 5:

    value = str(sys.argv[4])
    cache.set(key, value)

    print "Value for {0} set!".format(key)

# If a value is not supplied, return the value for the key
else:

    value = cache.get(key)
    print "Value for {0} is {1}.".format(key, value)

And here’s how we test our Memcached is up and running:

$ python cache.py ce50fdc59b13.ares.tutum.io 49237 my_key my_value
Value for my_key set!

$ python cache.py ce50fdc59b13.ares.tutum.io 49237 my_key
Value for my_key is my_value.

Now, some of you may notice that this container can be accessible by anyone on the Internet. But fret not, as we will soon be announcing private networking capabilities for Tutum.

For more information on Docker, please check out the docker documentation at docker.io. For any questions or feedback about Tutum, please contact us directly, or find us on Twitter @tutumcloud

Borja is a co-founder and the CEO at Tutum. Borja holds a MSc in Information Security from Carnegie Mellon, a MSc in Applied Informatics from University of Hyogo, and a BSc in Computer Engineering from Georgia Tech. In his previous life he worked as a R&D engineer developing location based services, and later as a tech consultant for large Telecom providers around the World. Borja describes himself as a tech entrepreneur, hacker and DIYer. When not working on Tutum, Borja likes to tinker with hardware and build things.

Posted in Tutorial
One comment on “Docker Explained: How To Quickly Create Docker Containers Running Memcached
  1. Hurrah, that’s what I was exploring for, what a data!
    present here at this webpage, thanks admin of this website.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories
%d bloggers like this: