---
# System prepended metadata

title: 'LS Lab 1: Containerization and application layer load balancing'

---

:::    success

# LS Lab 1: Containerization and application layer load balancing

Tutunchy Ammar
:::


**Task 1: Get familiar with Docker Engine**

- **1. Pull Nginx v1.23.3 image from dockerhub registry and confirm it is listed in local images**

|<center>![](https://i.imgur.com/cBR7YYU.png)</center>|
|---|
||

:::info
In the first step, we pull the nginx image from docker-hub using the command "Docker pull nginx:1.23.3" with the version 1.23.3.
:::

- **2. Run the pulled Nginx as a container with the below properties
a. Map the port to 8080.
b. Name the container as nginx:<stX>.
c. Run it as daemon .
d. Access the page from your browser.**

|<center>![](https://i.imgur.com/rUSMfGY.png)</center>|
|---|

|<center>![](https://i.imgur.com/7AmJyjm.png)</center>|
|-------|
||

:::info
We used this command to construct a container in Docker.

" docker run -- name nginx-ammar-st8 -p 8080:80 -d  nginx:1.23.3 "

-p: This means map through the port, each container has a pot number and an IP address to connect to other containers.

The first port, 8080, is for docker host, while the second is for container.
nginx-ammar-st8 is the name of the container constructed using the NGINX image.
The -d option causes the container to operate in detached mode, which means it will continue to run until it is stopped but will not react to instructions entered on the command line.

:::
    
**3. Confirm port mapping.**
- **a. List open ports in host machine.**
    
    



| <center>![](https://i.imgur.com/Q9LPE9j.png)|
| -------- |
| |
    
:::info
" netstate -an | grep listen " was used to examine the open port that we utilized when building the container.
we examin from our host.
:::

- **b. List open ports inside the running container.**
    
|<center>![](https://i.imgur.com/WAAhSjv.png)</center>|
|----|

:::info
We also ran the same command " netstate -an " in the container to examine the open port that we used while building the container, which was 80 as we had specified it.
:::

- **4. Create Dockerfile similar with below properties (let’s call it container A).
a. Image tag should be Nginx v1.23.3.
b. Create a custom index.html file and copy it to your docker image to replace Nginx default web page.
c. Build the image from the Dockerfile, tag it during build as nginx:<stX>, check/validate local images, and run your custom made docker image.
d. Access via browser and validate your custom page is hosted.**
   
|<center>![](https://i.imgur.com/ZVSUWyJ.png)</center>|
|--|
    
|<center>![](https://i.imgur.com/xvtWwjy.png)</center>|
|--|    
    
|<center>![](https://i.imgur.com/5rkmsKF.png)</center>|
|--|
:::warning
We create a file named dockerfile and write within it the name of an image from Docker Hub that is nginx in our topic and some settings to construct our image and container, then we copy the file to using the command "***COPY./index.html /usr/share/nginx/html***".
***" docker build -t nginx-ammar-st8 "*** will be used to construct our images. and then execute the command "***docker run —name containerA -p 8080:80 -d nginx-ammar-st8***" to construct the container from these images, then we will check from our browser in the machine and write in the tab ***localhost:8080*** to connect to the container and view the page that was putted in the container.
:::   
    
---    
    
**Task 2: Work with multi-container environment**
    
- **1. Create another Dockerfile similar to step 1.4 (Let’s call it container B), and an index.html with different content.**
    
|<center>![](https://i.imgur.com/gK5LcpK.png)</center>|
|--|

|<center> ![](https://i.imgur.com/D8lA6ri.png)</center>|
|--|
:::danger
We repeat the instructions in the previous section to construct the image, then build the container from the image, but change the container's name and port number to containerB and 9090:80. We also tested it in a browser using localhost:9090.
:::  
- **2. Write a docker-compose file with below properties
a. Multi-build: Builds both Dockerfiles and run both images.
b. Port mapping: Container A should listen to port 8080 and container B should listen to port 9090. (They host two different web pages)
c. Volumes: Mount (bind) a directory from the host file system to Nginx containers to replace the default Nginx web page with the two index.html files created in Steps 1.4.b and 2.1.**

|<center>![](https://i.imgur.com/J0nD573.png)</center>|
|--|

:::info
To work with docker compose, first create a yaml file called dcoker-compose.yml and place our settings in it. We created two containers, each with a name, a dockerfile, a volume to copy data to Nginx, and a port to map to the container, similar to our previous work, but this time there would be too many containers running at the same time.
:::
- **3. Run the docker compose file and validate you have access to both Nginx web pages in your browser via their respective ports.**  
    
|<center>![](https://i.imgur.com/s8BBB1s.png)</center>|
|--|
    
:::info
When we used docker compose, we got two sites from two distinct containers, as seen in the figure.
:::

---
    
- **4. Configure L7 Loadbalaner
a. Install Nginx in the host machine, and configure it in front of two containers in a manner that it should distribute the load in RR approach.**

|<center>![](https://i.imgur.com/2OAfWBn.png)</center>|
|---|
    
    
|<center>![](https://i.imgur.com/LgVSjB4.png)</center>|
|---|
:::info
After installing Nginx on our system, we configured the /etc/nginx.conf, then we ran Nginx and tested the website, and it seemed to us two websites by making a refresh in the browser that was setup in that container on our server.
:::    

### Reference

[hub.docker](https://hub.docker.com/_/nginx)
[Deploying NGINX and NGINX Plus on Docker](https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-docker/)
[linuxhint](https://linuxhint.com/create-dockerfile/)
[dockercompose](https://docs.docker.com/get-started/08_using_compose/)
[docker reference](https://docs.docker.com/engine/reference/builder/)
[docs.docker](https://docs.docker.com/compose/gettingstarted/)