13

I have 2 Docker containers: App & Web.

App — simple container with php application code. It is used only for storage and deliver the code to the remote Docker host.

App image Dockerfile:

FROM debian:jessie
COPY . /var/www/app/
VOLUME ["/var/www/app"]
CMD ["true"]

Web — web service container, consist of PHP-FPM + Nginx.

Web image Dockerfile:

FROM nginx

# Remove default nginx configs.
RUN rm -f /etc/nginx/conf.d/*

# Install packages
RUN apt-get update && apt-get install -my \
  supervisor \
  curl \
  wget \
  php5-cli \
  php5-curl \
  php5-fpm \
  php5-gd \
  php5-memcached \
  php5-mysql \
  php5-mcrypt \
  php5-sqlite \
  php5-xdebug \
  php-apc

# Ensure that PHP5 FPM is run as root.
RUN sed -i "s/user = www-data/user = root/" /etc/php5/fpm/pool.d/www.conf
RUN sed -i "s/group = www-data/group = root/" /etc/php5/fpm/pool.d/www.conf

# Pass all docker environment
RUN sed -i '/^;clear_env = no/s/^;//' /etc/php5/fpm/pool.d/www.conf

# Add configuration files
COPY config/nginx.conf          /etc/nginx/
COPY config/default.vhost        /etc/nginx/conf.d
COPY config/supervisord.conf    /etc/supervisor/conf.d/
COPY config/php.ini             /etc/php5/fpm/conf.d/40-custom.ini

VOLUME ["/var/www", "/var/log"]

EXPOSE 80 443 9000

ENTRYPOINT ["/usr/bin/supervisord"]

My question: Is it possible to link Web container and App container by the socket?

The main reason for this - using App container for deploy updated code to remote Docker host. Using volumes/named volumes for share code between containers is not a good idea. But Sockets can help.

Thank you very much for your help and support!

Alex Fatyeev
  • 131
  • 1
  • 1
  • 4

1 Answers1

21

If both containers run on the same host, it's possible to share a socket between the two as they are plain files.

You can create a local docker volume and mount that volume on both containers. Then configure you program(s) to use that path.

docker volume create --name=phpfpm
docker run phpfpm:/var/phpfpm web
docker run phpfpm:/var/phpfpm app

If the socket can be generated on the host you can mount the file into both containers. This is the method used to get a docker container to control the hosts docker.

docker run -v /var/container/some.sock:/var/run/some.sock web
docker run -v /var/container/some.sock:/var/run/some.sock app
Matt
  • 68,711
  • 7
  • 155
  • 158
  • 1
    Matt, thank you very much for your answer! I did it. But I do not fully understand how Web container will have access to the files from App container by the soket located it the common volume. How the FPM from container Web knows that the files are in a specified folder in App container? Sorry for the my dumb question, but I'm new to data exchange through socket. And really want to understand this question. – Alex Fatyeev Jun 09 '16 at 16:55
  • Sharing a socket only allows communication between the two. So fpm listens on the socket, nginx sends requests to fpm via the socket. Any files for the web site will need to be in the web container. Any files for the php app will need to be in the app container. What are you trying to – Matt Jun 09 '16 at 23:41
  • Thank you for your help! I'm trying to make a site code in a separate container App. And link it with the nginx + fpm container Web. Without using volumes. i.e., Web — container _only_ with nginx + php-fpm. Without any site code. Only web-server and interpreter. App — container _only_ with site data: php,html,css,js. Only for delivery & store code. I need it to further deployment process: - rebuild image & container only with site code (App) - deliver it to Docker Host and change old App to new App. – Alex Fatyeev Jun 10 '16 at 10:10