如何构建Memcached Docker容器

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:
+关注继续查看

如何把Memcached运行到docker容器中?

image

Docker

Docker为容器(应用程序)提供运行环境。使用Docker镜像创建容器,既可以通过人工执行命令,也可以通过cSphere平台可视化操作。

Memcached简介

Memcached是一个分布式,开源的数据存储引擎。它被设计用来在RAM(替换了低速的传统硬盘)中存储特定种类的数据,供应用程序进行快速检索。减少了处理申请所花费的时间,通过减少查询的次数来抵消沉重缓慢的数据集或者API,比如传统的数据库(MySQL等)。

通过引进一个灵巧的,精心设计并经过最优化的缓存机制,它变得可以处理更大的请求量,执行更多的程序。这是Memcached最重要的应用实例,因为它也是这样缓存其他应用或内容的。

可以深度依赖,并被用在网站或者其他应用的生产中,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.

文章转载自 开源中国社区[https://www.oschina.net]

相关文章
|
1天前
|
运维 Docker 容器
docker容器运维操作命令
docker容器运维操作命令
9 0
|
1天前
|
应用服务中间件 nginx Docker
将 react-typescript + django 部署到 nginx 容器(docker)
将 react-typescript + django 部署到 nginx 容器(docker)
37 0
|
1天前
|
Linux 网络安全 Docker
windows ssh连接docker 容器
windows ssh连接docker 容器
10 0
|
1天前
|
分布式计算 分布式数据库 Docker
docker 构建 hbase 容器
docker 构建 hbase 容器
17 1
|
1天前
|
应用服务中间件 nginx Docker
docker为容器分配静态ip
docker为容器分配静态ip
11 0
|
1天前
|
网络安全 数据安全/隐私保护 网络虚拟化
完整模型容器的使用,迁移tensorflow2 实现的 yolo3 到docker容器
完整模型容器的使用,迁移tensorflow2 实现的 yolo3 到docker容器
15 1
|
1天前
|
Linux TensorFlow 算法框架/工具
把物体检测模型迁移到docker容器
把物体检测模型迁移到docker容器
13 1
|
11天前
|
关系型数据库 MySQL 应用服务中间件
【Docker 系列】docker 学习 五,容器数据卷
【Docker 系列】docker 学习 五,容器数据卷
|
11天前
|
Ubuntu 关系型数据库 MySQL
【Docker 系列】docker 学习六,数据卷容器
【Docker 系列】docker 学习六,数据卷容器
|
16天前
|
边缘计算 持续交付 数据中心
Docker在边缘计算中的崭露头角:探索容器技术如何驱动边缘计算的新浪潮
边缘计算是一项快速发展的技术,它旨在将计算能力更接近数据源和终端用户,以提供低延迟、高性能的计算体验。在这个充满活力的领域,Docker容器技术崭露头角,成为推动边缘计算革新的一股新力量。本文将深入探讨Docker在边缘计算中的应用,介绍其优势和挑战,并穿插一些示例代码,以帮助读者更好地理解这一新兴趋势。 第一部分:边缘计算和Docker容器 边缘计算的定义 边缘计算是一种计算范式,它将计算资源和数据处理能力推向网络边缘,靠近数据源和终端用户。这与传统的集中式云计算模型形成鲜明对比,后者将大部分计算任务集中在中央数据中心。边缘计算的关键目标是减少数据传输的延迟,提高响应速度,以满足对实时性要
相关产品
容器镜像服务
容器服务Kubernetes版
推荐文章
更多