Search code examples
dockerdocker-composedocker-network

How to connect to db with docker-compose which connect to another service on local machine (redash & mysql & python_script )


I have the db, which connect to python script throw network avito_connect. This script change data in MySQL. And I want to visualise this data with redash. Docker-compose file initialise redash with mysql_db. But when I try add network in mysql service - redash don't see mysql db. I don't understand why. docker-compose code below:

 version: '2'
x-redash-service: &redash-service
  image: redash/redash:8.0.0.b32245
  depends_on:
    - postgres
    - redis
  env_file: redash.env
  restart: always

services:
  server:
    <<: *redash-service
    command: server
    ports:
      - "5000:5000"
    environment:
      REDASH_WEB_WORKERS: 4

  scheduler:
    <<: *redash-service
    command: scheduler
    environment:
      QUEUES: "celery"
      WORKERS_COUNT: 1

  scheduled_worker:
    <<: *redash-service
    command: worker
    environment:
      QUEUES: "scheduled_queries,schemas"
      WORKERS_COUNT: 1

  adhoc_worker:
    <<: *redash-service
    command: worker
    environment:
      QUEUES: "queries"
      WORKERS_COUNT: 2

  redis:
    image: redis:5.0-alpine
    restart: always
  postgres:
    image: postgres:9.5-alpine
    env_file: redash.env
    restart: always
  nginx:
    image: redash/nginx:latest
    ports:
      - "80:80"
    depends_on:
      - server
    links:
      - server:redash
    restart: always

  mysql:
 #   container_name: mysql_db
    image: mysql:5.7
    stdin_open: true # docker run -i
    tty: true        # docker run -t
    volumes:
      - /Users/zzema/Documents/Docker/selenium-template/mysql:/var/lib/mysql
      - /Users/zzema/Documents/Docker/selenium-template/sql_config_path:/etc/mysql/conf.d
    restart: always
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_DATABASE=todos
      - MYSQL_USER=devuser
      - MYSQL_PASSWORD=secret

# With this uncomment lines I can't connect to my_sql from redash
#    networks:
#      avito_connect:
#        aliases:
#          - mysql
#networks:
#  avito_connect:
#    external: true

I comment not working lines of code in the end of docker-compose


Solution

  • For one container to reach another, they must be on the same Docker network.

    The overall networking environment you have available to you is described in Networking in Compose. If you do nothing, Compose will create a network named default for you, and containers will be attached to that network. If you declare networks: for a service, that replaces the default network unless you explicitly declare it.

    If you uncomment those lines, in effect you get:

    networks:
      default: {}          # created by Compose
      avito_connect: {...} # manually created
    services:
      server:
        networks:
          default: {}      # automatically inserted
      mysql:
        networks:          # no `default` because there is an explicit setting
          avito_connect: {...}
    

    Now the two services aren't on the same network and can't connect to each other.

    There are a couple of ways to work around this. One is to attach the database to both networks:

    services:
      mysql:
        networks:
          default:
          avito_connect:
    

    Another is to make the "default" network be the external network you're trying to connect to:

    version: '2.4' # minimum 2.1, must be later than 2(.0)
    networks:
      default:
        external: true
        name: avito_connect
    # and no networks: blocks in any service