简体   繁体   中英

Mysql - Docker: Connection issue

My docker-compose.yml looks like this:

version: '3.3'

services:

  frontend:
    build: frontend
    container_name: yii-frontend
    ports:
      - 20080:80
    volumes:
      # Re-use local composer cache via host-volume
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      # Mount source-code for development
      - ./:/app
    networks:
      - my-marian-net

  backend:
    build: backend
    container_name: yii-backend
    ports:
      - 21080:80
    volumes:
      # Re-use local composer cache via host-volume
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      # Mount source-code for development
      - ./:/app
    networks:
      - my-marian-net

  db:
    image: mysql:8.0
    container_name: mysql8
    command: --user=root --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=verysecret
      - MYSQL_DATABASE=yii2advanced
      - MYSQL_USER=yii2advanced
      - MYSQL_PASSWORD=secret
    ports:
      - 6033:3306
    networks: 
      - my-marian-net

networks: 
  my-marian-net:
    driver: bridge

I get an error message:

'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known'

Solutions I have tried:

  • Ping from backend and frontend container to mysql8: docker exec -ti yii-frontend ping mysql8 (It works!)

  • Manually connect each container to the same network (my-marian-net) docker network connect my-marian-net mysql8 (same for each container)

  • I have connected into the mysql container and test connection there, it works.

  • I have connected from containers to mysql8 container MySQL service and it works.

So far no luck getting connected. In my code, I am using "db" as hostname since I am using bridge mode.

After trying different solutions, I have isolated the issue to a connection from outside docker, Any suggestion?

I am using macOS Mojave. Docker version 19.03

The issue was simpler than I thought, from outside container "db" service does not exist, so no possibility to get connected.

In the future when I run console commands from local, I will change host in database connection to use localhost instead of "db" or get connected to the container itself and run them from inside.

Thanks to @Smankusors for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM