Docker conect mysql in another Docker-Compose

Asked

Viewed 172 times

0

I have two Docker-Compose.yml

in the first is defined my database:

# Docker-compose com banco mysql e postgress junto com phpmyadmin e PGADMIN
version: '3'
services:

# MYSQL
  db:
    image: mysql
    container_name: db_mysql
    command: --default-authentication-plugin=mysql_native_password
    environment: 
      MYSQL_ROOT_PASSWORD: "admin"
    ports:
      - 3306:3306
    networks: 
      - mysql_net
  adminer:
    image: adminer
    restart: always
    ports:
      - 8081:8080
    networks: 
      - mysql_net


# POSTGRES
  postgres:
    image: postgres
    container_name: postgres
    environment: 
      POSTGRES_PASSWORD: admin
    ports:
      - "5432:5432"
  pgadmin4:
    image: dpage/pgadmin4
    container_name: pgadmin4
    environment: 
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: admin
    ports:
      - "15432:80"
networks: 
  mysql_net:
    driver: bridge

In my second Docker-Compose is set a small application in PHP.

version: '3'

services:
  nginx:
    image: richarvey/nginx-php-fpm
    ports: 
      - "8080:80"
    container_name: nginx
    volumes:
        - ./code:/var/www/html
    networks: 
      - mysql_net
networks:
  mysql_net:
    external: true

The problem is that my PHP application is not able to see the mysql database, always returns the error

Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name does not resolve in /var/www/html/cap1/mysql_lista.php on line 4

My connection string looks like this: $conn = mysqli_connect("db_mysql", 'root', 'admin', 'livros');

If I create the mysql container inside the Docker-Compose that my PHP is running it works, but if I try to separate it already from the error.

I also tried to connect using the mysql Docker IP and also did not roll.

I’m not a deep connoisseur of Docker and it’s taking my sleep away.

I’d appreciate it if someone could help.

  • in the application’s Docker-Compose tries to define network as instead of mysql_net for db_mysql_net

  • ERROR: Network db_mysql_net declared as External, but could not be found. Please create the network Manually using docker network create db_mysql_net and Try Again.

2 answers

0

You can try two approaches that might work:

  1. Create an external network for everyone to access. It seems that’s what you did when, in the second file, you declared

    networks:
       mysql_net:
         external: true
    

So you probably already have a network created with that name before running Docker-Compose up. If you haven’t created one yet, you can create one with

    docker network create mysql_net

More about network creation here. You can also set an IP of your preference when creating the external network.

Also add "Xternal: true" to your first database Docker-Compose. The way it is, it creates the network only for internal use of those who are in that file only. When you set the network to external, it will know that other services will be able to access containers within the Compose.

  1. The name of your DB host is the name of the Docker-Compose service you created, that is, in the case it is "db", not the container name that is "db_mysql". Perhaps that amendment will resolve.

0

Try it with this:

version: '3.5'
services:

# MYSQL
  db:
    image: mysql
    container_name: db_mysql
    command: --default-authentication-plugin=mysql_native_password
    environment: 
      MYSQL_ROOT_PASSWORD: "admin"
    ports:
      - 3306:3306
    networks: 
      - mysql_net
  adminer:
    image: adminer
    restart: always
    ports:
      - 8081:8080
    networks: 
      - mysql_net


# POSTGRES
  postgres:
    image: postgres
    container_name: postgres
    environment: 
      POSTGRES_PASSWORD: admin
    ports:
      - "5432:5432"
    networks:
      - mysql_net
  pgadmin4:
    image: dpage/pgadmin4
    container_name: pgadmin4
    environment: 
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: admin
    ports:
      - "15432:80"
    networks:
      - mysql_net
networks: 
  mysql_net:
    name: mysql_net
    driver: bridge

--- (another commie)

version: '3.5'

services:
  nginx:
    image: richarvey/nginx-php-fpm
    ports: 
      - "8080:80"
    container_name: nginx
    volumes:
        - ./code:/var/www/html
    networks:
      - mysql_net
networks:
  mysql_net:
    name: mysql_net

Browser other questions tagged

You are not signed in. Login or sign up in order to post.