创建dockerfile,本文将dockerfile命名为Dockerfile_memcached(文件名可随意定义):
# Memcached
#
# VERSION 2.2
FROM ubuntu:12.04
MAINTAINER Marker.King <majk@vip.qq.com>
# make sure the package repository is up to date
RUN echo "deb http://mirrors.aliyun.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
# install memcached
RUN apt-get install -y memcached
# Launch memcached when launching the container
ENTRYPOINT ["memcached"]
# run memcached as the daemon user
USER daemon
# expose memcached port
EXPOSE 11211
根据dockerfile构建镜像:
$ docker build -t memecached - < Dockerfile_memcached
使用构建的镜像启动容器:
$ docker run -d -name memcached_service memcached
根据镜像启动一个shell交互方式的容器,连接memcached服务,ubuntu_with_pip是我预先安装了pip的一个镜像:
$ docker run -i -t -link memcached_service:db ubuntu_with_pip:12.04 /bin/bash
查看shell交互容器的环境变量:
root@665f1bdc5913:/# env
HOSTNAME=665f1bdc5913
DB_NAME=/compassionate_pasteur/db
TERM=xterm
DB_PORT=tcp://172.17.0.2:11211
DB_PORT_11211_TCP_PROTO=tcp
DB_PORT_11211_TCP_ADDR=172.17.0.2
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
SHLVL=1
HOME=/
DB_PORT_11211_TCP_PORT=11211
DB_PORT_11211_TCP=tcp://172.17.0.2:11211
container=lxc
_=/usr/bin/env
安装python-memcached,并通过python测试是否能够正常使用:
root@665f1bdc5913:/# pip install python-memcached
root@665f1bdc5913:/# python
>>> import memcache
>>> mc = memcache.Client(["172.17.0.2:11211"], debug=0)
>>> mc.set("MDM", "Marker.King")
True
>>> value = mc.get("MDM")
>>> print(value)
Marker.King