Saturday 9 May 2020

Docker Network: example 2: Connecting to sql server

This is continuation to my previous post. In this post I am going to explain how to connect to MySQL server running in one container from other.

Step 1: Create a network.
Execute below command.
docker network create my_network_1
$docker network create my_network_1
75bee5bf8384a1631267ddfe91dc30e4a61b563a05af6a56f275b04d0d932eb5
Step 2: You can confirm the network creation by listing all the networks.
$docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f2fb9b25f38f        bridge              bridge              local
f0b207369485        host                host                local
75bee5bf8384        my_network_1        bridge              local
7e1bfc494e53        none                null                local
Step 3: Add MySQL container to the network my_network_1.
docker run --rm -d -e MYSQL_ROOT_PASSWORD=tiger --net my_network_1 --name mysql_container_1 mysql:latest
$docker run --rm -d -e MYSQL_ROOT_PASSWORD=tiger --net my_network_1 --name mysql_container_1 mysql:latest
6876928fe4be475a014b93e203c189f4c6f71d36964e77964b5e28cf1029c22b
Step 4: Add another MySQL container to the network my_network_1 and connect to the first mySQL container from it.

docker run -it --rm -e MYSQL_ROOT_PASSWORD=tiger123 --net my_network_1 --name mysql_container_2 mysql:latest /bin/bash
$docker run -it --rm -e MYSQL_ROOT_PASSWORD=tiger123 --net my_network_1 --name mysql_container_2 mysql:latest /bin/bash
root@7ec9b132b955:/#

Connect to ‘mysql_container_1’ using mysql command.
mysql -h mysql_container_1 -u root -p

root@7ec9b132b955:/# mysql -h mysql_container_1 -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>


Previous                                                    Next                                                    Home

No comments:

Post a Comment