[英]Linking to a Docker memcached container
I have been experimenting with Docker for a few days now and have grown to like it. 我已经尝试了几天Docker并且已经成长为喜欢它。 However, there are a few things that still elude me. 但是,有一些事情仍然无法实现。 Here is what I have thus far 这是我到目前为止所拥有的
Create a low footprint Ubuntu 14.04 image 创建一个低占用空间的Ubuntu 14.04映像
//I got this from a post on this forum
#!/bin/bash
docker rm ubuntu-essential-multilayer 2>/dev/null
set -ve
docker build -t textlab/ubuntu-essential-multilayer - <<'EOF'
FROM ubuntu:14.04
# Make an exception for apt: it gets deselected, even though it probably shouldn't.
RUN dpkg --clear-selections && echo apt install |dpkg --set-selections && \
SUDO_FORCE_REMOVE=yes DEBIAN_FRONTEND=noninteractive apt-get --purge -y dselect-upgrade && \
dpkg-query -Wf '${db:Status-Abbrev}\t${binary:Package}\n' |grep '^.i' |awk -F'\t' '{print $2 " install"}' |dpkg --set-selections && \
rm -r /var/cache/apt /var/lib/apt/lists
EOF
TMP_FILE="`mktemp -t ubuntu-essential-XXXXXXX.tar.gz`"
docker run --rm -i textlab/ubuntu-essential-multilayer tar zpc --exclude=/etc/hostname \
--exclude=/etc/resolv.conf --exclude=/etc/hosts --one-file-system / >"$TMP_FILE"
docker rmi textlab/ubuntu-essential-multilayer
docker import - textlab/ubuntu-essential-nocmd <"$TMP_FILE"
docker build -t textlab/ubuntu-essential - <<'EOF'
FROM textlab/ubuntu-essential-nocmd
CMD ["/bin/bash"]
EOF
docker rmi textlab/ubuntu-essential-nocmd
rm -f "$TMP_FILE"
Create a Dockerfile for an Apache image 为Apache映像创建Dockerfile
FROM textlab/ubuntu-essential
RUN apt-get update && apt-get -y install apache2 && apt-get clean
RUN a2enmod ssl
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
EXPOSE 443
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
docker build -t droidos/apache .
Create a Dockerfile for PHP5 为PHP5创建一个Dockerfile
FROM droidos/apache
RUN apt-get update && apt-get -y --reinstall install php5 php5-redis php5-memcached php5-curl libssh2-php php5-mysqlnd php5-mcrypt && apt-get clean
RUN php5enmod mcrypt
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
EXPOSE 443
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
docker build -t droidos/php5 .
Create a Dockerfile for memcached and build the image 为memcached创建一个Dockerfile并构建映像
FROM textlab/ubuntu-essential
# Install packages
RUN DEBIAN_FRONTEND=noninteractive apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install memcached
# memcached public variable
EXPOSE 11211
CMD ["/usr/bin/memcached", "-u", "memcache", "-v"]
docker build -t droidos/memcached .
Fireup a docker container with memcached Fireup与memcached的码头工人容器
docker run -d -P --name memcached droidos/memcached
Fireup a docker container with apache and link it to the memcached container created earlier 使用apache启动一个docker容器并将其链接到之前创建的memcached容器
docker run -d --name apache --link memcached:memcached -v /var/droidos/site:/var/www/html -v /var/droidos/logs:/var/log/apache2 -p 8080:80 droidos/php5
Browse to example.com:8080 浏览到example.com:8080
Everything seems ok 一切似乎都好
Create a memcached test script in /var/droidos/site 在/ var / droidos / site中创建memcached测试脚本
<?php
error_reporting(E_ALL);
header('Content-type:text/plain');
$mc = new Memcached();
$mc->addServer("localhost", 11211);
$flag = $mc->add('name','droidos');
echo ($flag)?'y':'n';
echo $mc->getResultCode();
?>
This script returns n47 implying that the memcached server is disabled. 此脚本返回n47,表示已禁用memcached服务器。
Either my linking is incorrect or memcached has not been started or the memcached container port is not visible in the apache container. 我的链接不正确或者memcached尚未启动或者memcached容器端口在apache容器中不可见。 SSHing into the memcached container SSH进入memcached容器
docker exec -it <container-id> /bin/bash
and running 并运行
service memcached status
indicates that the service is not in fact running. 表示该服务实际上并未运行。 So I start it 所以我开始吧
service memcached start
verify it has started and run the script above again. 验证它已启动并再次运行上面的脚本。 No joy - I still get an n47 reply rather than the y0 I would like to see. 没有快乐 - 我仍然得到一个n47回复而不是我希望看到的y0。 Clearly, I am missing a step somewhere here. 显然,我在这里错过了一步。 I'd be most obliged to anyone who might be able to tell me what that might be. 对于任何可能告诉我这可能是什么的人,我最有责任。
I think it fails because you're trying to access memcached from the apache container connecting to the localhost of the apache container, while the memcached container is made accessible to the apache one on a different IP address. 我认为它失败了,因为你试图从连接到apache容器的localhost的apache容器访问memcached,而memcached容器可以访问不同IP地址上的apache。
This is the line I think is wrong: 这是我认为错误的路线:
$mc->addServer("localhost", 11211);
When you link containers, Docker adds a host entry for the source container to the /etc/hosts
file (see the docs about linking ). 链接容器时,Docker会将源容器的主机条目添加到/etc/hosts
文件中(请参阅有关链接的文档 )。
Therefore you should be able to connect from the apache container to the memcached one using this PHP command: 因此,您应该能够使用此PHP命令从apache容器连接到memcached容器:
$mc->addServer("memcached", 11211);
If it doesn't work, check that you can connect to the memcached service from the memcached container itself. 如果它不起作用,请检查您是否可以从memcached容器本身连接到memcached服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.