0% found this document useful (0 votes)
49 views

Assignment 2

hi

Uploaded by

kingkyrie888
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Assignment 2

hi

Uploaded by

kingkyrie888
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Deliverables

The following screenshots are to be submitted:

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>”

● 1a.jpg: Screenshot of running docker hello-world.


● 2a.jpg: Screenshot of python-mongodb application running as a
docker-compose application(logs of the application)
● 2b.jpg: Screenshot of 3 python application writes and reads from MongoDB
after scaling the python application.
● 3a.jpg: Screenshot of docker container running nginx
● 3b.jpg: Sample.html showing the web page on the browser.
● 3c.jpg: Screenshot of python application successfully writing and reading
from the MongoDB database
● 3d.jpg: Screenshot showing mongodb being run within the network(docker
command has to be clearly highlighted)
● 3e.jpg: Screenshot showing python file being run within the network and
successfully writing and reading from MongoDB(docker command has to be
clearly highlighted)
● 4a.jpg: Screenshot of C Program successfully run inside the container.
● 4b.jpg: Screenshot of the image pushed to Dockerhub.

Few key points to note:


1. All required Dockerfile(s) have been given.
2. It is very important to go through all reference material given in the pre-reading and
installation guide, as this will help you understand and debug the lab tasks.
3. Ensure docker has been installed before starting this lab.
4. Apart from the attached resources, you can always refer to the official docker
documentation. The docker documentation is well maintained and should help you
through all your tasks.https://docs.docker.com/
5. Additional resources have been given at the end of the manual.
6. When you run docker commands in the foreground, you cannot access the command
prompt in which case press [Ctrl+C] and continue with the next step or run docker in
the background mode by using -d option.
7. The lab experiment folder contains all the python, html, c and Dockerfiles. Make
changes if and when necessary.

Task 1: Installing Docker Engine

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:

docker run hello-world

Take a screenshot of running docker hello-world and name it 1a.jpg

Task 2 Docker Compose:

(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)

Take a screenshot of python-mongodb application running as a docker-compose


application(logs of the application) (2a.jpg).

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.

docker-compose up --scale pycode=3 (windows)


docker compose up --scale pycode=3 (linux)

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

Save and name the file as Dockerfile.

1. Build the docker image using


docker build -t <myimage-name> .

2. Run the docker container using the previously created docker image and expose the
HTTP port using

docker run -p 80:80 <myimage-name>

Take a screenshot of docker container running nginx (3a.jpg)

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)

4. Press Ctrl+C in the terminal to stop the nginx server

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

docker run -dp 27017:27017 mongo

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)

from pymongo import MongoClient

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

b. Run docker inspect <container_id>


Find this IP Address field and note this down

Create a Dockerfile using the template given in task 2, which:


a. Uses python base image
b. Updates the apt-repository
c. Installs pymongo using pip (pymongo 3.11.2 ).
d. Copies the sample.py from the instance to the container.
e. Runs the python command , to run the python file.

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"]

Save and name the file as Dockerfile.


8. Build the docker image using the above Dockerfile and run the container.

docker build -t <myimage-name> .

docker run <myimage-name>

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.

11. Use docker ps to get the container id of the mongo image.

12. Note the container id and stop running the container using

docker stop <container-id>


The above tasks showed the connectivity between 2 containers without a network. This can
cause problems as every time a container is created it could have a different IP address. This
is the issue docker networks tries to solve.

1. Create a docker bridge network called my-bridge-network.

docker network create my-bridge-network

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

docker run -dp 27017:27017 --network=my-bridge-network --name=mongodb mongo:latest


Take a screenshot showing mongodb being run within the network(docker command has to
be clearly highlighted) (3d.jpg).

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.

docker build -t <myimage-name> .


docker run --network=my-bridge-network <myimage-name>

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

● Ubuntu 18.04 : docker pull ubuntu

● Nginx : docker pull nginx


● Python : docker pull python

● MongoDB : docker pull mongo

(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"]

Save and name the file as Dockerfile.

2. After you have your Dockerfile, build the image using

docker build -t <myimage-name> .

‘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

docker run <myimage-name>

Take a screenshot of the C Program successfully running inside the container (4a.jpg).

4. To push the image to Docker hub:


a. Login from the terminal using

docker login -u "myusername" -p "mypassword" docker.io

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:

docker tag <myimage-name> <myusername>/<myimage-name>:<version-number>

c. Push the image using

docker push <myusername>/<myimage-name>:<version-number>

Login to docker hub and the image you pushed will appear in your repository.

Take a screenshot of the image pushed to Dockerhub (4b.jpg).


Additional Resources/ Common bugs you might encounter:

1. How to debug and fix common docker issues.


2. Not able to build/run docker containers due to insufficient space: How To Remove
Docker Images, Containers, and Volumes
3. Docker - Container is not running
4. Docker Build: A Beginner's Guide to Building Docker Images
5. Docker Container Tutorial #8 Exposing container ports
6. Overview of Docker Compose

You might also like