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

CC Intro to Docker

The document provides a comprehensive guide to using Docker for cloud computing, detailing the setup of Docker images, containers, and networks. It explains key concepts such as Dockerfiles, images, containers, and volumes, along with practical steps for creating and deploying a web application and a MySQL database. Additionally, it emphasizes the importance of Docker networking for reliable communication between containers and the use of volumes for data persistence.

Uploaded by

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

CC Intro to Docker

The document provides a comprehensive guide to using Docker for cloud computing, detailing the setup of Docker images, containers, and networks. It explains key concepts such as Dockerfiles, images, containers, and volumes, along with practical steps for creating and deploying a web application and a MySQL database. Additionally, it emphasizes the importance of Docker networking for reliable communication between containers and the use of volumes for data persistence.

Uploaded by

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

CLOUD COMPUTING LAB UE22CS351

Note: If you face any issues, you can mail us: [email protected]

Introduction to Docker
In the last lab, we explored monolithic applications, and while setting up the app, many of you faced
several issues, including but not limited to:
●​ Setting up the environment on different operating systems.
●​ Version conflicts with installed dependencies.
●​ Issues caused by existing running processes.

Since that lab was limited to our college, we could provide help and fix these issues quickly. But
imagine if the app was built for millions of customers—resolving every individual’s issue would be
impossible.

To address the challenge of packaging and running applications without needing any environment
setup or worrying about compatibility issues, we have Docker!

Docker provides the ability to package and run an application in a loosely isolated environment called
a container. This isolation and security let you run many containers simultaneously on a single host.
●​ Containers are lightweight and include everything needed to run the application, so you don’t
have to rely on what’s installed on the host.
●​ You can share containers with others, ensuring they get the exact same setup that works
seamlessly.

Docker Concepts: Simplified Analogies


1. Dockerfile

Think of a Dockerfile as a blueprint or instruction manual. It specifies all the steps and components
needed to build your project environment (or, in Docker’s case, the image).

●​ Example: A construction blueprint says, "Lay the foundation, build the walls, install the
windows, and paint everything." Similarly, the Dockerfile tells Docker how to build an image
step-by-step.

2. Images

A Docker Image is like the pre-assembled parts of a car, ready for assembly. It contains everything
required to run your app: the code, dependencies, and configurations.

●​ Example: The car parts (engine, tires, body) are shipped as a kit (image). Once delivered, the
factory (Docker) can use them to assemble fully functional cars (containers).

3. Containers

A Docker Container is like the final, assembled car built from the image. It’s the running instance of
the image, isolated from the rest of the factory floor.

●​ Lightweight, efficient, and consistent—ready to run on any road (host).


●​ Example: You can drive this car anywhere, and it performs the same, whether it’s on smooth
city roads (local machine) or rugged highways (server).

4. Docker Hub

This is like a library or repository where you can find pre-built plans or templates.
CLOUD COMPUTING LAB UE22CS351

●​ You can download pre-made blueprints (images) or share your own designs with others.
●​ Example: Need a basic sedan (Python environment)? Download it from the library instead of
starting from scratch.

5. Volumes

Containers are designed to be temporary, so any data they hold is lost when they stop. Volumes allow
you to save this data outside the container so it persists.

●​ Example: Think of a blackboard in a classroom. Even if the teacher leaves (container stops),
the notes written on the board (volume) remain for the next class.

6. Networks

Docker provides networking features to let containers communicate with each other or with the host
machine.

●​ Example: Imagine two separate offices needing to work on the same project. A secure
intercom system (Docker networking) allows them to share updates without shouting across
the hall (public communication).

Why Use Docker?

1. Works Everywhere: Build once, run anywhere—be it your laptop, a server, or the cloud.
2. Simplifies Collaboration: Share containers with your team to ensure everyone has the exact same
setup.
3. Saves Resources: Containers are more lightweight than traditional virtual machines, using fewer
system resources.
4. Eliminates "It Works on My Machine!": Docker ensures consistency across all environments.

BEFORE WE START
1.​ Make sure your docker engine is running, (for windows before you
run docker, wsl should be running.
2.​ If you're using Windows, please use a Linux Virtual Machine or
WSL (Windows Subsystem for Linux) for the lab. Attempting
to use native Windows may lead to a very horrible experience.
You have been warned.
3.​ Double check your docker hub username.
4.​ Make sure no other containers are running on port 8080, 3306

1. Create Docker Image, and push it to docker hub and publish the service

1.​ Remember first lab that you had deployed a small portfolio website on vercel, we will
be doing something similar, for this you need to have that public github repository
which has your portfolio website code.
2.​ Make a file named Dockerfile and paste the following
FROM nginx:alpine
CLOUD COMPUTING LAB UE22CS351

WORKDIR /usr/share/nginx/html

RUN rm -rf ./*

RUN apk add --no-cache git && \


git clone <Add your repository url here> /temp-repo && \
cp -r /temp-repo/* . && \
rm -rf /temp-repo

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

1.​ FROM nginx:alpine​


This directive specifies that the base image for the Docker build will be the official
Nginx image. The :alpine tag indicates that the Alpine Linux variant of the Nginx
image will be used, which is known for its lightweight size and efficiency.
2.​ WORKDIR /usr/share/nginx/html​
This sets the working directory for the container. It defines the path where files will be
downloaded or manipulated during the build process. In this case, the path
/usr/share/nginx/html is chosen because Nginx serves files from this
directory by default.
3.​ RUN rm -rf ./*​
This command removes all existing files and directories in the working directory. It
ensures that the directory is clean and ready to receive the new content.
4.​ Next, the RUN command performs the following steps:
a.​ Installs Git to enable repository cloning.
b.​ Clones the repository into a temporary directory named /temp-repo.
c.​ Copies the content from the temporary directory to the working directory.
d.​ Deletes the temporary directory to clean up unnecessary files.
5.​ EXPOSE 80​
This instructs Docker to expose port 80 of the container to the host machine, allowing
external access to the application. Port 80 is the default HTTP port used by Nginx to
serve web content.
6.​ CMD ["nginx", "-g", "daemon off;"]​
This command defines the default behavior of the container upon startup. It launches
the Nginx server with the -g daemon off; flag, ensuring that Nginx runs in the
foreground instead of exiting, which keeps the container running.

3 . Now we build the image, (here ‘.’ signifies the directory where you have your Dockerfile)

docker build -t pes1ug2xcsxxx .

4. Now we will test the image built, we will instantiate a container from the image, also we
will port forward from 80 to 8080.

docker run -d -p 8080:80 pes1ug2xcsxxx

5. Now if you visit http://localhost:8080 you should see the portfolio site.
CLOUD COMPUTING LAB UE22CS351

6. Ok since we have created the image, and we saw its working fine. We will push it to the
docker hub. (Dockerhub-username is the username you used to login to DockerHub)

docker tag pes1ug2xcsxxx <Dockerhub-username>/pes1ug2xcsxxx:latest

7. Make sure you are logged in to docker, and then you can push your image to the world.​

docker push <Dockerhub-username>/pes1ug2xcsxxx:latest

8. Now we will deploy this, for this you have to signup to this website https://www.koyeb.com/.

9. Here we will create a webservice with docker, click on docker, provide the image path that
you pushed ( <Dockerhub-username>/pes1ug2xcsxxx:latest), Under cpu eco choose free cpu option,
and region closest to India. Click on next, then on the configuration under exposed port change default
port 8000 to 80, then Deploy ( Note that it might take a few minutes to deploy). This will be
Screenshot 1 (SS1).

10. You should see the service being deployed with the link, you can visit the link. This will be
Screenshot 2(SS2).
CLOUD COMPUTING LAB UE22CS351

Docker networks and volumes

1.​ Update the password in the mysql-dockerfile to your SRN


•​ ENV MYSQL_ROOT_PASSWORD='PES1UG21CSXXX'

2.​ Now build and run the mysql image


•​ docker build --file mysql-dockerfile -t mysql:1 .
•​ docker run -d -p 3306:3306 mysql:1

3.​ Create the mysql container. The terminal output will be the First Screenshot(SS1).
Make sure your SRN is visible and the logs show the creation of database notes.
•​ docker ps -a
•​ docker logs <container_id>

4.​ The setting up of MySQL is done. Now we move on to the web app. It's a simple
notes app. First we need an image for this app. Before creating the docker image , go
to the /utils folder, create a .env file and add the lines shown below. Make sure the
password is your SRN.

5.​ Now build the image and create the container using the commands below
•​ docker build --file Dockerfile -t notesapp:1 .
•​ docker run -d -p 8080:8080 notesapp:1
6.​ Now check the container logs using the commands below and get the error visible in
the terminal output. This will be your second Screenshot (SS2). Do you see a similar
error??. So what happened here?? As it turns out, although we have done port
forwarding the mysql container is not accessible. localhost in Docker refers to the
container's own network namespace. If you're inside the notesapp container and
you use localhost, it will try to connect to a MySQL server running within that same
CLOUD COMPUTING LAB UE22CS351

container, which doesn't exist. Hence the error.


•​ docker ps -a
•​ docker logs <container_id>

7.​ So how do we fix that error?? What if we use the mysql container's IP address?
That’s exactly what we do here. Use the command mentioned below to find the IP
address of the mysql container. You can use grep IPAddress to help find the IP
address faster. This will be the third screenshot(SS3). ( If your using windows and
grep doesn’t works, then use Findstr instead of grep )
•​ docker inspect <container_id> | grep IPAddress

8.​ Now go back and update the .env file in the /utils directory. Update the host and set
it to the IP address of the mysql container. An example is mentioned below. Note that
the IP address of the container might be different on your machine.
•​ host="172.17.0.2"

9.​ Repeat step 5 (Make sure to use a different image name). You can repeat step 6 to
ensure there are no errors now. Then you can visit localhost:8080/ and see an output
similar to the one shown below (note: if you see something different running on that
port you can change port while forwarding docker run -d -p 8090:8080 <image-name>
). Add at least 2 notes. This would be the fourth Screenshot(SS4).
CLOUD COMPUTING LAB UE22CS351

10.​So that works. But there is a major issue here. Hope you have already identified it ??
Using IP address is not reliable at all. It works fine on a local machine, but when
scaled to a thousand containers, it would not work. The reason being that in a
distributed environment containers keep going down. Their IP address keeps
changing. It would be a hassle to manually configure the IP address in the .env file
each time a container goes up or down.

11.​This is why we have a docker network. Docker networking enables


communication between containers, the host machine, and the external world. A
network can contain any number of containers. To communicate with any other
container all you would need is its name. So now bring down the containers that were
running using docker stop <container_id>.

12.​ Create a new network with the name of your choice and run the mysql container in
that network as shown below. This will be Screenshot 5 (SS5). (Here name you can
keep anything, make sure you use the same further in the lab)
•​ docker network create <network-name>
•​ docker run -d --name <container-name> -p 3306:3306 --network
<network-name> mysql:1
CLOUD COMPUTING LAB UE22CS351

13.​Now go back to the .env file in the /utils folder and update the host field. It must now
look like this. <container-name> is the name of the sql container you just started.
•​ host="<container-name>"
14.​Build the docker image of the notesapp again using the commands mentioned in the
earlier steps and create a container using that image and in the same network as the
mysql container. This will be Screenshot 6 (SS6).
•​ docker run -d -p 8080:8080 --network <network-name> <image-name>

15.​ Now you have an app that is resilient to IP changes. Now did you notice anything
else when you landed on localhost:8080 page. What happened to the notes you had
added earlier before stopping the mysql container?? The container doesn’t retain the
data that you have added by default. Stop and remove all the containers using the
commands you have learned so far.

16.​That is why you need to create docker volumes which are used to make the data
persistent. This will be Screenshot 7 (SS7).
•​ docker volume create <volume-name>
•​ docker run -d --name <container-name> -p 3306:3306 -v
<volume-name>:/var/lib/mysql --network <network-name> mysql:1
CLOUD COMPUTING LAB UE22CS351

17.​ Now stop and rerun the notesapp container. Add 2 new notes. This will be
Screenshot 8 (SS8).

18.​Now stop and remove both the running containers. This will be Screenshot 9 (SS9).

19.​Again run both the containers and take the final Screenshot(SS10). Note that all the
notes are now retained.
CLOUD COMPUTING LAB UE22CS351

You might also like