Docker Symfony Development Box with Data Containers

Data containers are containers where you define a path or folder to be mounted onto other containers. The data here can be read only or writeable. This means that for when you need a swap or temp storage for your projects you can use these containers since any other containers that are perhaps mounted on your host will not be writeable.

So we start by defining the cache and logs container as follows:

// cache_container
FROM busybox
RUN mkdir -p /var/cache/repay/dev
VOLUME /var/cache/repay
ENTRYPOINT chmod -R 777 /var/cache/repay; /usr/bin/tail -f /dev/null

And:

// logs_container
FROM busybox
RUN mkdir -p /var/log/repay/
VOLUME /var/log/repay
ENTRYPOINT chmod -R 777 /var/log/repay; /usr/bin/tail -f /dev/null

Here is how you piece everything together. You build those containers, notice that they have entry points so that after running that command gets invoked setting the right permissions for those folders when they get ran.

After the symfony box gets build you can run the whole thing and attach/mount the data containers to your main host shared container where the code is:

!#/usr/bin/env bash
 
# troubleshoot
# docker exec -i -t repay bash
 
# kill all previous containers
docker rm -f $(docker ps -a -q)
 
docker build --rm -t "cache_container:v1" docker/cache_container
docker build --rm -t "logs_container:v1" docker/logs_container
 
docker run -d --name repay_cache_container cache_container:v1
docker run -d --name repay_logs_container logs_container:v1
 
# build symfony project box
docker build -rm -t repay .
 
# run symfony container
docker run \
    -v $PWD:/srv \
    -e DB_NAME=repay \
    -e INIT=bin/reload \
    --volumes-from repay_cache_container:rw \
    --volumes-from repay_logs_container:rw \
    --name repay_development_webserver \
    -itP repay
</bash>
 
This can work thanks to adding in the kernel a couple of methods:
 
<pre lang="php">
    /**
     * {@inheritdoc}
     */
    public function getCacheDir()
    {
        return '/var/cache/repay/'.$this->environment;
    }
 
    /**
     * {@inheritdoc}
     */
    public function getLogDir()
    {
        return '/var/log/repay';
    }

I grabbed the top Dockerfile from an adaptation of ubermuda repo for symfony docker:

FROM debian:jessie
 
ENV DEBIAN_FRONTEND noninteractive
 
RUN apt-get update -y
RUN apt-get install -y nginx php5-fpm php5-mysqlnd php5-cli mysql-server supervisor
 
RUN sed -e 's/;daemonize = yes/daemonize = no/' -i /etc/php5/fpm/php-fpm.conf
RUN sed -e 's/;listen\.owner/listen.owner/' -i /etc/php5/fpm/pool.d/www.conf
RUN sed -e 's/;listen\.group/listen.group/' -i /etc/php5/fpm/pool.d/www.conf
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
 
ADD vhost.conf /etc/nginx/sites-available/default
ADD supervisor.conf /etc/supervisor/conf.d/supervisor.conf
ADD init.sh /init.sh
RUN chmod 777 /init.sh
 
EXPOSE 80
 
VOLUME ["/srv"]
WORKDIR /srv
 
CMD ["/usr/bin/supervisord"]

Now you can enjoy!