Task 2 - Create two docker container, one of which should be of mongoDB and the application should connected to the mongoDB container.
For windows - https://runnable.com/docker/install-docker-on-windows-10
Step 2 - Here, we are taking NGINX Image to work on, so to pull the Docker image for nginx, we use the Command -
docker pull nginx:alpine
Step 3 - Containers are designed to be isolated. But they can send and receive requests to other applications, using networking.
For example: a web server container might expose a port, so that it can receive requests on port 80. Or an application container might make a connection to a database container.
After that, Check that the bridge network is running: You can check it’s running by typing
docker network ls
To create your own bridge network , we need to run this command -
then, Create two container with the same network so that they can communicate with each other
like this
docker run -d -p 8080:80 --network="shruti-network" --name server1 nginx:alpine
docker run -d -p 8081:80 --network="shruti-network" --name server2 nginx:alpine
We can check the specification of container using the command
docker inspect server2
Now we have created two containers of nginx on the same network, so they should communicate with each other, for this we will try to ping one container to the other, so for that we will take shell of server2 and will try to ping server1 from the server2
docker exec -it server2 sh
server2 is succesfully able to ping the server1 - Hence they are able to communicate with each other
Create two docker container, one of which should be of mongoDB and the application should connected to the mongoDB container.
docker run -d --name my-mongo mongo:latest
docker run -d -p 8020:80 --name php-apache php:5-apache
Note: This will run a php container, but in order to be able to connect to mongo db container, you need to link this container to mongo db container. So, we will create the Linked container like this -
docker run -d -p 8020:80 --link my-mongo --name php-mongo-test php:5-apache
Now, you should be able to see two container running by typing: docker ps
command.
docker exec -it PHP_IMAGE_ID bash
Now, you are into the shell terminal of php container. You will need to install php-mongo dependencies.
Run following commands:
apt-get update
apt-get install openssl libssl-dev libcurl4-openssl-dev
pecl install mongo
echo "extension=mongo.so" > /usr/local/etc/php/conf.d/mongo.ini
Note - In above steps, we basically installed few dependencies required for mongo db connector, and installed mongo db php extension, and included that in php.ini list, and then restart the container using the docker start Container-ID
and docker stop Container-ID
Now, Prepare a phpfile in /var/www/html directory say info.php, and put following content:
<?php
print phpinfo();
On your browser, try: localhost:8082/info.php
Now, Run Php code that connects to Mongo DB - I named the File as mogo.php
<?php
$connection = new MongoClient( "mongodb://my-mongo:27017" );
$collection = $connection->selectCollection('db-name', 'collection-name');
if (!$collection) {
echo 'not connected to collection';
exit;
}
echo "MongoDB is successfully connected to the PHP container";
Test is using - http://localhost:8020/mongo.php
TASK LAUNCH THREE CONTAINER SUCH THAT THE CONNECTIVITY BETWEEN THEM SHOULD BE LIKE THIS -
A -> B and C
B -> D
C -> D
Solution - Containers in different networks can not communicate with each other If we connect all the four networks on the same network, then A also will be able to connect with the D , but this should not be happen
So, we will create one new network such that we get the connectivity as the way we want to be done -
shruti-network
A
B
C
shruti-network2
B
C
D
Note -
To disconnect a container from existing network - docker network disconnect shruti-network container-name-
A -> B and C only
So that A and D would be on different network and they cannot communicate with other.
B -> D
C -> D
Note - To check the networks - docker network ls
to create new network - docker network create shruti-network2
And, if you want to inspect that which containers are connected to your network - docker network inspect shruti-network2