Showing posts with label network. Show all posts
Showing posts with label network. Show all posts

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

Docker Network: Communicate between containers

‘docker network’ command is used to establish communication between containers.

Step 1: Create a network.
docker network create my_network_1
$docker network create my_network_1
4e7b04536608fd84cc1bfad3fb45205063417df181f72f18b8eb3b6cae825a0d


Step 2: List all the networks.
docker network ls
$docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f2fb9b25f38f        bridge              bridge              local
f0b207369485        host                host                local
4e7b04536608        my_network_1        bridge              local
7e1bfc494e53        none                null                local

Step 3: Add a container to the network ‘my_network_1’.

sleep.py
import time
while True:
    time.sleep(2)

Execute the below command to start the container container_1.

docker run --rm -d --net my_network_1 --name container_1 -v $(pwd):/src python:3 python3 /src/sleep.py

--rm: Remove the container once the command finish execution.
-d: Run the container in background, so we can't see all the log messaages of docker in terminal.
--net my_network_1: Add the container to network my_network_1.
--name container_1: Name of the container is container_1.
-v $(pwd):/src: Mount current directory to /src folder.
python:3: Image name with tag
python3 /src/sleep.py:  Command to run

$docker run --rm -d --net my_network_1 --name container_1 -v $(pwd):/src python:3 python3 /src/sleep.py
32e743cbe40f49ed9f581e2f5e4ff93f765df6ecb1724f1063b0f09ff91b9ab6

You can execute the command ‘docker container ls’ to confirm that the container is up and running.
$docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
32e743cbe40f        python:3            "python3 /src/sleep.…"   19 seconds ago      Up 18 seconds                           container_1

Step 4: Add the second container to the network ‘my_network_1’.

docker run --rm -it --net my_network_1 --name container_2 python:3 /bin/bash

Above command runs the container container_2 with image python:3 in interactive mode.
$docker run --rm -it --net my_network_1 --name container_2 python:3 /bin/bash
root@052aac3ac7c4:/#

Since both the containers container_1, container_2 are in the same network, you can ping the container ‘container_1’ from container_2.

root@052aac3ac7c4:/# ping container_1
PING container_1 (172.18.0.2) 56(84) bytes of data.
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=1 ttl=64 time=0.086 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=2 ttl=64 time=0.174 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=3 ttl=64 time=0.168 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=4 ttl=64 time=0.274 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=5 ttl=64 time=0.170 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=6 ttl=64 time=0.166 ms
^C
--- container_1 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 104ms
rtt min/avg/max/mdev = 0.086/0.173/0.274/0.054 ms

Come out of the container by executing ‘exit’ command.

Stop the container ‘container_1’.

$docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
32e743cbe40f        python:3            "python3 /src/sleep.…"   6 minutes ago       Up 6 minutes                            container_1
$
$docker container stop container_1

container_1
$
$
$docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Remove the network ‘my_network_1’ by executing the below command.
docker network rm my_network_1

$docker network rm my_network_1
my_network_1
$
$docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f2fb9b25f38f        bridge              bridge              local
f0b207369485        host                host                local
7e1bfc494e53        none                null                local
$



Previous                                                    Next                                                    Home