美文网首页Docker容器
如何构建Memcached Docker容器

如何构建Memcached Docker容器

作者: 希云Docker容器管理平台 | 来源:发表于2018-02-22 10:25 被阅读1038次

摘要: 理论+实战,精彩详述“如何构建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.

docker的更多知识,请观看免费培训视频(http://csphere.cn/training)。

相关文章

  • 如何构建Memcached Docker容器

    摘要: 理论+实战,精彩详述“如何构建Memcached Docker容器” ! 如何把Memcached运行到d...

  • docker镜像

    Docker镜像是构建docker容器的基础,容器是docker镜像的运行的实例。构建镜像只用三条命令就可以搭建一...

  • Docker部署(二):MySQL数据库

    Docker 容器构建 使用Dockerfile构建镜像 使用官方提供的Docker镜像 所谓Dockerfile...

  • 101、镜像的构建

    1、镜像构建方式 1.1、docker commit 运行容器 对容器进行修改 通过docker commit 把...

  • Docker常用命令

    docker常用命令 进入容器 docker run -it xxxxx:1.1 /bin/bash 构建容器 d...

  • Docker容器日志清理

    docker容器导致主机磁盘空间满了 如何清理Docker容器日志? 如何找出docker容器日志文件 容器日志一...

  • Docker容器日志清理

    docker容器导致主机磁盘空间满了 如何清理Docker容器日志? 如何找出docker容器日志文件 容器日志一...

  • 使用docker构建nexus3私服

    使用 docker 构建 Nexus 3 Maven仓库 拉取Docker镜像 启动Docker容器

  • Docker深度学习1

    dockerfile 构建镜像docker run 启动容器docker-compose 启动服务 Docker ...

  • Docker

    [TOC] 使用 Docker 镜像 下载镜像 列出镜像 构建镜像 删除本地镜像 操作 Docker 容器 容器是...

网友评论

    本文标题:如何构建Memcached Docker容器

    本文链接:https://www.haomeiwen.com/subject/iirvgxtx.html