建立dockerfile,本文將dockerfile命名爲Dockerfile_memcached(文件名可隨意定義):python
# 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
$ docker build -t memecached - < Dockerfile_memcached
使用構建的鏡像啓動容器:shell
$ docker run -d -name memcached_service memcached
根據鏡像啓動一個shell交互方式的容器,鏈接memcached服務,ubuntu_with_pip是我預先安裝了pip的一個鏡像:ubuntu
$ docker run -i -t -link memcached_service:db ubuntu_with_pip:12.04 /bin/bash
查看shell交互容器的環境變量:
bash
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測試是否可以正常使用:tcp
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