Assignment 2
Assignment 2
Note: The screenshots must be pasted into a Word document and sent in PDF format. The file
should be named in this manner
<Section>_<SRN>_<Name>_A2.pdf ( Eg. A_PES1UG21CSXXX_Name_A2.pdf )
IMPORTANT:
Every terminal screenshot should include your SRN. The path to your lab folder
directory should contain your SRN. Make sure you have a folder named <your_srn>
and run the tasks within this folder.
Eg. “C:\User\Name\your_srn\lab_folder\task2>”
Note:
If you are using Play-with-docker, ensure you add a new instance before starting the lab.
Verify that Docker Engine is installed correctly by running:
(Dockerfile, sample.py and docker-compose.yml files are present in the lab instructions
folder, modify only required details)
1. For the purpose of this lab make sure all the following files (present in the lab
instructions folder) are in the same directory:
a. docker-compose.yml
b. Dockerfile
c. sample.py
2. Go to the docker-compose.yml file and try to understand the syntax and what each
line does.
3. Within the same directory, run:
docker-compose up (windows)
docker compose up (linux)
What you see is that Docker compose has built your python application, started the
MongoDB server, created links internally between the containers (network) and started both
the containers together as a unified application. The python container exits since it's done
with its utility of writing and reading to the database. (Press CTRL-C to exit Docker compose
if the shell prompt is not returned)
4. Now we will scale the python application, so that we have 3 containers of the python
application, but keep only one container of the mongodb.
Take a screenshot of 3 python application writes and reads from MongoDB after scaling the
python application (2b.jpg)
Task 3 Exposing ports, docker networks :
(Dockerfile and my.html files are present in the lab instructions folder under task3-nginx.
Run the Docker container in this directory)
1. Create a sample HTML (my.html) file as given below, and modify your SRN.
(You can use the file present in the lab experiment folder)
<html>
<body>
<h1>My SRN is your_srn</h1>
<h2>I am running a nginx container!</h2>
</body>
</html>
2. Create a Dockerfile and then a docker image having an nginx base image, and
copying the html file into the default folder in the container.
Dockerfile:
(This file is also present in the lab experiment folder)
FROM nginx
COPY my.html /usr/share/nginx/html
2. Run the docker container using the previously created docker image and expose the
HTTP port using
3. Access the nginx server displaying the webpage by typing out the following url
http://localhost:80/my.html
(If you are using PWD, access it via the `Open Port` button and access port 80.
Append my.html to the end of the url)
Take a screenshot of my.html showing the web page on the browser (3b.jpg)
We will explore connectivity without docker networks and see how docker networks make it
much easier to connect within containers. To demonstrate this we will create a simple
application using a python client and a MongoDB NoSQL server.
Note: You don't need to know how to use mongodb, code to use mongodb has been provided
to you.
(Dockerfile and my.html files are present in the lab instructions folder under task3-pymongo.
Run the Docker container in this directory)
5. Run the mongodb container in a detached mode, exposing the default port (27017) of
mongodb using
6. Create a sample.py file as given below, and modify your SRN wherever mentioned.
(This file is also present in the lab experiment folder)
host = MongoClient("170.17.0.2")
host = MongoClient("mongodb")
db = host["sample_db"]
collection = db["sample_collection"]
sample_data = {"Name:":"your_name","SRN":"your_srn"}
collection.insert_one(sample_data)
print('Inserted into the MongoDB database!')
rec_data = collection.find_one({"SRN":"your_srn"})
print("Fetched from MongoDB: ",rec_data)
7. The MongoDB container is running, but we need to find out the IP address of the
mongodb container.
a. Find the container ID of the mongodb container using docker ps -a
Dockerfile:
(This file is also present in the lab experiment folder)
FROM python
RUN apt-get update
RUN pip install pymongo
COPY sample.py sample.py
CMD ["python","sample.py"]
Take a screenshot of python application successfully writing and reading from the MongoDB
database (3c.jpg).
You should see that the data was correctly inserted and fetched from the database
container.
12. Note the container id and stop running the container using
2. Run a mongodb container again, but now with the following parameters;
a. network : my-bridge-network
b. name: mongodb
c. Exposed ports: 27017
d. Image: mongo:latest
3. Go back to sample.py, comment line 3 and uncomment line 4. We are now going to
use the name of the database containers as the host name, leaving the ip address
resolution to docker.
4. Build the python app docker image again as you did previously and run the container
using the built image. You should see that insertion and retrieval have been done
successfully.
Take a screenshot showing python file being run within the network and successfully writing
and reading from MongoDB(docker command has to be clearly highlighted) (3e.jpg).
Task 4: Docker images and docker files
Make sure you have completed the sub tasks (docker pull tasks) as a part of the
pre-installations
Sub tasks (Ensure you have created an account on Docker Hub before starting this task):
1. Pull the following images onto your docker instance using docker pull
(Dockerfile and program.c is present in the lab instructions folder, modify only required
details)
1a. Open a text editor and create a C program and name it program.c
Use the following C program given in the lab manual folder as a base, only modify
your SRN:
#include <stdio.h>
int main()
{
printf("Running this inside a container !\n");
printf("My SRN is <YOUR SRN HERE>\n");
}
1b. Create your own Docker image by writing a Dockerfile (template shown below).
The image you will create will run a simple C program, after installing the GCC
compiler.
The Dockerfile must:
a. Specify the base image as ubuntu:18.04
b. Update the “apt” repository and install the GCC compiler.
c. Copy the program.c file from your instance to the docker image.
d. Compile the C program.
e. Run the ./a.out command.
Dockerfile:
(This file is also present in the lab experiment folder)
FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install gcc -y
COPY program.c program.c
RUN gcc program.c
CMD ["./a.out"]
‘myimage-name’ is the name you will give for your newly created docker image. Do not
forget to include the period after the image name. The period indicates the use of Dockerfile
in the local repository. You can replace this with the path to your Dockerfile
3. Run the container using
Take a screenshot of the C Program successfully running inside the container (4a.jpg).
Note:
● Avoid passwords with special characters like $ as it will be interpreted by
shell and the above login command will not work.
● Write your username and password within double quotes
Eg. docker login -u “abc” -p “abc12” docker.io
b. Create a tag using:
Login to docker hub and the image you pushed will appear in your repository.