[Problem] Docker external_links

Hello,

Can your help me for this works with 2 stack under Portainer .2.15.1

Stack 1: Database

################
version: '3.8' #
services:      #
################
#
#########################################################################
 MariaDB:                                                               #
  image: 'mysql'                                                        #
  network_mode: 'bridge'                                                #
  container_name: 'CN_MYSQL'                                            #
  # ------------------------------------------------------------------- #
  command: --default-authentication-plugin=mysql_native_password        #
  # ------------------------------------------------------------------- #
   environment:                                                         #
     MYSQL_ROOT_PASSWORD: 'root'                                        #
     MYSQL_DATABASE: 'database'                                         #
     MYSQL_USER: 'admin'                                                #
     MYSQL_PASSWORD: 'admin'                                            #
     MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'                                  #
#########################################################################

Stack 2: Administration

################
version: '3.8' #
services:      #
################
#
#
#################################################
 phpmyadmin:                                    #
  # ------------------------------------------- #
  image: 'phpmyadmin/phpmyadmin'                #
  container_name: 'CN_phpmyadmin'               #
  network_mode: 'bridge'                        #
  # ------------------------------------------- #
  external_links:                               #
   - 'CN_MYSQL:db'                              #
  # ------------------------------------------- #
  environment:                                  #
   PMA_ARBITRARY: '0'                           #
   PMA_HOST: 'db'                               #
   PMA_PORT: '3306'                             #
   PMA_USER: 'root'                             #
   PMA_PASSWORD: 'root'                         #
   PMA_PMADB: 'phpmyadmin'                      #
   HIDE_PHP_VERSION: 'true'                     #
  # ------------------------------------------- #
  ports:                                        #
   - '1005:80'                                  #
#################################################

Abend :

mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known

 mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known

Portainer Infos

Portainer Community Edition
Server Version: 2.15.1
Database Version: 61
CI Build Number: 22936
Image Tag: linux-amd64-2.15.1
Compilation tools:
Nodejs v14.20.0
Yarn v1.22.19
Webpack v5.68.0
Go v1.18.3

Each docker compose file creates it’s own network. It is considered a user definied network and allows dns-based service discovery for service that share the same network.

You used two different compose file (=different compose projects), which do not share a network. Instead, you try to use the deprecated external_links feature, which to my knowledge must be on the consuming service and not on the consumed service (I don’t recall EVER using it for compose project deployments in the last 9 years). Thus said: I do not recommend using this approach at all, as it will be removed at one point.

Instead, either put both services in the same compose file OR create a network using docker network create and then declare it as external network in both compose files and then attach that network to each of the services and redeploy your compose project(s).

Maybe I miss something, but for me, it looks like the link is defined in the right service. PHPAdmin wants to access the database.

Neither do I :slight_smile:

This is so true, the documentation mentions that the externally linked services “must be connected to at least one of the same networks”, which makes me wonder what external_links was for, since you can access containers on the same network even without external links. (Update: it could be for using aliases)

Note

The externally-created containers must be connected to at least one of the same networks as the service that is linking to them. Links are a legacy option. We recommend using networks instead.

Good catch! I thought I saw it declared on consumed service.

external_links are prehistoric and are only required and meaningful in the default bridge network, as it lacks dns-based service disovery. As a result the linked container’s ip is injected into the consuming containers /etc/hosts file with the container name, and it’s alias.

When a user defined network is used, it allows setting an alias for a specific service in a specific network as well.

I see no value in using external_links at all.

Each container is connected to the Bridge network which means that they are on the same basic network

Indeed, this line seem to attach the containers to the default bridge:

I just tested your compose files and after fixing your db compose file, it works like a charme. The db compose file has no service declaration. The way you shared it, it never could have worked.

Note: I also tried it on my NAS and it didn’t work, which is caused by the php random number generator inside the container that requires a kernel feature that is not available on the NAS’s 3.10 kernel. The phpadmin container died and kept on restarting.

Anyway, I stand by my word: using external links is deprected and will be eventually removed. There is zero value in using it.

Hello,

I found a problem, just create a new network class C and change Bridge.

When a code in a one stack, the bridge not work.

Bidge

Network details
Name		    bridge
Id		        ***************
Driver		    bridge
Scope	     	local
Attachable  	false
Internal	    false
IPV4 Subnet 	172.17.0.0/16	
IPV4 Gateway 	172.17.0.1
IPV4 IP Range	IPV4 Excluded IPs

com.docker.network.bridge.default_bridge	    true
com.docker.network.bridge.enable_icc		    true
com.docker.network.bridge.enable_ip_masquerade	true
com.docker.network.bridge.host_binding_ipv4 	0.0.0.0
com.docker.network.bridge.name			        docker0
com.docker.network.driver.mtu			        1500