I created this answer based on your docker-compose.yml file
Create new bridge network in your docker-compose.yml file:
networks:
my-new-network:
driver: bridge
It's important to connect all your containers to one network(bridge) to allow communication between them.
Add each your container to the new network:
db:
image: 'mysql:latest'
volumes:
- ./database:/var/lib/mysql
ports:
- '3306:3306'
restart: always
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: database
networks:
- my-new-network
After these changes check if the containers can communicate(send a ping from one to the other - use container names):
From nginx container:
ping db
if you don't have ping
command, install iputils:
apt-get update
apt-get install iputils-ping
If everything works well - change your Yii DB connection configuration:
return => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=db;port=3306;dbname=database', // change IP here to database container name
'username' => 'root',
'password' => 'pass',
'charset' => 'utf8',
],
I think this should help to solve your problem. Take care of communication between containers :)
Ensure also that the database can accept external connections. Default mysql configuration block external connections, so you should check your mysql configuration and add new user on which you can log in "from outside"(from external hosts/networks):
CREATE USER 'newuser'@'%' IDENTIFIED BY 'password';
mysql root user has disabled remote connections by default. More you can read here
Logs will tell you much more :) https://docs.docker.com/engine/reference/commandline/logs/
I forked your repository: https://github.com/raciniewski/docker-yii2
- Build and run all containers
docker-compose up --build


- Send ping to
db
container from other container in your new my-new-network
network:

- Done - containers can communicate