如何構建Memcached Docker容器

 如何把Memcached運行到docker容器中?python

Dockernginx

Docker爲容器(應用程序)提供運行環境。使用Docker鏡像建立容器,既能夠經過人工執行命令,也能夠經過cSphere平臺可視化操做。
web

 Memcached簡介docker

Memcached是一個分佈式,開源的數據存儲引擎。它被設計用來在RAM(替換了低速的傳統硬盤)中存儲特定種類的數據,供應用程序進行快速檢索。減小了處理申請所花費的時間,經過減小查詢的次數來抵消沉重緩慢的數據集或者API,好比傳統的數據庫(MySQL等)。數據庫

經過引進一個靈巧的,精心設計並通過最優化的緩存機制,它變得能夠處理更大的請求量,執行更多的程序。這是Memcached最重要的應用實例,由於它也是這樣緩存其餘應用或內容的。ubuntu

能夠深度依賴,並被用在網站或者其餘應用的生產中,Memcached已經成爲一個即時提高性能的工具,而沒必要使用更好的硬件條件(好比更多的服務器或者服務資源)。緩存

Memcached的工做方式是將關鍵詞和他們對應的值(最大能達到1MB)保存在一個關聯矩陣中(好比哈希表),延展和分佈在大量的虛擬服務器中。服務器

開始建立Memcached鏡像編輯器

基於咱們以前學習的Docker系列文章裏面的知識,咱們直接深刻到建立Dockerfile來實現自動構建安裝Mamcached功能的鏡像(將能夠用來運行沙盒化的Memcached實例)。分佈式

快速回顧:什麼是Dockerfile?
Dockerfile是包含可執行的聲明的命令的腳本,將以給定的順序執行,來讓Docker自動的建立一個新的Docker鏡像。這給部署工做帶來了極大的幫助。

這些文件(Dockerfile)使用FROM命令,老是以對基礎鏡像的描述開頭。從那開始,構建進程開始運行,向主機提交(保存鏡像的狀態)的每一步的操做造成了最終的鏡像。

用法:

 # Build an image using the Dockerfile at current location
 # Tag the final image with [name] (e.g. *nginx*)
 # Example: sudo docker build -t [name] .
 sudo docker build -t memcached_img .

建立Memcached鏡像的Dockerfile

經過熟悉文本編輯器,建立一個新的Dockerfile:

首先讓咱們定義一下Dockerfile的目標,並聲明須要使用的基礎鏡像。

 ############################################################
 # 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 cSphere

而後咱們就能夠開始安裝Memcached

 
 # Install Memcached
 RUN apt-get install -y memcached

設置默認對外開放的容器端口:

 # Port to expose (default: 11211)
 EXPOSE 11211

設置默認的執行命令和入口(例如Memcached進程):

 # Set the user to run Memcached daemon
 USER daemon
 
 # Set the entrypoint to memcached binary
 ENTRYPOINT memcached

 # Default Memcached run command arguments
 CMD ["-u", "root", "-m", "128"]

### 最終的Dockfile

 ############################################################
 # 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
 
 # Install Memcached
 RUN apt-get install -y memcached
 
 # Port to expose (default: 11211)
 EXPOSE 11211
 
 # Set the user to run Memcached daemon
 USER daemon
 
 # Set the entrypoint to memcached binary
 ENTRYPOINT memcached

    # Default Memcached run command arguments
 CMD ["-m", "128"]

    Dockerfile準備完畢!

建立Memcached容器

構建memcached鏡像:「csphere-memcached」

 sudo docker build -t  csphere-memcached.

**Note**:不要遺漏了最後的「 .」 ,Docker須要它來找到Dockerfile。

啓動memcached容器

使用下面的命令來建立一個新容器,能夠根據你的需求修改這個例子。

 # sudo docker run -name csphere-memcached -d -p 45001:11211 csphere-memcached

「csphere-memcached」容器,已啓動,可以使用45001端口鏈接使用。

限制Memcached容器的內存

若是想要限制一個Docker容器進程可使用的內存量,只要設置`-m [memory amount]`並標上限制就ok。

運行一個內存限制爲256MB的容器:

   ` # sudo docker run -name csphere-memcached -m 256m -d -p 45001:11211 csphere-memcached`

檢查此容器內存限制是否設置成功,執行如下命令:

 `# Example: docker inspect [container ID] | grep Memory
sudo docker inspect csphere-memcached | grep Memory`

測試Memcached容器

咱們使用一個簡單的Python CLI程序來測試。

確保你的主機擁有爲Python/Memcached準備的必要庫文件:

   ` sudo apt-get update && sudo apt-get -y upgrade
 sudo apt-get install -y python-pip
 pip install python-memcached`

建立一個簡單的Python腳本,名爲cache.py

把下面的內容複製粘貼進去:

 ` # Import python-memcache and sys for arguments
 import memcache
 import sys
 
 # Set address to access the Memcached instance
 addr = 'localhost'
 
 # Get number of arguments
 # Expected format: python cache.py [memcached port] [key] [value]
 len_argv = len(sys.argv)
 
 # At least the port number and a key must be supplied
 if len_argv < 3:
     sys.exit("Not enough arguments.")
 
 # Port is supplied and a key is supplied - let's connect!
 port  = sys.argv[1]
 cache = memcache.Client(["{0}:{1}".format(addr, port)])
 
 # Get the key
 key   = str(sys.argv[2])
 
 # If a value is also supplied, set the key-value pair
 if len_argv == 4:

     value = str(sys.argv[3])
     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)`
  

      測試Docker的Memcached實例:
    
   # Example: python cache.py [port] [key] [value]
 python cache.py 45001 my_test_key test_value
 
 # Return: Value for my_test_key set
 
 # See if the key is set:
 python cache.py 45001 my_test_key
 
 # Return: Value for my_test_key is test_value.

docker的更多知識,請觀看免費培訓視頻(http://csphere.cn/training)。

相關文章
相關標籤/搜索