Home » Categories » Linux Cloud Server » UBuntu |
Docker Explained: How To Create Docker Containers Running Memcached |
Article Number: 259 | Rating: Unrated | Last Updated: Sat, Jan 4, 2014 at 1:16 AM
|
Docker in BriefThe docker project offers higher-level tools, working together, which are built on top of some Linux kernel features. The goal is to help developers and system administrators port applications - with all of their dependencies conjointly - and get them running across systems and machines -headache free. Docker achieves this by creating safe, LXC (i.e. Linux Containers) based environments for applications called docker containers. These containers are created using docker images, which can be built either by executing commands manually or automatically through Dockerfiles. Memcached in BriefMemcached 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 server, 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 UbuntuUpdate:
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 DEFAULT_FORWARD_POLICY. Replace:
With:
Press CTRL+X and approve with Y to save and close. Finally, reload the UFW:
Basic Docker CommandsRunning 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 ListHere is a summary of currently available (version 0.7.1) docker commands: attachAttach to a running container buildBuild a container from a Dockerfile commitCreate a new image from a container's changes cpCopy files/folders from the containers filesystem to the host path diffInspect changes on a container's filesystem eventsGet real time events from the server exportStream the contents of a container as a tar archive historyShow the history of an image imagesList images importCreate a new filesystem image from the contents of a tarball infoDisplay system-wide information insertInsert a file in an image inspectReturn low-level information on a container killKill a running container loadLoad an image from a tar archive loginRegister or Login to the docker registry server logsFetch the logs of a container portLookup the public-facing port which is NAT-ed to PRIVATE_PORT psList containers pullPull an image or a repository from the docker registry server pushPush an image or a repository to the docker registry server restartRestart a running container rmRemove one or more containers rmiRemove one or more images runRun a command in a new container saveSave an image to a tar archive searchSearch for an image in the docker index startStart a stopped container stopStop a running container tagTag an image into a repository topLookup the running processes of a container versionShow the docker version information Getting Started with Creating Memcached ImagesBuilding 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, thebuild processstarts and each following action taken forms the final with commits (saving the image state) on the host. Usage:
Dockerfile Commands OverviewAddCopy a file from the host into the container CMDSet default commands to be executed, or passed to the ENTRYPOINT ENTRYPOINTSet the default entrypoint application inside the container ENVSet environment variable (e.g. "key = value") EXPOSEExpose a port to outside FROMSet the base image to use MAINTAINERSet the author / owner data of the Dockerfile RUNRun a command and commit the ending result (container) image USERSet the user to run the containers from the image VOLUMEMount a directory from the host to the container WORKDIRSet the directory for the directives of CMD to be executed Creating a DockerfileSince 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 ContainersWe 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 Running dockerised Memcached ContainersIt 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 Creating a Memcached Installed ContainerTo 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 "memcached_ins", accessible from port 45001, Limiting the Memory for a Memcached ContainerIn order to limit the amount of memory a docker container process can use, simply set the 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 Testing the Memcached ContainerThere 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 "mc.py" 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:
|
Attachments
There are no attachments for this article.
|
How To Install and Secure phpMyAdmin on Ubuntu 12.04
Viewed 2679 times since Mon, Dec 23, 2013
How To Scale Django: Finding the Bottleneck
Viewed 2477 times since Fri, Jan 3, 2014
Initial Server Setup with Ubuntu 12.04
Viewed 2443 times since Mon, Dec 23, 2013
How To Use ps, kill, and nice to Manage Processes in Linux
Viewed 2718 times since Thu, Dec 26, 2013
How To Launch Your Site on a New Ubuntu 12.04 Server with LAMP, SFTP, and DNS
Viewed 2560 times since Thu, Dec 26, 2013
How To Set Up mod_security with Apache on Debian/Ubuntu
Viewed 4062 times since Thu, Dec 26, 2013
How To Setup uWSGI On Ubuntu 12.10
Viewed 2155 times since Sat, Jan 4, 2014
How To Add and Delete Users on Ubuntu 12.04 and CentOS 6
Viewed 2825 times since Mon, Dec 23, 2013
How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu
Viewed 6628 times since Thu, Jan 2, 2014
How To Install and Use Docker: Getting Started
Viewed 3825 times since Sat, Jan 4, 2014
|