Answer the question
In order to leave comments, you need to log in
How to use multiple containers with mysql in Docker?
docker-compose.yml
...
version: '2.1'
volumes:
dbdata: null
services:
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
entrypoint: docker-files/app/entrypoint.sh
volumes:
- './:/var/www'
links:
- db
- redis
depends_on:
db:
condition: service_started
redis:
condition: service_started
web:
build:
context: ./
dockerfile: web.dockerfile
volumes_from:
- app
ports:
- '8080:80'
links:
- app
db:
image: 'mysql:5.7'
volumes:
- 'dbdata:/var/lib/mysql'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: grabid
ports:
- '33061:3306'
redis:
image: redis
ports:
- "6379:6379"
# for debugging purposes
phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- db
links:
- db
ports:
- '4040:80'
# testing
phpunit:
image: vcarreira/phpunit
command: -c phpunit.xml
volumes:
- './:/var/www'
links:
- db_test
depends_on:
db_test:
condition: service_started
db_test:
image: 'mysql:5.7'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: grabid_test
ports:
- '33062:3306'
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_HOST" value="db_test"/>
<env name="DB_DATABASE" value="grabid_test"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
Answer the question
In order to leave comments, you need to log in
The piece you provided is correct. Show the whole file. Maybe the problem is that while the database has not started completely, another container makes a request to it and because of it the error goes.
Inside, although they listen to one port, the IP addresses of the containers are different.
Check the settings of your applications, UMVR. Here is the test script:
mysql -uroot -hdb -proot -e "SHOW MASTER STATUS;" > /dev/null && echo "connection to db: ok" || echo "connection to db: failed" >&2
mysql -uroot -hdb_test -proot -e "SHOW MASTER STATUS;" > /dev/null && echo "connection to db_test: ok" || echo "connection to db_test: failed" >&2
docker-compose -f docker-compose-file.yaml run --rm -v '/home/user/test.sh:/test.sh' --entrypoint="bash" db /test.sh
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question