千家信息网

怎么构建Memcached Docker容器

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容主要讲解"怎么构建Memcached Docker容器",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么构建Memcached Docker容器"
千家信息网最后更新 2026年11月01日怎么构建Memcached Docker容器

本篇内容主要讲解"怎么构建Memcached Docker容器",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么构建Memcached Docker容器"吧!

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

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.

到此,相信大家对"怎么构建Memcached Docker容器"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

容器 镜像 命令 应用 运行 限制 内存 内容 数据 程序 实例 更多 进程 学习 服务 测试 主机 传统 可以通过 基础 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 咖啡网 云南咖啡 亿人帮互联网科技有限公司 互联网科技工作委员会主任 互联网属科技 互联网科技有限公司图片 深圳互联网络科技有限公司 狮子座互联网科技 贷财互联网科技 中盈科技互联网信息有限公司 中国的互联网科技企业的崛起 苏州铭阳互联网科技有限公司 速维有品互联网信息科技(苏州)有限公司 互联网媒体平台算高科技产业 互联网科技研发水平 青海科技产业互联网发展 洛阳共同富裕互联网科技有限公司 上海钢联属于互联网科技股吗 马化腾互联网科技教学 青岛互联网科技园 博彦科技互联网金融 以房互联网科技 互联网科技视频素材 开启互联网科技时代小说 须眉科技是互联网公司吗 济南有互联网科技公司吗 广东工业互联网大赛深圳学泰科技 广州百诚互联网科技有限公司官网 萌小萌互联网科技河南有限公司 东风日产互联网科技公司叫什么
0