Aniks
Aniks

Reputation: 1131

Using Docker Compose to connect to mysql running in another container

I am new to docker and docker-compose.

I have mysql running in a docker container, with two databases initialized.

I can connect to it using:

mysql --port=3306 -h192.0.0.1 -uroot -ppassword

When I run:

docker ps

04bc1d308484        container-mysql   "docker-entrypoint.sh"   2 hours ago         Up 2 hours          3306/tcp            container-mysql

My query is that now I want to connect to it from another container using

docker-compose up.

another-image:
  image: another-image
  command: /opt/start.sh
  ports:
    - "8080:8080"
  environment:
    DB_URL: container-mysql
    DB_USER: root
    DB_PASSWORD: password
  external_links:
    - container-mysql

Can someone please guide me whats missing and what else is needed.

My container-mysql has two database schema's.

Example: firstdb seconddb

Upvotes: 2

Views: 1998

Answers (1)

Ronald Quenta
Ronald Quenta

Reputation: 186

Ok, found the answer to it. In case anyone else comes across the same problem, just do

docker network ls

This command lists all the docker networks.

| NETWORK ID | NAME | DRIVER | SCOPE |
| ------------- | --------------- | -------- | -------- |
| c24bee6b5b8e | network-mysql | bridge | local |

Thus, a compose file demonstrating this might look like the following:

another-image:
  image: another-image
  command: /opt/start.sh
  ports:
    - "8080:8080"
  environment:
    DB_URL: container-mysql
    DB_USER: root
    DB_PASSWORD: password
  external_links:
    - container-mysql
  network_mode: network-mysql

Upvotes: 1

Related Questions