Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make multi-container connection with Docker of Nginx+PHP

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use Nginx+PHP Docker multi-container connection, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Docker provides access between multiple containers. The easiest way is to directly use the port mapping-p parameter to specify the mapped port or-P map all ports, and multiple containers are accessed directly through the network port.

However, network port mapping is not the only way to connect multiple containers in Docker. You can also use Docker's connection system (--link) to connect multiple containers. When the containers are connected together, the recipient container can see the information of the source container.

# # establish a connection between containers-take Nginx+PHP as an example

To establish a connection directly in the container, use the-- link option

-- link: alias

Here we demonstrate how to establish a connection between two or more containers by setting up a nginx/php-fpm service.

To establish a container connection, you must rely on the name of the container. Use-- name to specify the name of the source container as phpfpm

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 container to be connected is specified as phpfpm through the-- link option, and the volume mounted by the phpfpm container is also mounted to the nginx container using-- volumes-from phpfpm. In addition, the previous configuration is overwritten with a custom nginx configuration file (nginx.conf). The new nginx.conf content is as follows:

... root / app; # here sets the root directory of the container mounted by the project, location ~\ .php$ {fastcgi_pass phpfpm:9000;# phpfpm access address.

It should be noted that in this configuration file, the server's root directory (root) is set to / app directory, that is, we mount the directory, and it is the configuration of phpfpm, we change the value of fastcgi_pass from 127.0.0.1 fastcgi_pass 9000 to phpfpm:9000, where phpfpm is the domain name and is automatically configured as the access IP of the phpfpm container in the / etc/hosts file of the phpfpm container.

# # Container Interoperability Information

After the connection between the two containers is established, it is necessary to access the resources of the source container (Source) in the receiving container (Recipient). When we establish a connection for the container, the source container does not specify the port to be exposed with-p CPM P, so how to access the information of the source container?

To enable the receiving container to access the information of the source container, Docker provides two ways:

Environment variable

/ etc/hosts file

# Environment variables

When Docker connects the container, it automatically creates some environment variables in the receiver container according to the parameters provided by-- link, including the environment variables set by the ENV command in the Dockerfile of the source container and when the source container starts (docker run), using the environment variables specified by-e or-- env,-- env-file parameters.

It mainly contains the following environment variables, which assumes alias=webdb.

_ NAME_PORT___ADDR_PORT_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 above example, the alias of the container is specified as php, so all environment variables start with PHP_.

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 configured in / etc/hosts.

# / etc/hosts file

In addition to environment variables, Docker also updates the hosts information in the / etc/hosts file of the receiving container.

$docker run-I-t-- rm-- link phpfpm:php php:5.6-fpm / bin/bashroot@4678acd72dca:/var/www/html#root@4678acd72dca:/var/www/html# cat / etc/hosts172.17.0.77 4678acd72dca...172.17.0.74 php f81b2615a6a8 phpfpm

As you can see from above, two additional pieces of information have been added to the hosts file of the receiving container, the native IP and alias, and the IP and php of the source container.

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

The above is how to connect to multiple containers in Nginx+PHP 's Docker. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report