Docker multi-Container connection-taking nginx+php as an example

Source: Internet
Author: User
Tags docker run

Docker provides a method for direct access to multiple containers, the simplest way is to specify the mapped port directly using the port mapping -p parameter or -P to map all the ports, and multiple containers are accessed directly over the network port.

However, network port mapping is not the only way to connect multiple containers in Docker, and you can use Docker's connection system ( --link ) to connect multiple containers, and when a container is connected, the recipient container can see the information of the source container.

Establish a connection between containers-take nginx+php as an example

Create a connection directly in the container to use the --link option

--link <name or id>:alias

Here we create a NGINX/PHP-FPM service, which is an example of how to establish a connection between two or more containers.

To establish a container connection, you rely on the name of the container, using the name of the --name specified source containerphpfpm

docker run --name phpfpm -d -v /Users/mylxsw/codes/php:/app php:5.6-fpm

Next, create the Nginx container and connect to the PHPFPM container.

docker run --name nginx_server -d -p 80:80 --link phpfpm:phpfpm -v /Users/mylxsw/Dockers/php/nginx.conf:/etc/nginx/nginx.conf --volumes-from phpfpm  nginx

Here, the --link option specifies that the container to be connected is phpfpm, and that the volume attached to the --volumes-from phpfpm phpfpm container is attached to the Nginx container, and that the custom Nginx configuration file (nginx.conf) is used to overwrite the original configuration, and the new Nginx.conf content is as follows:

...root   /app; # 这里设置了项目挂载的容器的根目录location ~ \.php$ {    fastcgi_pass   phpfpm:9000;# phpfpm访问地址...

Note that in this configuration file set the root directory of the server (root) for the directory, that is, /app we mount the directory, the other is the PHPFPM configuration, we will fastcgi_pass change the value from 127.0.0.1:9000 phpfpm:9000 , here is the phpfpm domain name, in the Nginx container The /etc/hosts access IP for the PHPFPM container is automatically configured in the file.

Container Interoperability Information

After establishing a connection between two containers, it is necessary to access the source container's resources in the receiving container (Recipient), and when we make a connection to the container, the source container is created without using -p/-P the port specified to be exposed, so how can I access the information of the source container?

In order for the receiving container to be able to access the information of the source container, Docker provides two ways:

    • Environment variables
    • /etc/hosts file
Environment variables

When Docker joins a container, it --link automatically creates some environment variables in the recipient container based on the supplied parameters, including the environment variables set in the source container Dockerfile using the ENV command and the source container when it starts ( docker run ), -e or --env , --env-file parameter specifies the environment variable.

It mainly contains the following environment variables, which are assumed to be alias=**webdb**.

<alias>_NAME<name>_PORT_<port>_<protocol><prefix>_ADDR<prefix>_PORT<prefix>_PROTO

For example:

$ docker run  -i -t --rm --link phpfpm:php php:5.6-fpm envPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOSTNAME=e5973c0d639fTERM=xtermPHP_PORT=tcp://172.17.0.74:9000PHP_PORT_9000_TCP=tcp://172.17.0.74:9000PHP_PORT_9000_TCP_ADDR=172.17.0.74PHP_PORT_9000_TCP_PORT=9000PHP_PORT_9000_TCP_PROTO=tcpPHP_NAME=/tender_banach/phpPHP_ENV_PHP_INI_DIR=/usr/local/etc/phpPHP_ENV_GPG_KEYS=6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3 0BD78B5F97500D450838F95DFE857D9A90D90EC1PHP_ENV_PHP_VERSION=5.6.9PHP_INI_DIR=/usr/local/etc/phpPHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-dataGPG_KEYS=6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3 0BD78B5F97500D450838F95DFE857D9A90D90EC1PHP_VERSION=5.6.9HOME=/root

In the example above, the container alias is specified php , so all environment variables are PHP_ prefaced.

Note that if the source container is restarted, the environment variable information in the receiving container is not automatically updated, so if you want to use the IP address of the source container, use the host information that is configured in/etc/hosts.

/etc/hosts file

In addition to the environment variables, Docker /etc/hosts updates the hosts information in the file that receives the container.

$ docker run  -i -t --rm --link phpfpm:php php:5.6-fpm /bin/bash[email protected]:/var/www/html#[email protected]:/var/www/html# cat /etc/hosts172.17.0.77 4678acd72dca...172.17.0.74 php f81b2615a6a8 phpfpm

As can be seen from the hosts file in the receiving container, two additional messages, native IP and aliases, and the IP and Alias (PHP) of the source container are added.

Unlike environment variables, the information in the receiving container is updated automatically if the source container is restarted /etc/hosts .

Reference:

Linking Containers Together

How can I use environment variables in nginx.conf

Docker multi-Container connection-taking nginx+php as an example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.