Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Use sockets for communication between 2 containers

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!

like image 970
Alex Fatyeev Avatar asked Jun 09 '16 13:06

Alex Fatyeev


People also ask

Can 2 Docker containers talk to each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

How do you communicate between containers in Docker?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

Can two Docker containers use the same port?

Surprisingly or not, neither Docker nor Podman support exposing multiple containers on the same host's port right out of the box. Example: docker-compose failing scenario with "Service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash."

How do two containers in the same pod communicate?

Multiple containers in the same Pod share the same IP address. They can communicate with each other by addressing localhost . For example, if a container in a Pod wants to reach another container in the same Pod on port 8080, it can use the address localhost:8080 .


1 Answers

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
like image 50
Matt Avatar answered Oct 18 '22 16:10

Matt