T
T
tvsjke2018-03-26 20:04:03
Docker
tvsjke, 2018-03-26 20:04:03

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'

phpunit.xml
<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"/>

A separate database is needed for testing.
The problem (as I believe) is that two databases cannot work on the same port 3306
Error - Connection refused
Who did something like this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Hikmat Abdunabiev, 2018-03-26
@Khikmat

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.

C
chupasaurus, 2018-03-26
@chupasaurus

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

Save it on the host to a file (eg /home/user/test.sh) and run the one-liner:
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 question

Ask a Question

731 491 924 answers to any question