Began to master Docker. There are two images: based on the first container initialized with Php + Apache + mounted volume with php application (yii2) + put Composer and Git, based on the second - Mysql. I do all this through Docker-compose.

Docker-compose.yml content

 version: '2'
 services:
   app:
     build:.
     volumes:
       - ./app:/var/www/html
     ports:
       - "80:80"
     links:
       - db
   db:
     image: mysql: 5.7
     ports:
       - "3306: 3306"
     expose:
       - "3306"
     environment:
       MYSQL_ROOT_PASSWORD: pass
       MYSQL_DATABASE: docker_template

The question is this: I need to apply the migration by running the "php yii migrate" command. In the container with Php, I do not have Mysql, so Yii2 will give an error when starting migrations. And in the container with Mysql I do not have Php and a mounted image. What to do in this situation? Do I understand correctly that I can not connect to Mysql from within a php container?

  • It depends on how you connect, if via Unix socket, php will not find it. Connect to the ip address of the container where mysql is spinning, and it should be configured to connect from external networks. - Hills of Eternity

1 answer 1

You need to use networks , you can read more here https://docs.docker.com/compose/compose-file/#networks

services: app: build: . volumes: - ./app:/var/www/html ports: - "80:80" networks: - nw_internal db: image: mysql:5.7 ports: - "3306:3306" expose: - "3306" networks: - nw_internal environment: MYSQL_ROOT_PASSWORD: pass MYSQL_DATABASE: docker_template networks: nw_internal: 

Then the db service container will be accessible by name from the app container, and MySQL will be available in db: 3306

  • it will be available in the default grid anyway - etki
  • From the question, I realized that links works somehow differently. Ok, ok) I'll know - Andrei Mindubayev