Linking to a Docker memcached container

2019-02-24 01:07发布

I have been experimenting with Docker for a few days now and have grown to like it. 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

//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

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

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

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

docker run -d -P --name memcached droidos/memcached

Fireup a docker container with apache and link it to the memcached container created earlier

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

Everything seems ok

Create a memcached test script in /var/droidos/site

<?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.

Either my linking is incorrect or memcached has not been started or the memcached container port is not visible in the apache container. SSHing into the memcached container

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. 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.

1条回答
再贱就再见
2楼-- · 2019-02-24 01:32

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.

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).

Therefore you should be able to connect from the apache container to the memcached one using this PHP command:

$mc->addServer("memcached", 11211);

If it doesn't work, check that you can connect to the memcached service from the memcached container itself.

查看更多
登录 后发表回答