Build a custom docker network for two-tier application

To create an EC2 instance and set up a custom Docker network for a two-tier application consisting of a Python container and a MongoDB container, you can follow these steps:

github project: https://github.com/Pardeep32/microservices-k8s.git

  1. Launch an EC2 Instance:

    • Go to the AWS Management Console and navigate to the EC2 service.

    • Click on "Launch Instance" to start the instance creation wizard.

    • Choose an Amazon Machine Image (AMI), such as Ubuntu, CentOS, or Amazon Linux.

    • Select an instance type based on your requirements.

    • Configure instance details like network settings, subnet, IAM role, etc.

    • Add storage if needed.

    • Configure security groups to allow inbound traffic to your instance. Make sure to open ports for SSH (22), HTTP (80), and any other ports required by your application.

    • Review and launch the instance. Choose an existing key pair or create a new one to connect to your instance via SSH.

  2. Connect to Your EC2 Instance:

    • Once the instance is launched, connect to it via SSH using the private key associated with your key pair.

    • Use the public DNS or IP address of your instance to connect.

  3. Install Docker on the EC2 Instance:

    • Update the package index: sudo apt update

Install docker on ec2:

sudo apt-get update

sudo apt-get install docker.io

Change the ownership of docker.

sudo chown $USER /var/run/docker.sock

cat /etc/group

Create docker image from docker file:

docker build -t tws-app .

docker images

Create a custom network deep-network:

docker network create deep-network

run the conatiner:

docker run -d -p 5000:5000 --name twsapp-ctr --network deep-network tws-app:latest

go to ec2 instance security group and add 5000 in inbound group:

go to ec2ip_address:5000 and your app is up and running:

http://35.183.4.225:5000/

this is app is running on custom network i.e deep-network.

ip address show This command will list all network interfaces along with their associated IP addresses, subnet masks, and other relevant information.

The last entry appears to be a virtual Ethernet interface (veth2860849@if9) associated with a network namespace. this entry represents a virtual Ethernet interface that is part of a bridge network (br-0fc6be78d8cf) and is associated with the default network namespace. It is used for communication between containers or network namespaces on the same host.

Now, docker ps to check conatiners which are up and running.

docker exec -it 9cef5de7f928 sh use to go inside the conatiner.

The error message "sh: 1: ping: not found" indicates that the ping command is not available in the shell environment within the Docker container.

This issue usually occurs if the container does not have the ping utility installed, which is a common scenario in minimalistic or specialized container images.

To resolve this issue, you can install the ping utility inside the container. Since you're using a shell prompt inside the container, you can do this interactively:

apt update

apt install iputils-ping

# apt update
# apt install iputils-ping

After installing iputils-ping, you should be able to use the ping command successfully:

# ping google.com

This will allow you to ping external hosts from within the Docker container.

Now, run mongo db:

docker run -d --name mongo --network deep-network mongo

ec2ip_address:5000/tasks

http://35.183.4.225:5000/tasks it will show output.

go to postman to check api:

Thank you for taking the time to read this article. I hope you found it informative and helpful.