1

I try to run a yii2 basic project with docker in ubuntu 18.04. This is my docker-compose.yml:

version: '2'
services:
  php:
    image: yiisoftware/yii2-php:7.1-apache
    volumes:
      - ~/projects/basic/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
    ports:
      - 8000:80
    links:
      - mysql

  mysql:
   image: mysql:5.7
   volumes:
      - ~/projects/basic/.composer-docker/mysql:/var/lib/mysql
   environment:
      - "MYSQL_ROOT_PASSWORD=secret"
      - "MYSQL_USER=app"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_DATABASE=app"
   ports:
    - '33061:3306' 

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    restart: always
    environment:
      PMA_HOST: mysql
      MYSQL_USERNAME: app
      MYSQL_ROOT_PASSWORD: secret
  ports:
    - 8080:80
  volumes:
    - /sessions
  links:
    - 'mysql:mysql'

and this is config/db.php:

return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=app',
'username' => 'app',
'password' => 'secret',
'charset' => 'utf8',

I use the same folder console/migrations like in the yii2 advanced. When I run command ./yii migrate, the error "Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] Connection refused'" is occured. In the another case when host=mysql I get error "Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known'". What I did wrong?

ZaurK
  • 187
  • 1
  • 13

1 Answers1

1

while using docker-compose your host would be referred by the name of the service. so you would have the host=mysql in your case, not host=127.0.0.1.

If you run docker-compose different services are like different hosts. You run the web app in your php (service name) app which connects to the database service to the mysql (service name) app

t6nnp6nn
  • 319
  • 1
  • 6
  • Thanks, I can't figure out why the Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known' is occured when I use host=mysql and how to fix it. – ZaurK Jul 14 '18 at 09:06
  • i've put host=mysql, but create migration don't works – Diego Santa Cruz Mendezú Jan 17 '21 at 19:19