|
| 1 | +## Docker Networking Assignment: Building a Web App with Bridge Networks |
| 2 | + |
| 3 | +This assignment focuses on understanding and implementing Docker bridge networks to isolate and manage different components of a simple web application. You will create two separate bridge networks: one for the frontend and one for the backend. |
| 4 | + |
| 5 | +**Learning Objectives:** |
| 6 | + |
| 7 | +* Understand the concept of Docker bridge networks. |
| 8 | +* Learn how to create and manage custom bridge networks. |
| 9 | +* Learn how to connect containers to specific networks. |
| 10 | +* Practice inter-container communication using Docker networks. |
| 11 | +* Understand basic containerization of a web application. |
| 12 | + |
| 13 | +**Scenario:** |
| 14 | + |
| 15 | +You will build a simple web application consisting of a frontend (a basic HTML page served by Nginx) and a backend (a simple "Hello World" service using Python Flask). These components will communicate over a dedicated backend network, while the frontend will be accessible from the host machine. |
| 16 | + |
| 17 | +**Tasks:** |
| 18 | + |
| 19 | +1. **Backend Network and Service:** |
| 20 | + * Create a Docker bridge network named `backend-network`. |
| 21 | + * Create a Dockerfile for a simple Python Flask application that returns "Hello from Backend!". This application should listen on port 5000. Example: |
| 22 | + |
| 23 | + ```python |
| 24 | + from flask import Flask |
| 25 | + |
| 26 | + app = Flask(__name__) |
| 27 | + |
| 28 | + @app.route("/") |
| 29 | + def hello(): |
| 30 | + return "Hello from Backend!" |
| 31 | + |
| 32 | + if __name__ == "__main__": |
| 33 | + app.run(debug=True, host='0.0.0.0', port=5000) |
| 34 | + ``` |
| 35 | + |
| 36 | + * Build the backend image and name it `backend-app`. |
| 37 | + * Run a container from the `backend-app` image and connect it to the `backend-network`. Name the container `backend`. |
| 38 | + |
| 39 | +2. **Frontend Network and Service:** |
| 40 | + * Create a Docker bridge network named `frontend-network`. |
| 41 | + * Create a simple `index.html` file with the following content (or similar): |
| 42 | + |
| 43 | + ```html |
| 44 | + <!DOCTYPE html> |
| 45 | + <html> |
| 46 | + <head> |
| 47 | + <title>Frontend</title> |
| 48 | + </head> |
| 49 | + <body> |
| 50 | + <h1>Welcome to the Frontend</h1> |
| 51 | + <p id="backend-message">Fetching message from backend...</p> |
| 52 | + <script> |
| 53 | + fetch('http://backend:5000') // Use container name for resolution |
| 54 | + .then(response => response.text()) |
| 55 | + .then(data => document.getElementById('backend-message').innerText = data) |
| 56 | + .catch(error => document.getElementById('backend-message').innerText = "Error connecting to backend."); |
| 57 | + </script> |
| 58 | + </body> |
| 59 | + </html> |
| 60 | + ``` |
| 61 | + |
| 62 | + * Create a Dockerfile for an Nginx container that serves the `index.html` file. You can use a multi-stage build to copy the HTML file. Example Dockerfile: |
| 63 | + |
| 64 | + ```dockerfile |
| 65 | + FROM nginx:alpine AS builder |
| 66 | + COPY index.html /usr/share/nginx/html |
| 67 | + |
| 68 | + FROM nginx:alpine |
| 69 | + COPY --from=builder /usr/share/nginx/html /usr/share/nginx/html |
| 70 | + EXPOSE 80 |
| 71 | + ``` |
| 72 | + |
| 73 | + * Build the frontend image and name it `frontend-app`. |
| 74 | + * Run a container from the `frontend-app` image and connect it to the `frontend-network`. Map port 80 of the container to port 8080 of the host machine. Name the container `frontend`. Also connect the container to the `backend-network`. |
| 75 | + |
| 76 | +3. **Verification:** |
| 77 | + * Access the application by navigating to `http://localhost:8080` in your web browser. You should see the "Welcome to the Frontend" message, and the message from the backend ("Hello from Backend!") should be displayed below it. |
| 78 | + * Use `docker network inspect backend-network` and `docker network inspect frontend-network` to verify the network configurations and the connected containers. |
| 79 | + * Use `docker ps` to verify the running containers and their port mappings. |
| 80 | + |
| 81 | +**Deliverables:** |
| 82 | + |
| 83 | +* All Dockerfiles (backend and frontend). |
| 84 | +* The `index.html` file. |
| 85 | +* A brief document explaining the steps you took and any challenges you encountered. |
| 86 | +* Screenshots demonstrating the working application in your browser and the output of the `docker network inspect` commands. |
| 87 | + |
| 88 | +**Bonus:** |
| 89 | + |
| 90 | +* Use Docker Compose to orchestrate the entire setup. |
| 91 | +* Explore using a custom DNS server within Docker to resolve container names instead of relying on Docker's built-in DNS (e.g., using `dnsmasq`). |
| 92 | + |
| 93 | +This assignment will help you gain practical experience with Docker networking and container orchestration. Remember to document your steps and explain your reasoning. Good luck! |
0 commit comments