Docker networks allow containers to communicate with each other, the host system, and external networks. Docker provides several types of networks, each designed for different use cases:
Bridge: Default, isolated, container-to-container communication on the same host.
Host: Shared network stack with the host, no isolation, performance-critical apps.
None: No network access, maximum isolation.
Custom Bridge: You can create your own bridge networks. Custom bridge networks allow automatic DNS resolution between containers.
Overlay: Multi-host communication, used in Docker Swarm, built-in encryption.
Macvlan: Direct access to physical network, custom IP assignment, legacy apps.
Step 1: Launch an EC2 Instance
With the help of this: https://lalitakashyapblog.hashnode.dev/step-by-step-guide-to-setting-up-an-ec2-instance
Step 2: Install docker
With the help of this: https://lalitakashyapblog.hashnode.dev/from-zero-to-docker-a-beginners-introduction-to-containers
Step 3: Check docker network list
docker network ls
Step 4: Create docker network
docker network create twotier
Step 5: Check docker network list again
docker network ls
Step 6: Now inspect network twotier
docker inspect twotier
Step 7: Check no container is assign in containers block
Step 8: Now create volumes directory and move into it
mkdir volumes
cd volumes/
Step 9: Create directory for "mysql" and move into it
mkdir mysql-data
cd mysql-data/
Step 10: Mount the volume in present working directory
target path - /var/lib/mysql
docker run -d -v $(pwd):/var/lib/mysql --name two-tier-mysql -e MYSQL_ROOT_PASSWORD=test@123 mysql:5.7
Step 11: Check docker container created or not
docker ps
Step 12: Now execute container
docker exec -it <container_id> bash
Step 13: Create database
mysql -u root -p
test@123
show databases;
create database kyc;
show databases;
Step 14: Create table in database and exit from container
use kyc;
CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
message TEXT
);
exit
exit
Step 15: Assign container to network
docker network connect twotier <container_ID>
Step 16: Inspect docker network twotier. Mysql container is added now
docker inspect twotier
Step 17: Came out from directory
cd
Step 18: Create directory and move into it
mkdir projects
cd projects/
Step 19: Clone repository url and move into project folder
git clone https://github.com/Lalita5Kashyap/two-tier-flask-app.git
cd two-tier-flask-app/
Step 20: List down the files
ls
Step 21: Build image for backend from dockerfile
docker build -t two-tier-backend:latest .
Step 22: Create and run container for backend and assign to network
docker run -d -p 5000:5000 -e MYSQL_HOST=two-tier-mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=test@123 -e MYSQL_DB=kyc --network twotier two-tier-backend:latest
Step 23: Give permission to instance for port "5000"
Go to security group in instance
Click SG link
- Click on "edit inbound rules"
- Click on "Add rules"
- Give "5000" in port range and select "Anywhere" in source and click Save
- Copy public dns from instance in networking
Now go to browser and paste public ip of instance
In summary, Docker networks are essential for efficient and secure container communication. By understanding and utilizing various network drivers, you can optimize your containerized applications for performance and scalability.
Thank you ๐
Keep Learning..