Connect to container create by docker-compose

Hello Everybody,

I have a docker-compose.yml like this:

    version: '3'
    services:
      mysql-1:
        container_name: mysql-1
        image: mysql:5.6
        restart: always
        environment:
          - MYSQL_ROOT_PASSWORD=123456
        networks:
          vpcbr:
            ipv4_address: 10.5.0.5
      mysql-2:
        container_name: mysql-2
        image: mysql:5.6
        restart: always
        environment:
          - MYSQL_ROOT_PASSWORD=123456
        networks:
          vpcbr:
            ipv4_address: 10.5.0.6
      mysql-3:
        container_name: mysql-3
        image: mysql:5.6
        restart: always
        environment:
          - MYSQL_ROOT_PASSWORD=123456
        networks:
          vpcbr:
            ipv4_address: 10.5.0.7
    networks:
      vpcbr:
        driver: bridge
        ipam:
          config:
            - subnet: 10.5.0.0/16          

Who can help me to connect to each container to create DB and import data ? i’m using Navicat to connect.

Thanks you so much :).

Hi @dovanmanh080485,

One question.

Do you want to connect directly to the Mysql Engine or to the container shell?

If yo want to connect directly to de mysql engine, must set the port to expose in the docker-compose yaml

Otherwise, you can exec docker-compose exec <service_name> <path_to_shell> and login into the container.

Hello @mgvazquez,

Thank for your reply. I modifed docker-compose.yml like this:

version: '3'
services:
  mysql-1:
    container_name: mysql-1
    image: mysql:5.6
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=123456
    networks:
      vpcbr:
        ipv4_address: 10.5.0.5
    ports:
      - "3306"

  mysql-2:
    container_name: mysql-2
    image: mysql:5.6
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=123456
    networks:
      vpcbr:
        ipv4_address: 10.5.0.6
    ports:
      - "3306"
  mysql-3:
    container_name: mysql-3
    image: mysql:5.6
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=123456
    networks:
      vpcbr:
        ipv4_address: 10.5.0.7
    ports:
      - "3306"

networks:
  vpcbr:
    driver: bridge
    ipam:
      config:
        - subnet: 10.5.0.0/16    

I using “MySQL Workbench” https://dev.mysql.com/downloads/workbench/ to connect to each container to create DB. Can you help me config “MySQL Workbench” to connect to each container?

Thanks so much !

Hi @dovanmanh080485,

You are welcome :slight_smile:

I saw that you are using the Official MySQL image from dockerHub. I recommend you that read the doc of these image on Hub to learn how use it.

There are a lot of envirenment variables that you can use to customize your mysql containers when it’s started.
For example:

  • MYSQL_DATABASE: Automatic creation of the DB
  • MYSQL_USER: User with ALL privilege to ‘MYSQL_DATABASE’
  • MYSQL_PASSWORD: Password of ‘MYSQL_USER’

Other thing that you can do is avoid to fix an IP to the container, and map an specific port of your localhost to the mysql port on the container.

Look at this ‘docker-compose.yaml’

version: '3'
services:
  mysql-1:
    container_name: mysql-1
    image: mysql:5.6
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=123456
      - MYSQL_ROOT_HOST=%
      - MYSQL_DATABASE=test1
      - MYSQL_USER=test
      - MYSQL_PASSWORD=test
    ports:
      - "13306:3306"
  mysql-2:
    container_name: mysql-2
    image: mysql:5.6
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=123456
      - MYSQL_ROOT_HOST=%
      - MYSQL_DATABASE=test2
      - MYSQL_USER=test
      - MYSQL_PASSWORD=test
    ports:
      - "23306:3306"
  mysql-3:
    container_name: mysql-3
    image: mysql:5.6
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=123456
      - MYSQL_ROOT_HOST=%
      - MYSQL_DATABASE=test3
      - MYSQL_USER=test
      - MYSQL_PASSWORD=test
    ports:
      - "33306:3306"

With this YAML, docker will launch 3 mysql containers, mapping the port 13306 for the mysql-1, 23306 for the mysql-2 and 33306 for the mysql-3.

Then you can access the theses containers with workbench, configuring the connection to: ‘localhost: 13306’, ‘localhost: 23306’, ‘localhost: 33306’.

After that, use the credentials setted in the ‘docker-compose.yaml’.

PD: I apologize for my bad English.

2 Likes

Hello @mgvazquez

Thank you so much! it’s worked.

:slight_smile:

Thanks a lot!!! I was googling for hours and finally found the answer!

1 Like

Welcome @imnd , I’m glad I helped you help :mechanical_arm:

Hello @mgvazquez,
Thank you! it’s working. 2