使用官方的php docker镜像并在启动时运行memcached

时间:2016-11-28 23:31:24

标签: php docker debian memcached boot

我有一个基于php:5.5.36-apache图像的docker文件,用于为开发创建图像。我的Dockerfile安装了memcached,但我没有运气获得memcached来启动。如果我ssh到容器中并手动启动memcached,它就会正常启动。

FROM php:5.5.36-apache

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    default-jdk 
RUN apt-get install -y --no-install-recommends autoconf    
RUN apt-get install -y --no-install-recommends python python-pip

RUN apt-get update && apt-get install -y libmemcached-dev \
    apt-utils re2c g++ memcached \
    zlib1g zlib1g-dbg zlib1g-dev zlibc mysql-client php5-mysql \
    && pecl install memcached \
    && docker-php-ext-enable memcached\
    && pecl install memcache \
    && docker-php-ext-enable memcache

RUN docker-php-ext-install pdo pdo_mysql

RUN apt-get install -y gettext

RUN pip install hgapi

RUN a2enmod headers \
    && a2enmod rewrite

COPY ./apache2.conf /etc/apache2/apache2.conf

RUN mkdir /var/www/content

EXPOSE 11211

RUN systemctl enable memcached.service

基本图像基于debian:jessie

2 个答案:

答案 0 :(得分:1)

php:5.5.36-apache有一个名为apache2-foreground的bash脚本,它使用exec启动apache,该脚本在Dockerfile末尾用CMD ["apache2-foreground"]调用。这是Docker在启动时执行的一个脚本,exec命令将执行传递给系统。

我的解决方案,我非常优雅,我不建议在任何类型的生产服务器上执行此操作是复制apache2-foreground脚本并在启动apache之前启动memcached。由于这是一个用作本地开发服务器的映像,因此符合我的需求。

更新后的apache2-foreground:     #!/斌/庆典     设置-e

# Apache gets grumpy about PID files pre-existing
rm -f /var/run/apache2/apache2.pid

/etc/init.d/memcached start
exec apache2 -DFOREGROUND

然后我换了:

RUN systemctl enable memcached.service

使用:

COPY apache2-foreground /usr/local/bin/

答案 1 :(得分:1)

此链接适用于基于ubuntu的基于Dockerfile的php5,apache2和memcached的相同问题:

forum post where someone says to use it

安装主管

RUN apt-get install -y supervisor

并在Dockerfile中配置:

RUN touch  /etc/supervisor/conf.d/supervisord.conf && \
    echo "[supervisord]" >> /etc/supervisor/conf.d/supervisord.conf && \
    echo "nodaemon=true" >> /etc/supervisor/conf.d/supervisord.conf

RUN touch /etc/supervisor/conf.d/memcached.conf && \
    echo "[program:memcache]" >> /etc/supervisor/conf.d/memcached.conf && \
    echo "command=/usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -DFOREGROUND" >> /etc/supervisor/conf.d/memcached.conf && \
    echo "autostart=true" >> /etc/supervisor/conf.d/memcached.conf && \
    echo "autorestart=true" >> /etc/supervisor/conf.d/memcached.conf

RUN touch /etc/supervisor/conf.d/apache2.conf && \
    echo "[program:apache2]" >> /etc/supervisor/conf.d/apache2.conf && \
    echo 'command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"' >> /etc/supervisor/conf.d/apache2.conf && \
    echo "autostart=true" >> /etc/supervisor/conf.d/apache2.conf && \
    echo "autorestart=true" >> /etc/supervisor/conf.d/apache2.conf

CMD ["/usr/bin/supervisord"]

此链接还说明了如何在容器中运行多个服务:

https://github.com/moby/moby/issues/5137